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

/**
 * 同步发票数据
 * @param {Number} deviceType 设备类型
 * @param {String} taxDiscId 盘号
 * @param {String} beginDate 同步数据的起始时间
 * @param {String} endDate 同步数据的截止时间
 */
function invoiceSyncCommit({ deviceType, taxDiscId, beginDate, endDate }) {
    const url = '/sys/invoiceSync/commit'
    let token = cache.getToken()
    let mobile = JSON.parse(cache.getSession('user')).account
    let company = JSON.parse(cache.getSession('company'))
    let entTaxId = company.entTaxId
    let args = {
        deviceType: deviceType,
        taxDiscId: taxDiscId,
        beginDate: beginDate,
        endDate: endDate,
        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 {Number} deviceType 设备类型
 * @param {String} taxDiscId 盘号
 * @param {String} createTimeBegin 提交同步记录的开始时间
 * @param {String} createTimeEnd 提交同步记录的截止时间
 * @param {Number} pageNum :  当前页码  默认为1
 * @param {Number} pageSize : 每页数量  默认为10
 */
function invoiceSyncRecords({ deviceType, taxDiscId, createTimeBegin, createTimeEnd, pageNum = 1, pageSize = 10 }) {
    const url = '/sys/invoiceSync/pageRecords'
    let token = cache.getToken()
    let company = JSON.parse(cache.getSession('company'))
    let entTaxId = company.entTaxId
    let args = {
        deviceType: deviceType,
        taxDiscId: taxDiscId,
        // createTimeBegin: createTimeBegin,
        // createTimeEnd: createTimeEnd,
        entTaxId: entTaxId,
        pageNum: pageNum,
        pageSize: pageSize
    }
    if (createTimeBegin != "" && createTimeBegin != null) {
        args.createTimeBegin = createTimeBegin;
    }
    if (createTimeEnd != "" && createTimeEnd != null) {
        args.createTimeEnd = createTimeEnd;
    }
    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} deviceType 设备类型
 * @param {String} taxDiscId 盘号
 * @param {Number} invoiceCategory 发票种类
 * @param {String} invoiceCode 发票代码
 * @param {String} invoiceNumber 发票号码
 * @param {String} recordId 对应的同步记录主键id
 * @param {Number} pageNum :  当前页码  默认为1
 * @param {Number} pageSize : 每页数量  默认为10
 */
function invoiceSyncDetails({ deviceType, taxDiscId, invoiceCategory = -1, invoiceCode = '', invoiceNumber = '', recordId = '', pageNum = 1, pageSize = 10 }) {
    const url = '/sys/invoiceSync/pageDetails'
    let token = cache.getToken()
    let company = JSON.parse(cache.getSession('company'))
    let entTaxId = company.entTaxId
    let args = {
        deviceType: deviceType,
        taxDiscId: taxDiscId,
        entTaxId: entTaxId,
        pageNum: pageNum,
        pageSize: pageSize
    }
    if (invoiceCategory != -1 && invoiceCategory != '') {
        args.invoiceCategory = invoiceCategory
    }
    if (invoiceCode != '') {
        args.invoiceCode = invoiceCode
    }
    if (invoiceNumber != '') {
        args.invoiceNumber = invoiceNumber
    }
    if (recordId != '') {
        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)
                }
            })
    })
}

/**
 * 根据id查询发票修复同步记录进度
 * @param {String} recordId 对应的同步记录主键id
 */
function invoiceSyncRecordProgress(recordId) {
    const url = '/sys/invoiceSync/record'
    let token = cache.getToken()
    let company = JSON.parse(cache.getSession('company'))
    let entTaxId = company.entTaxId
    let args = {
        recordId: recordId,
    }
    console.log('根据id查询同步记录进度 参数：')
    console.log(args)
    return new Promise(function (resolve, reject) {
        AxiosRequestData(url, token, args, false, 2)
            .then(val => {
                console.log('根据id查询同步记录进度 结果：')
                console.log(val)
                if (val.code === 0) {
                    resolve(val)
                } else {
                    reject(val.msg)
                }
            })
    })
}

export {
    invoiceSyncCommit,
    invoiceSyncRecords,
    invoiceSyncDetails,
    invoiceSyncRecordProgress
}
