import request from '@/config/axios'

// 训练 VO
export interface TrainVO {
  id: number // id
  name: string // 训练名称
  dataId: number // 项目id
  train: number // 训练集比例
  val: number // 验证图像比例
  test: number // 测试图像比例
  round: number // 轮次
  size: number // 批次大小
  imageSize: number // 图片大小（正方向，大于这个值进行缩放，小于这个值进行放大，不是正方形将图片周围涂黑）
  modelPath: string // 预选训练模型
  path: string // 训练图片路径
  trainType: number // 类型，使用哪个gpu
  visualType?: string // 识别类型（从项目获取）
}

// 训练 API
export const TrainApi = {
  // 查询训练分页
  getTrainPage: async (params: any) => {
    return await request.get({ url: `/annotation/train/page`, params })
  },

  // 查询训练详情
  getTrain: async (id: number) => {
    return await request.get({ url: `/annotation/train/get?id=` + id })
  },

  // 新增训练
  createTrain: async (data: TrainVO) => {
    return await request.post({ url: `/annotation/train/create`, data })
  },

  // 修改训练
  updateTrain: async (data: TrainVO) => {
    return await request.put({ url: `/annotation/train/update`, data })
  },
  
  // 初始化
  InfinityTrain: async (data: TrainVO) => {
    return await request.put({ url: `/annotation/train/init`, data })
  },

  // 查找环境
  environmentInquiry: async () => {
    return await request.post({ url: `/annotation/yolo/detect-env` })
  },
  // 删除训练
  deleteTrain: async (id: number) => {
    return await request.delete({ url: `/annotation/train/delete?id=` + id })
  },

  // 导出训练 Excel
  exportTrain: async (params) => {
    return await request.download({ url: `/annotation/train/export-excel`, params })
  },

  // 获取训练结果
  getTrainStatus: async (trainId: number) => {
    return await request.get({ url: `/annotation/train-result/status?trainId=` + trainId })
  },

  // 获取训练状态
  getTrainInfoStatus: async (trainId: number) => {
    return await request.get({ url: `/annotation/train-Info/get-list?trainId=` + trainId })
  },
  // 开始训练
  handleTrain: async (data: TrainVO) => {
    return await request.post({ url: `/annotation/yolo/train?trainId=` + data.id })
  },

  // 导出训练数据
  exportTrainData: async (trainId: number) => {
    return await request.download({ url: `/annotation/train/export-data?trainId=` + trainId })
  },

  // 获取测试图片列表
  getTestImages: async (dataId: number) => {
    return await request.get({ url: `/annotation/train/test-images?dataId=` + dataId })
  },

  // 测试识别
  testRecognition: async (data: { trainId: number; imageUrl: string }) => {
    return await request.post({ url: `/annotation/train/test-recognition`, data })
  },

  // 清空测试图片列表
  clearTestImages: async () => {
    return await request.delete({ url: `/annotation/train/test-images-clean` })
  },

  // 上传测试图片
  uploadTestImages: async (files: File[]) => {
    const formData = new FormData()
    files.forEach((file, index) => {
      formData.append('files', file)
    })
    return await request.upload({ 
      url: `/annotation/train/test-images-install`, 
      data: formData 
    })
  },
}
