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

/**
 * 发票申领状态信息查询
 * @param {Number} invoiceCategory 1.增值税普通电子发票   2.增值税电子专用发票  3.增值税普通纸质发票  4 增值税专用纸质发票
 * @param {*} handleStatus 申领状态（0申领中，1成功，2失败）
 * @param {*} applyDateBegin 申领日期 开始
 * @param {*} applyDateEnd 申领日期  结束
 * @param {*} pageNum 分页查询页号
 * @param {*} pageSize 每页显示的数量
 */
function GetInvoicePurchaseList({ invoiceCategory, handleStatus, applyDateBegin, applyDateEnd, pageNum = 1, pageSize = 10 }) {
    const url = '/sys/invoicePurchase/statusInfos'
    let token = cache.getToken()
    let mobile = JSON.parse(cache.getSession('user')).account
    let company = JSON.parse(cache.getSession('company'))
    let entTaxId = company.entTaxId
    let deviceType = company.defaultDeviceInfo.deviceType
    let taxDiscId = company.defaultDeviceInfo.taxDiscId
    let args = {
        pageNo: pageNum,
        pageSize: pageSize,
        entTaxId: entTaxId,
        deviceType: deviceType,
        taxDiscId: taxDiscId,
        reqChannel: 4,
        mobile: mobile
    }
    if (invoiceCategory != -1 && invoiceCategory != '') {
        args.invoiceCategory = invoiceCategory
    }
    if (handleStatus !== -1 && handleStatus !== '') {
        args.handleStatus = handleStatus
    }
    if (applyDateBegin != '' && applyDateBegin != null) {
        args.applyDateBegin = applyDateBegin
    }
    if (applyDateEnd != '' && applyDateEnd != null) {
        args.applyDateEnd = applyDateEnd
    }
    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 GetPrepareInfo() {
    const url = '/sys/invoicePurchase/preInfo'
    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)
                }
            })
    })
}

/**
 * 发票申领请求
 * @param {String} invoiceKindCode 发票种类编码     发票种类如  2016版增值税普通发票（二联折叠票）根据申领准备信息获取对应值
 * @param {String} invoiceKindName 发票种类名称 根据申领准备信息获取对应值
 * @param {String} invoiceCateCode 发票类别编码  发票类别如 纸质专票，纸质普票 不转换为本地系统发票类别编码   根据申领准备信息获取对应值
 * @param {String} invoiceCateName 发票类别名称   根据申领准备信息获取对应值
 * @param {Number} applyNum 申领份数
 * @param {String} operatorName 发票申领人姓名
 * @param {String} paperType 发票申领人的证件类型  根据申领准备信息获取对应的证件类型编码
 * @param {String} paperTypeChn 发票申领人的证件中文说明
 * @param {String} paperNumber 发票申领人证件号码
 * @param {String} remark 申请备注
 * @param {Number} getWay 领票方式  1：自行领取  2：快递配送
 * @param {String} receiverName 收件人姓名    getway==2时必输
 * @param {String} receiverAddress 快递地址   getway==2时必输
 * @param {String} mobilePhoneNumber 移动电话   getway==2时必输
 * @param {String} zipCode 邮编   getway==2时必输
 * @param {String} fixedPhoneNumber 固定电话   getway==2时必输
 */
function CommitInvoicePurchase({
    invoiceKindCode, invoiceKindName, invoiceCateCode, invoiceCateName,
    applyNum, operatorName, paperType, paperTypeChn, paperNumber, remark,
    getWay, receiverName = null, receiverAddress = null, mobilePhoneNumber = null, zipCode = null, fixedPhoneNumber = null }) {
    const url = '/sys/invoicePurchase/apply'
    let token = cache.getToken()
    let mobile = JSON.parse(cache.getSession('user')).account
    let company = JSON.parse(cache.getSession('company'))
    let entTaxId = company.entTaxId
    let deviceType = company.defaultDeviceInfo.deviceType
    let taxDiscId = company.defaultDeviceInfo.taxDiscId
    let args = {
        invoiceKindCode: invoiceKindCode,
        invoiceKindName: invoiceKindName,
        invoiceCateCode: invoiceCateCode,
        invoiceCateName: invoiceCateName,
        applyNum: applyNum,
        operatorName: operatorName,
        paperType: paperType,
        paperTypeChn: paperTypeChn,
        paperNumber: paperNumber,
        remark: remark,
        getWay: getWay,
        receiverName: receiverName,
        receiverAddress: receiverAddress,
        mobilePhoneNumber: mobilePhoneNumber,
        zipCode: zipCode,
        fixedPhoneNumber: fixedPhoneNumber,
        entTaxId: entTaxId,
        deviceType: deviceType,
        taxDiscId: taxDiscId,
        reqChannel: 4,
        mobile: mobile
    }
    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} invoiceCateCode 发票类别编码  发票类别如 纸质专票，纸质普票 不转换为本地系统发票类别编码   根据申领准备信息获取对应值
 * @param {String} invoiceCateName 发票类别名称   根据申领准备信息获取对应值
 * @param {String} applySerialNumber 申领序列号
 */
function RevokeInvoicePurchase({ invoiceCateCode, invoiceCateName, applySerialNumber }) {
    const url = '/sys/invoicePurchase/revoke'
    let token = cache.getToken()
    let mobile = JSON.parse(cache.getSession('user')).account
    let company = JSON.parse(cache.getSession('company'))
    let entTaxId = company.entTaxId
    let deviceType = company.defaultDeviceInfo.deviceType
    let taxDiscId = company.defaultDeviceInfo.taxDiscId
    let args = {
        invoiceCateCode: invoiceCateCode,
        invoiceCateName: invoiceCateName,
        applySerialNumber: applySerialNumber,
        entTaxId: entTaxId,
        deviceType: deviceType,
        taxDiscId: taxDiscId,
        reqChannel: 4,
        mobile: mobile
    }
    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} invoiceCateCode 发票类别编码  发票类别如 纸质专票，纸质普票 不转换为本地系统发票类别编码   根据申领准备信息获取对应值
 * @param {String} invoiceCateName 发票类别名称   根据申领准备信息获取对应值
 * @param {String} applySerialNumber 申领序列号
 * @param {Array} details 申领发票详情信息集合
 */
function ConfirmInvoicePurchase({ invoiceCateCode, invoiceCateName, applySerialNumber, details }) {
    const url = '/sys/invoicePurchase/confirm'
    let token = cache.getToken()
    let mobile = JSON.parse(cache.getSession('user')).account
    let company = JSON.parse(cache.getSession('company'))
    let entTaxId = company.entTaxId
    let deviceType = company.defaultDeviceInfo.deviceType
    let taxDiscId = company.defaultDeviceInfo.taxDiscId
    let args = {
        invoiceCateCode: invoiceCateCode,
        invoiceCateName: invoiceCateName,
        applySerialNumber: applySerialNumber,
        details: details,
        entTaxId: entTaxId,
        deviceType: deviceType,
        taxDiscId: taxDiscId,
        reqChannel: 4,
        mobile: mobile
    }
    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} applySerialNumber 申领序列号
 */
function GetInvoicePurchaseRecord(applySerialNumber) {
    const url = '/sys/invoicePurchase/record'
    let token = cache.getToken()
    let args = {
        applySerialNumber: applySerialNumber
    }
    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 增值税专用纸质发票
 * @param {Number} distributeStatus 0：待分发 1：分发中 2：已分发
 * @param {*} pageNum 分页查询页号
 * @param {*} pageSize 每页显示的数量
 */
function GetInvoicePurchaseDistributeInfos({ invoiceCategory, distributeStatus, pageNum = 1, pageSize = 10 }) {
    const url = '/sys/invoicePurchase/distributeInfos'
    let token = cache.getToken()
    let mobile = JSON.parse(cache.getSession('user')).account
    let company = JSON.parse(cache.getSession('company'))
    let entTaxId = company.entTaxId
    let deviceType = company.defaultDeviceInfo.deviceType
    let taxDiscId = company.defaultDeviceInfo.taxDiscId
    let args = {
        invoiceCategory: invoiceCategory,
        distributeStatus: distributeStatus,
        pageNo: pageNum,
        pageSize: pageSize,
        entTaxId: entTaxId,
        deviceType: deviceType,
        taxDiscId: taxDiscId,
        mobile: mobile
    }
    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 {
    GetInvoicePurchaseList,
    GetPrepareInfo,
    CommitInvoicePurchase,
    RevokeInvoicePurchase,
    ConfirmInvoicePurchase,
    GetInvoicePurchaseRecord,
    GetInvoicePurchaseDistributeInfos
}
