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

// 下发记录表 类型定义
export interface IssueRecord {
    id?: number

    issueCode?:  string

    issueType?:  string

    issueTypeName?:  string

    sourceId?: number

    sourceCode?:  string

    sourceName?:  string

    deviceId?: number

    status?: number

    statusName?:  string

    issueTime?:  string

    issueBy?: number

    completeTime?:  string

    result?:  string

    remark?:  string

    createTime?:  string

    createBy?: number

    assingId?: number

    processId?: number

}

// 下发记录表 API
export const issueRecordApi = {
    // 分页查询
    page(params: { page: number; pageSize: number; issueCode?: string; status?: number;deviceId?:number }) {
        return request({ url: '/biz/issueRecord/page', method: 'get', params })
    },

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

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

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

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

    // 导出
    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: `/biz/issueRecord/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/issueRecord/import`,
            method: 'post',
            data: formData,
            headers: { 'Content-Type': 'multipart/form-data' }
        })
    },

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

    //设备下发记录
    getDeviceDispatchLogList(deviceId: number) {
        return request({ url: `/biz/issueRecord/deviceIssueRecords/${deviceId}`, method: 'get' })
    }
}