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

/**
 * 快速红冲
 * @param {String} oriInvoiceReqFlowNo 原发票请求流水号
 * @param {String} remark 红冲备注
 */
function saveHongchong({ oriInvoiceReqFlowNo, remark }) {
    const url = '/sys/hongchongInfo/save'
    let token = cache.getToken()
    let mobile = JSON.parse(cache.getSession('user')).account
    let args = {
        mobile: mobile,
        reqChannel: 4,
        oriInvoiceReqFlowNo: oriInvoiceReqFlowNo
    };
    if (remark != '') {
        args.remark = remark
    }
    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} orderId 发票主键id
 */
function getHongchongFailedMsg(orderId) {
    const url = '/sys/hongchongInfo/failedMsg'
    let token = cache.getToken()
    let args = {
        orderId: orderId,
    }
    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} invoiceCode 发票代码
 * @param {String} invoiceNumber 发票号码
 */
function getHongchongDownloadUrl({ invoiceCode, invoiceNumber }) {
    const url = '/sys/hongchongInfo/formatFile'
    let token = cache.getToken()
    let company = JSON.parse(cache.getSession('company'))
    let entTaxId = company.entTaxId
    let args = {
        invoiceCode: invoiceCode,
        invoiceNumber: invoiceNumber,
        entTaxId: entTaxId,
        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} invoiceReqFlowNo : 红冲负数发票请求流水号
 */
function hongchongInvoiceRefresh(invoiceReqFlowNo) {
    const url = '/sys/hongchongInfo/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 hongchongInvoiceRetry(invoiceReqFlowNo) {
    const url = '/sys/hongchongInfo/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(args)
                if (v.code === 0) {
                    resolve(v)
                } else {
                    reject(v.msg)
                }
            })
    })
}

export {
    saveHongchong,
    getHongchongFailedMsg,
    getHongchongDownloadUrl,
    hongchongInvoiceRefresh,
    hongchongInvoiceRetry,
}
