import request from '@/config/axios'

export interface AttachmentVO {
  url: string
  name: string
}

export interface RepairOrderVO {
  id: number
  repairNo: string
  reportTime: string
  reporter: string
  contact: string
  deviceId: number
  deviceName: string
  deviceCode: string
  faultDesc: string
  faultTime: string
  faultLevel: string
  affectSafety: boolean
  status: string
  attachments?: AttachmentVO[]  // 只支持对象数组，不再接受字符串
}

// 设备 API
export const RepairOrderApi = {

  createRepairOrder: async (data: RepairOrderVO) => {
    return await request.post({ url: `/iot/repair-order/create`, data })
  },

  updateRepairOrder: async (data: RepairOrderVO) => {
    return await request.put({ url: `/iot/repair-order/update`, data })
  },


  deleteRepairOrder: async (id: number) => {
    return await request.delete({ url: `/iot/repair-order/delete/${id}`})
  },


  getRepairOrder: async (id: number) => {
    return await request.get({ url: `/iot/repair-order/get/${id}`})
  },
  // 查询
  getRepairOrderList: async (data:RepairOrderVO) => {
    return await request.post({ url: '/iot/repair-order/list' ,data})
  },


  getDeviceList: async () => {
    return await request.get({ url: `/iot/repair-order/device-list` })
  },

  // 图片上传
  uploadImage: async (file: File): Promise<{url: string, name: string} | any> => {
    const formData = new FormData();
    formData.append('file', file);
    // 使用通用文件上传接口
    const res = await request.post({ url: '/safe/danger-image/upload', data: formData, headers: { 'Content-Type': 'multipart/form-data' } });
    console.log('上传API响应:', res);
    // 返回格式兼容report.vue
    return {
      url: res.data || res.url,
      name: file.name
    };
  }

}
