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

/**
 * 电子发票列表查询
 * @param {String} invoiceCode ：发票代码
 * @param {String} invoiceNumber : 发票号码
 * @param {Number} invoiceCategory : 发票种类，1.增值税普通电子发票   2.增值税电子专用发票  3.增值税普通纸质发票  4 增值税专用纸质发票
 * @param {String} customerName : 客户名称
 * @param {Number} status ：发票状态   1：开票中  2：开票失败  3：正数开具成功  4：负数开具成功  5：正数已作废  6 负数已作废  7 ：空白作废
 * @param {String} invoicePerson 开票人
 * @param {Number} deviceType 开票设备类型
 * @param {String} taxDiscId 开票设备盘号
 * @param {String} invoiceDateBegin : 开票日期范围查询-开始时间 格式yyyy-MM-dd HH:mm:ss
 * @param {String} invoiceDateEnd : 开票日期范围查询-结束时间 格式yyyy-MM-dd HH:mm:ss
 * @param {Number} pageNum :  当前页码  默认为1
 * @param {Number} pageSize : 每页数量  默认为10
 */
function invoiceQuery({ invoiceCode = '', invoiceNumber = '', invoiceCategory = -1, customerName = '', status = -1, invoicePerson = "", deviceType = '', taxDiscId = '', invoiceDateBegin = '', invoiceDateEnd = '', pageNum = 1, pageSize = 10 }) {
    const url = '/sys/invoice/findPage'
    let token = cache.getToken()
    let mobile = JSON.parse(cache.getSession('user')).account
    let entTaxId = JSON.parse(cache.getSession('company')).entTaxId
    let args = {
        mobile: mobile,
        belongEntTaxId: entTaxId,
        reqChannel: 4,
        pageNum: pageNum,
        pageSize: pageSize
    }
    if (invoiceCode != '') {
        args.invoiceCode = invoiceCode
    }
    if (invoiceNumber != '') {
        args.invoiceNumber = invoiceNumber
    }
    if (invoiceCategory != -1 && invoiceCategory != '') {
        args.invoiceCategory = invoiceCategory
    }
    if (customerName != '') {
        args.customerName = customerName
    }
    if (status != -1 && status != '') {
        args.status = status
    }
    if (invoicePerson != '') {
        args.invoicePerson = invoicePerson
    }
    if (deviceType != '') {
        args.deviceType = deviceType
    }
    if (taxDiscId != '') {
        args.taxDiscId = taxDiscId
    }
    if (invoiceDateBegin != '' && invoiceDateBegin != null) {
        args.invoiceDateBegin = invoiceDateBegin
    }
    if (invoiceDateEnd != '' && invoiceDateEnd != null) {
        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 {String} invoiceReqFlowNo : 发票请求流水号
 */
function invoiceRefresh(invoiceReqFlowNo) {
    const url = '/sys/invoice/refreshResult'
    let token = cache.getToken()
    let mobile = JSON.parse(cache.getSession('user')).account
    let args = {
        invoiceReqFlowNo: invoiceReqFlowNo,
        mobile: mobile,
        reqChannel: 4
    }
    console.log('刷新开票结果 参数：')
    console.log(args)
    return new Promise(function (resolve, reject) {
        AxiosRequestData(url, token, args, false, 1)
            .then(v => {
                console.log('刷新开票结果 结果：')
                console.log(v)
                if (v.code === 0) {
                    resolve(v)
                } else {
                    reject(v.msg)
                }
            })
    })
}

/**
 * 开票失败重试
 * @param {String} invoiceReqFlowNo : 发票请求流水号
 */
function invoiceRetry(invoiceReqFlowNo) {
    const url = '/sys/blueInvoice/retry'
    let token = cache.getToken()
    let mobile = JSON.parse(cache.getSession('user')).account
    let args = {
        invoiceReqFlowNo: invoiceReqFlowNo,
        mobile: mobile,
        reqChannel: 4
    }
    console.log('开票失败重试 参数：')
    console.log(args)
    return new Promise(function (resolve, reject) {
        AxiosRequestData(url, token, args, false, 1)
            .then(v => {
                console.log('开票失败重试 结果：')
                console.log(v)
                if (v.code === 0) {
                    resolve(v)
                } else {
                    reject(v.msg)
                }
            })
    })
}

/**
 * 发票作废查询
 * @param {Number} invoiceCategory 发票种类
 * @param {String} invoiceNumber 发票号码
 * @param {Number} invoiceInvalidType 发票作废类型 0：蓝票作废 1：红票作废 2：空白作废
 * @param {String} invalidDateBegin 作废发票日期查询起始日期
 * @param {String} invalidDateEnd 作废发票日期查询截止时间
 * @param {Number} pageNum 当前页码  默认为1
 * @param {Number} pageSize 每页数量  默认为10
 */
function invoiceInvalidQuery({ invoiceCategory, invoiceNumber, invoiceInvalidType, invalidDateBegin, invalidDateEnd, pageNum = 1, pageSize = 10 }) {
    const url = '/sys/invoice/invalid/findPage'
    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 = {
        invoiceInvalidType: invoiceInvalidType,
        entTaxId: entTaxId,
        deviceType: deviceType,
        taxDiscId: taxDiscId,
        pageNum: pageNum,
        pageSize: pageSize
    }
    if (invoiceCategory != '') {
        args.invoiceCategory = invoiceCategory;
    }
    if (invoiceNumber != '') {
        args.invoiceNumber = invoiceNumber;
    }
    if (invalidDateBegin != '' && invalidDateBegin != null) {
        args.invalidDateBegin = invalidDateBegin;
    }
    if (invalidDateEnd != '' && invalidDateEnd != null) {
        args.invalidDateEnd = invalidDateEnd;
    }
    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 {
    invoiceQuery,
    invoiceRefresh,
    invoiceRetry,
    invoiceInvalidQuery
}
