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

// 生产订单 类型定义
export interface OrderItem {
  id?: number,
  productionCode?: string
  name?:  string

  code?:  string

  sort?: number

  quantity?: number

  projectId?: number
  projectName?: string

  parentId?: number

  sfProduct?: number

  procurement?: number

  beginTime?:  string

  endTime?:  string

  actualStartTime?:  string

  actualEndTime?:  string

  remark?:  string

  createTime?:  string

  createBy?:  string

  assingWorkId?:  string

  assingWorkTime?:  string

  assingWorkOperationId?:  string

  assingWorkOperationTime?:  string

  starter?: number
  
  orderCode?:  string

  routeCode?:  string

  proWorkshop?:  string
}

export interface KingdeePrdMo {
  productionOrderNo: string
  billNo: string
  workShopName: string
  materialCode: string
  quantity: number | null
  specModel: string | null
  unit: string | null
  materialName: string | null
  routingNo: string | null
  planStartTime: string | null
  planFinishTime: string | null
  pickMtrlStatus: string | null
  children: KingdeeProcessRoute[]
}


/** 金蝶工艺路线工序（树子节点） */
export interface KingdeeProcessRoute {
  /** 工序号 FOperNumber */
  operNumber: number | null
  /** 工作中心 FWorkCenterId.FName */
  workCenterName: string | null
  /** 生产车间 FDepartmentId.FName */
  departmentName: string | null
  /** 工序名称 FProcessProperty */
  processName: string | null
  /** 工序说明 FOperDescription */
  operDescription: string | null
  /** 活动单位 FActivity1UnitID.FName */
  activityUnit: string | null
  /** 工序控制码 FOptCtrlCodeId.FName */
  optCtrlCodeName: string | null
  /** 活动量 FActivity1Qty */
  activityQty: number | null
  /** 计划开始时间 yyyy-MM-dd HH:mm:ss */
  planStartTime: string | null
  /** 计划结束时间 yyyy-MM-dd HH:mm:ss */
  planFinishTime: string | null
}


// 生产订单 API
export const orderItemApi = {
  // 分页查询
  page(params: any) {   //{ page: number; pageSize: number; code?:  string; projectId?: number; beginTime?:  string; endTime?:  string }
    return request({ url: '/biz/orderItem/page', method: 'get', params })
  },

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

  // 新增
  create(data: any) { //OrderItem
    return request({ url: '/biz/orderItem', method: 'post', data })
  },

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

  // 删除
  delete(ids:  any) {
    return request({ url: `/biz/orderItem/${ids.join(',')}`, method: 'delete' })
  },
  //派工
  assignWork(data: any) {
    return request({ url: '/biz/orderItem/assignWork', method: 'post', data })
  }, 
  //根据生产编号查询预生产订单
  preCreation(data: any) {
    return request({ url: '/biz/orderItem/preCreation', method: 'post', data })
  },
    //根据生产编号查询预生产订单
  preCreationInfo(id: number, params:any) {
    return request({ url: `/biz/orderItem/preCreation/${id}`, method: 'get', params})
  },

  //查询项目名称
  xmoptions(params: any) { 
    return request({ url: '/biz/orderProject/options', method: 'get', params })
  },

  //根据项目查询物料
  wloptions(params: any) {  //projectId
    return request({ url: '/biz/orderItem/options', method: 'get', params })
  },

  holdTemporarilySave(id: number, data: KingdeePrdMo[]){
    return request({ url: `/biz/orderItem/kingdee/temporarily/save/${id}`, method: 'post', data })
  },
  // 同步订单和计划表
  synchronizationSave(id: number, data: KingdeePrdMo[]) {
    return request({ url: `/biz/orderItem/kingdee/order/create/${id}`, method: 'post', data })
  },
  // 导出
  export(params?: {
    ids?: string[]
    code?: string
    projectName?: string
    orderCode?: string
    routeCode?: string
    proWorkshop?: string
    beginTime?: string
    endTime?: string
  }) {
    const p: Record<string, any> = {}
    if (params?.ids?.length) p.ids = params.ids.join(',')
    if (params?.code !== undefined && params?.code !== null) p.code = params.code
    if (params?.projectName !== undefined && params?.projectName !== null) p.projectName = params.projectName
    if (params?.orderCode !== undefined && params?.orderCode !== null) p.orderCode = params.orderCode
    if (params?.routeCode !== undefined && params?.routeCode !== null) p.routeCode = params.routeCode
    if (params?.proWorkshop !== undefined && params?.proWorkshop !== null) p.proWorkshop = params.proWorkshop
    if (params?.beginTime !== undefined && params?.beginTime !== null) p.beginTime = params.beginTime
    if (params?.endTime !== undefined && params?.endTime !== null) p.endTime = params.endTime
    return request({ url: `/biz/orderItem/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/orderItem/import`,
      method: 'post',
      data: formData,
      headers: { 'Content-Type': 'multipart/form-data' }
    })
  },

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