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

// 委外记录表 类型定义
export interface Outsourcing {
  id?: number

  name?:  string

  code?:  string

  businessType?: number

  businessId?: number

  businessCode?:  string

  orderItemId?: number

  quantity?: number

  supplierId?: number

  supplierName?:  string

  unitPrice?:  string

  totalAmount?:  string

  beginPreparationTime?:  string

  endPreparationTime?:  string

  beginTime?:  string

  endTime?:  string

  remark?:  string

  createTime?:  string

  createBy?:  string

  overdue?: number

}

// 委外记录表 API
export const outsourcingApi = {
  // 分页查询
  page(params: { page: number; pageSize: number; id?: number; name?:  string; businessType?: number }) {
    return request({ url: '/biz/outsourcing/page', method: 'get', params })
  },

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

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

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

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

  // 导出
  export(params?: { ids?:  string[]; id?: number; name?:  string; businessType?: number }) {
    const p: Record<string, any> = {}
    if (params?.ids?.length) p.ids = params.ids.join(',')
    if (params?.id !== undefined && params?.id !== null) p.id = params.id
    if (params?.name !== undefined && params?.name !== null) p.name = params.name
    if (params?.businessType !== undefined && params?.businessType !== null) p.businessType = params.businessType
    return request({ url: `/biz/outsourcing/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/outsourcing/import`,
      method: 'post',
      data: formData,
      headers: { 'Content-Type': 'multipart/form-data' }
    })
  },

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