import request from '@/utils/request'
import { download } from '@/utils/request'
import {ElLoading, ElMessage} from "element-plus";
import {blobValidate, tansParams} from "@/utils/ruoyi";
import errorCode from "@/utils/errorCode";
import service from "@/utils/request";

// request.defaults.baseURL = '/ezhizao-yzbh-crm'
// 查询档案入库列表
export function listOrder(query) {
    request.defaults.baseURL = '/ezhizao-yzbh-crm'
    return request({
        url: '/business/upgrade/archiveinput/list',
        method: 'get',
        params: query
    })
}
export function listOrder2(query) {
    request.defaults.baseURL = '/ezhizao-yzbh-crm'
    return request({
        url: '/business/upgrade/archiveinput/list2',
        method: 'get',
        params: query
    })
}
// 查询档案入库详细
export function getOrder(id) {
    request.defaults.baseURL = '/ezhizao-yzbh-crm'
    return request({
        url: '/business/upgrade/archiveinput/' + id,
        method: 'get'
    })
}

// 新增档案入库
export function addOrder(data) {
    request.defaults.baseURL = '/ezhizao-yzbh-crm'
    return request({
        url: '/business/archive/order',
        method: 'post',
        data: data
    })
}

// 修改档案入库
export function updateOrder(data) {
    request.defaults.baseURL = '/ezhizao-yzbh-crm'
    return request({
        url: '/business/archive/order',
        method: 'put',
        data: data
    })
}

// 删除档案入库
export function delBinOrder(id) {
    request.defaults.baseURL = '/ezhizao-yzbh-crm'
    return request({
        url: '/business/bin/order/' + id,
        method: 'post'
    })
}

// 删除档案入库
export function delOrder(id) {
    request.defaults.baseURL = '/ezhizao-yzbh-crm'
    return request({
        url: '/business/upgrade/archiveinput/delete?id=' + id,
        method: 'post'
    })
}

export function initTaskTypes(id) {
    request.defaults.baseURL = '/ezhizao-yzbh-crm'
    return request({
        url: '/business/archive/order/initTaskTypes',
        method: 'get'
    })
}

export function exportOrder(query) {
    request.defaults.baseURL = '/ezhizao-yzbh-crm'
    download(
        "/business/upgrade/archiveinput/export",
        {
            ...query,
        },
        `合同导出_${new Date().getTime()}.xlsx`
    )
}
export function checkInput(file) {
    request.defaults.baseURL = '/ezhizao-yzbh-crm'
    return request({
        url: `/upgrade/crm/order/checkInput`,
        method: 'post',
        data: file,
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    })
}
export function importLoop(file) {
    request.defaults.baseURL = '/ezhizao-yzbh-crm'
    return request({
        url: `/upgrade/crm/order/importLoop`,
        method: 'post',
        data: file,
        headers: {
            'Content-Type': 'multipart/form-data'
        },
        timeout: 1000000
    })
}
export function exportCheckExcel(data,type,filename) {
    request.defaults.baseURL = '/ezhizao-yzbh-crm'
    return request({
        url: `/upgrade/crm/export`,
        method: 'post',
        data: {"data":data,"type":type},
        responseType: 'blob'
    }).then(async (data) => {
        const isLogin = await blobValidate(data);
        if (isLogin) {
            const blob = new Blob([data]);
            saveAs(blob, filename);
        } else {
            const resText = await data.text();
            const rspObj = JSON.parse(resText);
            const errMsg =
                errorCode[rspObj.code] || rspObj.msg || errorCode["default"];
            ElMessage.error(errMsg);
        }
    })
        .catch((r) => {
            console.error(r);
            ElMessage.error("下载文件出现错误，请联系管理员！");
        });
    // inputDownload(
    //     "/upgrade/crm/export",
    //     {
    //         data,
    //     },
    //     `合同导出_${new Date().getTime()}.xlsx`
    // )
    // inputDownload(
    //     "/upgrade/crm/export",
    //     {
    //         data:data,
    //     },
    //     `个税零申报信息_${new Date().getTime()}.xlsx`
    // )
}
function inputDownload(url, params, filename, config) {
    return service
        .post(url, params, {
            headers: { "Content-Type": "application/x-www-form-urlencoded" },
            responseType: "blob",
            timeout: 1000000,
            ...config,
        })

}
export function exportLoopTemplate() {
    request.defaults.baseURL = '/ezhizao-yzbh-crm'
    download(
        "business/archive/order/exportLoopTemplate",
        {
        },
        `循环合同模板_${new Date().getTime()}.xlsx`
    )
}

export function exportOnceTemplate() {
    request.defaults.baseURL = '/ezhizao-yzbh-crm'
    download(
        "business/archive/order/exportOnceTemplate",
        {
        },
        `单次合同模板_${new Date().getTime()}.xlsx`
    )
}

// 文件上传

// 文件上传
export function importOnce(file) {
    request.defaults.baseURL = '/ezhizao-yzbh-crm'
    return request({
        url: `/business/archive/order/importOnce`,
        method: 'post',
        data: file,
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    })
}
export function verifyOrder(data) {
    request.defaults.baseURL = '/ezhizao-yzbh-crm'
    return request({
        url: '/business/archive/order/verify',
        method: 'post',
        data: data
    })
}

export function dissolutionOrder(data) {
    request.defaults.baseURL = '/ezhizao-yzbh-crm'
    return request({
        url: '/business/archive/order/dissolution',
        method: 'post',
        data: data
    })
}

export function alterOrder(data) {
    request.defaults.baseURL = '/ezhizao-yzbh-crm'
    return request({
        url: '/business/archive/order/alter',
        method: 'post',
        data: data
    })

}

export function verifyCheckOrder(data) {
    request.defaults.baseURL = '/ezhizao-yzbh-crm'
    return request({
        url: '/business/archive/order/verifyCheck',
        method: 'post',
        data: data
    })
}
