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

// 工艺路线主表 类型定义
export interface ProcessRoute {
  id?: number

  code?:  string

  name?:  string

  materialCode?:  string

  materialName?:  string

  version?:  string

  status?: number

  source?: number

  sourceId?:  string

  remark?:  string

  createTime?:  string

  createBy?:  string

  changeTime?:  string

  changeBy?:  string

}

// 工艺路线主表 API
export const processRouteApi = {
  // 分页查询
  page(params: { page: number; pageSize: number; id?: number; name?:  string; status?: number }) {
    return request({ url: '/biz/processRoute/page', method: 'get', params })
  },

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

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

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

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

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

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