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

import {QcResultDetail} from "@/api/qcResultDetail.ts";

// 质检单表 类型定义
export interface QualityTesting {
    id?: number

    qcNo?:  string

    sourceType?: number

    sourceId?: number

    sourceName?:  string

    orderId?: number

    processId?: number

    itemId?: number

    totalQty?:  number

    status?: number

    inspectorId?: number

    inspectTime?:  string

    traceType?: number

    createBy?: number

    createTime?:  string

    updateTime?:  string

}

// 质检单表 API
export const qualityTestingApi = {
    // 分页查询
    page(params: { page: number; pageSize: number; qcNo?: string; status?: number }) {
        return request({ url: '/mes/qc/list', method: 'get', params })
    },

    // 获取详情
    detail(id: number) {
        return request({ url: `/mes/qc/${id}`, method: 'get' })
    },

    // 新增
    create(data: QualityTesting) {
        return request({ url: '/mes/qc', method: 'post', data })
    },

    // 修改
    update(data: QualityTesting) {
        return request({ url: '/mes/qc', method: 'put', data })
    },

    // 删除
    delete(ids: number[]) {
        return request({ url: `/mes/qc/${ids.join(',')}`, method: 'delete' })
    },
    //提交判定明细
    submit(data: QcResultDetail[]) {
        return request({ url: '/mes/qc/judge', method: 'put', data })
    },

    submitBF(data: QcResultDetail[]) {
        return request({ url: '/mes/qc/partial', method: 'post', data })
    },
    //质检撤回
    cancel(data:{assingId:number,remark:string}) {
        console.log(data);
        
        return request({ url: `/mes/dispatch/revoke`, method: 'put',data })
    },

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

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