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} invoiceState ：开票状态   2:开票中  3：成功  4：失败
 * @param {Number} hongchongFlag : 发票红冲状态：1.发票待成功开具，2.成功开票，可红冲 3.红冲开票中 4.红冲失败  5.红冲成功 6.非蓝字发票
 * @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 = '', invoiceState = -1, hongchongFlag = -1, 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 (invoiceState != -1) {
        args.status = invoiceState
    }
    if (hongchongFlag != -1 && hongchongFlag != '') {
        args.hongchongFlag = hongchongFlag
    }
    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/blueInvoice/refreshResult'
    let token = cache.getToken()
    let mobile = JSON.parse(cache.getSession('user')).account
    let args = {
        invoiceReqFlowNo: invoiceReqFlowNo,
        mobile: mobile,
        reqChannel: 4
    }
    return new Promise(function (resolve, reject) {
        AxiosRequestData(url, token, args, false, 1)
            .then(v => {
                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
    }
    return new Promise(function (resolve, reject) {
        AxiosRequestData(url, token, args, false, 1)
            .then(v => {
                console.log(v)
                if (v.code === 0) {
                    resolve(v)
                } else {
                    reject(v.msg)
                }
            })
    })
}

/**
 * 红冲列表查询
 * @param {Number} hongchongState : 开票状态   2:开票中  3：成功  4：失败
 * @param {Number} invoiceCategory : 发票种类 1.增值税普通电子发票   2.增值税电子专用发票  3.增值税普通纸质发票  4 增值税专用纸质发票
 * @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 hongchongInvoiceQuery({ hongchongState = -1, invoiceCategory = -1, invoiceDateBegin = '', invoiceDateEnd = '', pageNum = 1, pageSize = 10 }) {
    const url = '/sys/hongchongInfo/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 (hongchongState != -1 && hongchongState != '') {
        args.status = hongchongState
    }
    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 {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
    }
    return new Promise(function (resolve, reject) {
        AxiosRequestData(url, token, args, false, 1)
            .then(v => {
                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
    }
    return new Promise(function (resolve, reject) {
        AxiosRequestData(url, token, args, false, 1)
            .then(v => {
                console.log(v)
                if (v.code === 0) {
                    resolve(v)
                } else {
                    reject(v.msg)
                }
            })
    })
}

/**
 * 获取纸质发票打印数据
 * @param {String} reqFlowNo : 发票请求流水号
 * @param {String} invoiceType : 0：蓝票、1：红
 */
function getInvoicePrintData(reqFlowNo, invoiceType) {
    const url = '/sys/invoiceExt/getPrintData'
    let token = cache.getToken()
    let args = {
        reqFlowNo: reqFlowNo,
        invoiceType: invoiceType
    }
    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} customerName : 客户名称
 * @param {Number} pageNum :  当前页码  默认为1
 * @param {Number} pageSize : 每页数量  默认为10
 */
function getInvoiceTodo({ customerName = '', status = 2, pageNum = 1, pageSize = 10 }) {
    const url = '/sys/fapiao/todo/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,
        status: status,
        reqChannel: 4,
        pageNum: pageNum,
        pageSize: pageSize
    }
    if (customerName != '') {
        args.customerName = customerName
    }
    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 getInvoiceTodoDetail(recordId) {
    const url = '/sys/fapiao/todo/findItemsById'
    let token = cache.getToken()
    let args = { recordId: recordId }
    console.log('根据待开发票记录id获取开票项目明细 参数：')
    console.log(args)
    return new Promise(function (resolve, reject) {
        AxiosRequestData(url, token, { recordId: recordId }, false, 2)
            .then(val => {
                console.log('根据待开发票记录id获取开票项目明细 结果：')
                console.log(val)
                if (val.code === 0) {
                    resolve(val)
                } else {
                    reject(val.msg)
                }
            })
    })
}

/**
 * 待开发票删除，只能删除未提交开票的待开发票
 * @param {Array} todoInvoiceList : 待开发票列表
 */
function deleteInvoiceTodo(todoInvoiceList) {
    let mobile = JSON.parse(cache.getSession('user')).account
    const url = '/sys/fapiao/todo/delete'
    let token = cache.getToken()
    let delList = new Array()
    for (let index = 0; index < todoInvoiceList.length; index++) {
        const element = todoInvoiceList[index]
        delList[index] = {
            recordId: element.recordId,
            belongEntTaxId: element.belongEntTaxId,
            status: element.status
        }
    }
    let args = {
        mobile: mobile,
        reqChannel: 4,
        delList: delList
    }
    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} interactType : 交付方式  1:邮箱  2:手机号短信   3:微信  4:支付宝 99:其他
 * @param {String} interactTypeDetail : 交付方式的详情数据  邮箱账号之类
 * @param {JSON} cmdParam : 根据待开发票记录id 拿到对应的预存的指令参数json对象
 * @param {String} todoRecordId : 待开发票记录id
 */
function invoiceTodoCommit({ interactType, interactTypeDetail, cmdParam, todoRecordId }) {
    const url = '/sys/fapiao/todo/commit'
    let token = cache.getToken()
    let mobile = JSON.parse(cache.getSession('user')).account
    let args = {
        mobile: mobile,
        reqChannel: 4,
        interactType: interactType,
        interactTypeDetail: interactTypeDetail,
        cmdParam: cmdParam,
        todoRecordId: todoRecordId
    }
    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} recordId : 待开发票记录id
 */
function getInvoiceTodoCmdParam(recordId) {
    const url = '/sys/fapiao/todo/findCmdParam'
    let token = cache.getToken()
    let args = { recordId: recordId }
    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)
                }
            })
    })
}

/**
 * 获取企业发票领用存信息
 */
function getEntPurchaseQuery() {
    const url = '/sys/invoiceExt/purchaseQuery'
    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 = {
        entTaxId: entTaxId,
        deviceType: deviceType,
        taxDiscId: taxDiscId
    }
    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,
    hongchongInvoiceQuery,
    hongchongInvoiceRefresh,
    hongchongInvoiceRetry,
    getInvoicePrintData,
    getInvoiceTodo,
    getInvoiceTodoDetail,
    deleteInvoiceTodo,
    invoiceTodoCommit,
    getInvoiceTodoCmdParam,
    getEntPurchaseQuery
}
