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

// 派工工单汇报记录 类型定义
export interface SubmitLog {
  id?: number

  assingWorkId?: number

  processId?: number

  quantity?: number

  createTime?:  string

  createBy?:  string

  materialName?:string

  materialCode?:string

  materialQty?:number

  assingStatus?: number

  remark?: string

}

// 派工工单汇报记录 API
export const submitLogApi = {
  // 分页查询
  page(params: { page: number; pageSize: number; submitStatus?: number }) {
    return request({ url: '/mes/report/page', method: 'get', params })
  },

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

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

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

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

  // 导出
  export(params?: { ids?:  string[]; id?: 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
    return request({ url: `/mes/report/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/submitLog/import`,
      method: 'post',
      data: formData,
      headers: { 'Content-Type': 'multipart/form-data' }
    })
  },

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

  //汇报统计
  statistics(params?: {time?:string; type?:string}){
      return request({url:'/mes/report/statistics',method:'get',params})
  },


  //按派工单id查询汇报
  getByDispatch(dispatchId:number){
    return request({url:`/mes/report/bydispatch/${dispatchId}`,method:"get"})
  }
}
