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

/**
 * 发票汇总统计
 * @param {Number} deviceType 开票设备类型
 * @param {String} taxDiscId 开票设备盘号
 * @param {Number} invoiceCategory 发票种类
 * @param {String} invoiceDateBegin 开始时间
 * @param {String} invoiceDateEnd 结束时间
 */
function InvoiceReports({ deviceType, taxDiscId, invoiceCategory = -1, invoiceDateBegin = '', invoiceDateEnd = '' }) {
    const url = '/sys/invoiceExt/reports'
    let token = cache.getToken()
    let mobile = JSON.parse(cache.getLocal('user')).account
    let entTaxId = JSON.parse(cache.getLocal('company')).entTaxId
    let args = {
        deviceType: deviceType,
        taxDiscId: taxDiscId,
        mobile: mobile,
        entTaxId: entTaxId,
        reqChannel: 4
    }
    if (invoiceCategory != -1 && invoiceCategory != '') {
        args.invoiceCategory = invoiceCategory
    }
    if (invoiceDateBegin != '') {
        args.invoiceDateBegin = invoiceDateBegin
    }
    if (invoiceDateEnd != '') {
        args.invoiceDateEnd = invoiceDateEnd
    }
    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)
                }
            })
    })
}

/**
 * 纸质发票作废
 * @param {Number} invoiceInvalidType 发票作废类型 0：已开蓝票  1： 已开红票  2: 空白发票
 * @param {Number} invoiceCategory 发票种类    3.增值税普通纸质发票  4 增值税专用纸质发票 支持纸质发票作废
 * @param {String} invoiceCode 发票代码
 * @param {String} invoiceNumber 发票号码
 */
function InvoiceInvalid({ invoiceInvalidType, invoiceCategory, invoiceCode, invoiceNumber }) {
    const url = '/sys/invoiceExt/invalid'
    let token = cache.getToken()
    let user = JSON.parse(cache.getLocal('user'));
    let mobile = user.account
    let userName = user.userName
    let company = JSON.parse(cache.getLocal('company'))
    let entTaxId = company.entTaxId
    let deviceType = company.defaultDeviceInfo.deviceType
    let taxDiscId = company.defaultDeviceInfo.taxDiscId
    let args = {
        invoiceInvalidType: invoiceInvalidType,
        invoiceCategory: invoiceCategory,
        invoiceCode: invoiceCode,
        invoiceNumber: invoiceNumber,
        invalidOper: userName,
        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 {String} entTaxId 企业税号
 * @param {String} orderId 系统内部订单id
 * @param {Number} invoiceType 发票类型 0：蓝票；1：红票
 */
function InvoiceResultData(entTaxId, orderId, invoiceType) {
    const url = '/sys/invoiceExt/getResultData'
    let token = cache.getToken()
    // let entTaxId = JSON.parse(cache.getLocal('company')).entTaxId
    let args = {
        orderId: orderId,
        invoiceType: invoiceType,
        entTaxId: entTaxId,
    }
    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)
                }
            })
    })
}

/**
 * 企业发票领用存信息
 * @param {Number} invoiceCategory 发票种类 1.增值税普通电子发票   2.增值税电子专用发票  3.增值税普通纸质发票  4 增值税专用纸质发票
 */
function EntPurchaseQuery(entTaxId, deviceType, taxDiscId,invoiceCategory) {
    const url = '/sys/invoiceExt/purchaseQuery'
    let token = cache.getToken()
    let args = {
        entTaxId: entTaxId,
        deviceType: deviceType,
        taxDiscId: taxDiscId
    }
    if (invoiceCategory != '') {
        args.invoiceCategory = invoiceCategory;
    }
    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)
                }
            })
    })
}

export {
    InvoiceReports,
    InvoiceInvalid,
    InvoiceResultData,
    EntPurchaseQuery
}
