import { request } from '@/utils/request'

// 叫料记录 类型定义
export interface CallingMaterials {
  id?: number

  userId?: number

  deviceId?: number

  status?: number

  createTime?:  string

}

// 叫料记录 API
export const callingMaterialsApi = {
  // 分页查询
  page(params: { page: number; pageSize: number; deviceId?: number; status?: number }) {
    return request({ url: '/biz/callingMaterials/page', method: 'get', params })
  },

  // 获取详情
  detail(id:  string) {
    return request({ url: `/biz/callingMaterials/${id}`, method: 'get' })
  },

  // 新增
  create(data: CallingMaterials) {
    return request({ url: '/biz/callingMaterials', method: 'post', data })
  },

  // 修改
  update(data: CallingMaterials) {
    return request({ url: '/biz/callingMaterials', method: 'put', data })
  },

  // 删除
  delete(ids:  string[]) {
    return request({ url: `/biz/callingMaterials/${ids.join(',')}`, method: 'delete' })
  },

  // 导出
  export(params?: { ids?:  string[]; deviceId?: number; status?: number }) {
    const p: Record<string, any> = {}
    if (params?.ids?.length) p.ids = params.ids.join(',')
    if (params?.deviceId !== undefined && params?.deviceId !== null) p.deviceId = params.deviceId
    if (params?.status !== undefined && params?.status !== null) p.status = params.status
    return request({ url: `/biz/callingMaterials/export`, method: 'get', params: p, responseType: 'blob' })
  },

  // 导入
  importData(file: File) {
    const formData = new FormData()
    formData.append('file', file)
    return request<{ success: number; fail: number; errors: string[] }>({
      url: `/biz/callingMaterials/import`,
      method: 'post',
      data: formData,
      headers: { 'Content-Type': 'multipart/form-data' }
    })
  },

  // 下载导入模板
  downloadTemplate() {
    return request({ url: `/biz/callingMaterials/template`, method: 'get', responseType: 'blob' })
  }
}
