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

/** 委外工单 */
export interface Outsourcing {
  id?: number
  outsourceCode?: string
  name?: string
  code?: string
  businessType?: number
  businessId?: number
  businessCode?: string
  orderItemId?: number
  quantity?: number
  supplierId?: number
  supplierName?: string
  unitPrice?: number | string
  totalAmount?: number | string
  beginPreparationTime?: string
  endPreparationTime?: string
  beginTime?: string
  endTime?: string
  remark?: string
  createTime?: string
  createBy?: string
  overdue?: number
  /** 0草稿 1执行中 2已完成 3已关闭 */
  status?: number
  /** 1正常 2紧急 3低 */
  priority?: number
  issuedQuantity?: number | string
  receivedQuantity?: number | string
  qualifiedQuantity?: number | string
}

export const OUTSOURCE_STATUS_MAP: Record<number, { label: string; type: 'default' | 'info' | 'success' | 'warning' | 'error' }> = {
  0: { label: '草稿', type: 'default' },
  1: { label: '执行中', type: 'info' },
  2: { label: '已完成', type: 'success' },
  3: { label: '已关闭', type: 'default' },
}

export const OUTSOURCE_PRIORITY_MAP: Record<number, { label: string; type: 'default' | 'info' | 'success' | 'warning' | 'error' }> = {
  1: { label: '正常', type: 'info' },
  2: { label: '紧急', type: 'error' },
  3: { label: '低', type: 'default' },
}

export const outsourcingApi = {
  page(params: {
    page: number
    pageSize: number
    id?: number
    name?: string
    businessType?: number
    outsourceCode?: string
    supplierName?: string
    status?: number
  }) {
    return request<{ list: Outsourcing[]; total: number; page: number; pageSize: number }>({
      url: '/biz/outsourcing/page',
      method: 'get',
      params,
    })
  },

  detail(id: number | string) {
    return request<Outsourcing>({ 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: number[]) {
    return request({ url: `/biz/outsourcing/${ids.join(',')}`, method: 'delete' })
  },

  export(params?: { ids?: number[]; id?: number; name?: string; businessType?: number }) {
    const p: Record<string, unknown> = {}
    if (params?.ids?.length) p.ids = params.ids.join(',')
    if (params?.id != null) p.id = params.id
    if (params?.name) p.name = params.name
    if (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' })
  },
}
