import { AxiosRequestData } from '@/data/axios_request'
import cache from '@/utils/cache'

/**
 * 执行发票导出
 * @param {Number} exportType 导出类型
 * @param {Number} invoiceCategory 发票种类
 * @param {String} beginDate 同步数据的起始时间
 * @param {String} endDate 同步数据的截止时间
 */
function invoiceExportCommit({ exportType, invoiceCategory, beginDate, endDate }) {
    const url = '/sys/invoiceExport/commit'
    let token = cache.getToken()
    let mobile = JSON.parse(cache.getSession('user')).account
    let company = JSON.parse(cache.getSession('company'))
    let entTaxId = company.entTaxId
    let deviceType = company.defaultDeviceInfo.deviceType
    let taxDiscId = company.defaultDeviceInfo.taxDiscId
    let args = {
        exportType: exportType,
        invoiceCategory: invoiceCategory,
        beginDate: beginDate,
        endDate: endDate,
        entTaxId: entTaxId,
        deviceType: deviceType,
        taxDiscId: taxDiscId,
        mobile: mobile,
        reqChannel: 4,
    }
    console.log('执行发票导出 参数：')
    console.log(args)
    return new Promise(function (resolve, reject) {
        AxiosRequestData(url, token, args, false, 1)
            .then(val => {
                console.log('执行发票导出 结果：')
                console.log(val)
                if (val.code === 0) {
                    resolve(val)
                } else {
                    reject(val.msg)
                }
            })
    })
}

/**
 * 分页查询发票导出记录
 * @param {Number} exportType 导出类型
 * @param {Number} invoiceCategory 发票种类
 * @param {String} createTimeBegin 提交同步记录的开始时间
 * @param {String} createTimeEnd 提交同步记录的截止时间
 * @param {Number} pageNum 当前页码  默认为1
 * @param {Number} pageSize 每页数量  默认为10
 */
function invoiceExportRecords({ exportType, invoiceCategory, createTimeBegin, createTimeEnd, pageNum = 1, pageSize = 10 }) {
    const url = '/sys/invoiceExport/pageRecords'
    let token = cache.getToken()
    let company = JSON.parse(cache.getSession('company'))
    let entTaxId = company.entTaxId
    let deviceType = company.defaultDeviceInfo.deviceType
    let taxDiscId = company.defaultDeviceInfo.taxDiscId
    let args = {
        exportType: exportType,
        invoiceCategory: invoiceCategory,
        // createTimeBegin: createTimeBegin,
        // createTimeEnd: createTimeEnd,
        entTaxId: entTaxId,
        deviceType: deviceType,
        taxDiscId: taxDiscId,
        pageNum: pageNum,
        pageSize: pageSize
    }
    if (createTimeBegin != "" && createTimeBegin != null) {
        args.createTimeBegin = createTimeBegin;
    }
    if (createTimeEnd != "" && createTimeEnd != null) {
        args.createTimeEnd = createTimeEnd;
    }
    console.log('分页查询发票导出记录 参数：')
    console.log(args)
    return new Promise(function (resolve, reject) {
        AxiosRequestData(url, token, args, false, 2)
            .then(val => {
                console.log('分页查询发票导出记录 结果：')
                console.log(val)
                if (val.code === 0) {
                    resolve(val)
                } else {
                    reject(val.msg)
                }
            })
    })
}

/**
 * 根据id查询发票导出状态
 * @param {String} recordId 对应的同步记录主键id
 */
function invoiceExportRecordStatus(recordId) {
    const url = '/sys/invoiceExport/record'
    let token = cache.getToken()
    let args = {
        recordId: recordId,
    }
    console.log('根据id查询发票导出状态 参数：')
    console.log(args)
    return new Promise(function (resolve, reject) {
        AxiosRequestData(url, token, args, false, 2)
            .then(val => {
                console.log('根据id查询发票导出状态 结果：')
                console.log(val)
                if (val.code === 0) {
                    resolve(val)
                } else {
                    reject(val.msg)
                }
            })
    })
}

export {
    invoiceExportCommit,
    invoiceExportRecords,
    invoiceExportRecordStatus
}
