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

/**
 * 获取发票清单
 * @param {String} orderId 订单id
 * @param {Number} pageNum 当前页码  默认为1
 * @param {Number} pageSize 每页数量  默认为10
 */
function GetGoodsList(orderId, pageNum = 1, pageSize = 10) {
    const url = '/sys/invoice/pageItems'
    let token = cache.getToken()
    let entTaxId = JSON.parse(cache.getSession('company')).entTaxId
    let args = {
        orderId: orderId,
        pageNum: pageNum,
        pageSize: pageSize,
        entTaxId: entTaxId,
        reqChannel: 4
    }
    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} orderId 发票主键id
 */
function GetInvoiceFailedMsg(orderId) {
    const url = '/sys/invoice/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)
                }
            })
    })
}

/**
 * 获取正数发票版式文件下载地址  如果不存在或者当前链接无效，服务端重新生成对应的版式文件，返回url
 * @param {String} invoiceCode 发票代码
 * @param {String} invoiceNumber 发票号码
 */
function GetInvoiceDownloadUrl({ invoiceCode, invoiceNumber }) {
    const url = '/sys/invoice/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 请求流水号
 * @param {string} interactType 交付方式  1:邮箱  2:手机号短信   3:微信  4:支付宝  5：不交付
 * @param {string} interactTypeDetail 交付方式的详情数据  邮箱账号之类
 */
 function invoiceDelivery({ invoiceReqFlowNo, interactType,interactTypeDetail }) {
    const url = '/sys/invoice/delivery'
    let token = cache.getToken()
    let args = {
        invoiceReqFlowNo: invoiceReqFlowNo,
        interactType: interactType,
        interactTypeDetail: interactTypeDetail,
        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 GetInvoiceFileUrl(invoiceReqFlowNo) {
    const url = '/sys/invoice/getFileUrl'
    let token = cache.getToken()
 
    let args = {
        invoiceReqFlowNo: invoiceReqFlowNo
    };
    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 {
    GetGoodsList,
    GetInvoiceFailedMsg,
    GetInvoiceDownloadUrl,
    invoiceDelivery,
    GetInvoiceFileUrl
}
