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

// AssingRework 类型定义
export interface AssingRework {
    id?: number

    processId?: number

    userId?: number

    quantity?: number

    completedQuantity?: number

    completedTime?:  string

    createTime?:  string

    createBy?:  string

    qualityReturn?: number

    qualityReturnNum?:  string

    qualityIds?:  string

    status?: number

    reason?:  string

    pauseTime?:  string

    assingStatus?: number

    返工派工单号?:  string

}

// AssingRework API
export const assingReworkApi = {
    // 分页查询
    page(params: { page: number; pageSize: number; id?: number; status?: number }) {
        return request({ url: '/biz/assingRework/page', method: 'get', params })
    },

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

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

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

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

    // 获取列表
    allList() {
        return request({ url: '/mes/dispatch/all', method: 'get' })
    },

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

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