import request from '@/config/axios'

// 创建成品质检
export const createFinishedProductQuality = (data: any) => {
  return request.post({
    url: '/mes/finished-product-quality/create',
    data
  })
}

// 更新成品质检
export const updateFinishedProductQuality = (data: any) => {
  return request.put({
    url: '/mes/finished-product-quality/update',
    data
  })
}

// 删除成品质检
export const deleteFinishedProductQuality = (id: number) => {
  return request.delete({
    url: '/mes/finished-product-quality/delete',
    params: { id }
  })
}

// 审核成品质检（将状态变更为已完成）
export const approveFinishedProductQuality = (id: number) => {
  return request.put({
    url: '/mes/finished-product-quality/approve',
    params: { id }
  })
}

// 获取成品质检详情
export const getFinishedProductQuality = (id: number) => {
  return request.get({
    url: '/mes/finished-product-quality/get',
    params: { id }
  })
}

// 获取成品质检分页列表
export interface FinishedProductQualityPageParams {
  pageNo: number
  pageSize: number
  code?: string
  workOrderCode?: string
  productName?: string
  status?: number
  qualified?: boolean
  inspectorName?: string
  inspectionTime?: [string, string]
}

export const getFinishedProductQualityPage = (params: FinishedProductQualityPageParams) => {
  return request.get({
    url: '/mes/finished-product-quality/page',
    params
  })
}

// 定义成品质检状态枚举
export enum FinishedProductQualityStatusEnum {
  PENDING = 0, // 待检
  INSPECTING = 1, // 检验中
  COMPLETED = 2 // 已完成
}

// 获取已入库成品质检分页列表
export interface WarehousedQualityPageParams {
  pageNo: number
  pageSize: number
  workOrderCode?: string
  weightMin?: number
  weightMax?: number
  steviaGlycosidesPercentMin?: number
  steviaGlycosidesPercentMax?: number
  inspectionTime?: [string, string]
  createTime?: [string, string]
}

export const getWarehousedFinishedProductQualityPage = (params: WarehousedQualityPageParams) => {
  return request.get({
    url: '/mes/finished-product-quality/warehoused-page',
    params
  })
}

// 导出已入库成品质检数据
export const exportWarehousedFinishedProductQuality = (params: WarehousedQualityPageParams) => {
  return request.download({
    url: '/mes/finished-product-quality/warehoused-export',
    params
  })
}

// 定义成品质检数据类型（与后端新接口对齐）
export interface FinishedProductQuality {
  id?: number
  code: string
  workOrderId?: number
  workOrderCode: string
  productId?: number
  productCode?: string
  productName?: string
  weight?: number
  status: FinishedProductQualityStatusEnum | number
  inspectionTime: string
  qualified: 0 | 1 | number
  unqualifiedReason?: string
  inspectorName: string
  attachmentUrls?: string
  remark?: string
  
  // 甜菊糖产品检验字段
  steviaGlycosidesPercent?: number
  steviaRd?: number
  steviaRa?: number
  steviaStv?: number
  steviaRf?: number
  steviaRc?: number
  steviaDa?: number
  steviaRv?: number
  steviaRb?: number
  steviaSb?: number
  steviaMoisture?: number
  steviaAshPercent?: number
  steviaPhValue?: number
  steviaSpecificAbsorbance?: number
  steviaTransmittance?: number
  
  createTime?: string
}
