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

/**
 * 获取勾选税控所属期信息
 */
function getDeductionSyncInputData() {
    const url = '/sys/invoice/deduction/syncInputData'
    let token = cache.getToken()
    let company = JSON.parse(cache.getSession('company'))
    // let entTaxId = "9122222222222A0090";
    let args = {
        entTaxId: company.entTaxId,
        deviceType: company.defaultDeviceInfo.deviceType,
        taxDiscId: company.defaultDeviceInfo.taxDiscId
    }
    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} certificationType 认证类型1-抵扣，2-退税，3-代办退税，4-不抵扣 5-撤销抵扣  6-撤销不抵扣
 * @param {Number} checkStatus 勾选状态 1：未勾选 2：勾选中 3：已勾选 4：勾选失败
 * @param {String} invoiceDateBegin 开票日期起始时间
 * @param {String} invoiceDateEnd 开票日期截止时间
 * @param {String} invoiceCode 发票代码
 * @param {String} invoiceNumber 发票号码
 * @param {String} inputInvoiceCategory 进项发票种类， 01-增值税专用发票 02-货运运输业增值税专用发票 03-机动车销售统一发票 14-增值税电子普通发票（通行费）
 * @param {String} status 发票状态：0-正常 1-失控 2-作废 3-红冲 4-异常
 * @param {String} sellerName 销方名称
 * @param {Number} cancelStatus 申请撤销状态 1：未申请撤销 2：撤销种 3：申请撤销成功 4：撤销失败
 * @param {String} manageState 管理状态0-正常 1-异常 2-疑似⾮正常
 * @param {Number} pageNum 当前页码 默认为1
 * @param {Number} pageSize 每页数量 默认为10
 */
function findDeductionUndeductedPage({ certificationType="",deductible, invoiceDateBegin, invoiceDateEnd, invoiceNumber = "", invoiceCode = "", inputInvoiceCategory = "", status = "", sellerName = "", cancelStatus = -1, manageState = "", pageNum = 1, pageSize = 10 }) {
    const url = '/sys/invoice/deduction/findUndeductedPage'
    let token = cache.getToken()
    let entTaxId = JSON.parse(cache.getSession('company')).entTaxId
    // let entTaxId = "9122222222222A0090";
    let args = {
        entTaxId: entTaxId,
        deductible: deductible,
        invoiceDateBegin: invoiceDateBegin,
        invoiceDateEnd: invoiceDateEnd,
        pageNum: pageNum,
        pageSize: pageSize,
    }
    if (certificationType != '' && certificationType != null) {
        args.certificationType = certificationType;
    }
    if (invoiceNumber != "" && invoiceNumber != null) {
        args.invoiceNumber = invoiceNumber;
    }
    if (invoiceCode != "" && invoiceCode != null) {
        args.invoiceCode = invoiceCode;
    }
    if (inputInvoiceCategory != "" && inputInvoiceCategory != null) {
        args.inputInvoiceCategory = inputInvoiceCategory;
    }
    if (status !== '' && status != null) {
        args.status = status;
    }
    if (sellerName != "" && sellerName != null) {
        args.sellerName = sellerName;
    }
    if (cancelStatus != -1 && cancelStatus != '') {
        args.cancelStatus = cancelStatus
    }
    if (manageState != '') {
        args.manageState = manageState
    }

    console.log('分页获取未认证的发票 参数：')
    console.log(args)
    return new Promise(function (resolve, reject) {
        AxiosRequestData(url, token, args, false, 2)
            .then(val => {
                console.log('分页获取未认证的发票 结果：')
                console.log(JSON.stringify(val))
                if (val.code === 0) {
                    resolve(val)
                } else {
                    reject(val.msg)
                }
            })
    })
}



/**
 * 认证抵扣申请
 * @param {String} period 税控所属期
 * @param {Number} delayDeDuct 是否延期认证 0-否 1-是
 * @param {Array} deductionApplyInvoices 勾选的发票集合
 */
function invoiceDeductionApply(period, delayDeDuct, deductionApplyInvoices) {
    const url = '/sys/invoice/deduction/deductApply'
    let token = cache.getToken()
    let mobile = JSON.parse(cache.getSession('user')).account
    let entTaxId = JSON.parse(cache.getSession('company')).entTaxId
    // let entTaxId = "9122222222222A0090";
    let args = {
        period: period,
        delayDeDuct: delayDeDuct,
        deductionApplyInvoices: deductionApplyInvoices,
        entTaxId: entTaxId,
        mobile: mobile,
        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} period 税控所属期
 * @param {Number} delayDeDuct 是否延期认证 0-否 1-是
 * @param {Array} deductionApplyInvoices 勾选的发票集合
 */
function invoiceDeductionCancel(period,delayDeDuct, deductionCancelInvoices) {
    const url = '/sys/invoice/deduction/deductCancel'
    let token = cache.getToken()
    let mobile = JSON.parse(cache.getSession('user')).account
    let entTaxId = JSON.parse(cache.getSession('company')).entTaxId
    // let entTaxId = "9122222222222A0090";
    let args = {
        period: period,
        delayDeDuct:delayDeDuct,
        deductionCancelInvoices: deductionCancelInvoices,
        entTaxId: entTaxId,
        mobile: mobile,
        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)
                }
            })
    })
}

export {
    getDeductionSyncInputData,
    findDeductionUndeductedPage,
    invoiceDeductionApply,
    invoiceDeductionCancel,
}
