import { AxiosRequestByBearer } from '@/data/axios_request'
import cache from "@/utils/cache"

/**
 * 获取税控服务器设备信息
 * @param {String} host_ip 税控服务器IP
 */
function getDeviceInfo(host_ip) {
    const url = '/v1/sk/get_device_info'
    let token = cache.getToken()
    let args = {
        host_ip: host_ip,
    }
    console.log('获取税控服务器设备信息 参数：')
    console.log(args)
    return new Promise(function (resolve, reject) {
        AxiosRequestByBearer(url, token, args, false, 1)
            .then(val => {
                console.log('获取税控服务器设备信息 结果：')
                console.log(val)
                if (val.code === 200) {
                    resolve(val)
                } else {
                    reject(val.message)
                }
            })
    })
}

/**
 * 扫描税控服务器,整合修改设备信息
 * 包括了更新税控服务器、编辑统一受理地址端口、修改设备发票上传地址及企业参数下载
 * @param {String} acceptance_ip 统一受理IP地址
 * @param {String} acceptance_port 统一受理端口号
 * @param {String} app_upload_url 应用服务器地址
 * @param {String} server_type 税控服务器类型，1-税控盘组；3-税务UKey
 * @param {String} host_ip 税控服务器IP
 */
function editDeviceServer({ acceptance_ip, acceptance_port, app_upload_url, server_type, host_ip }) {
    const url = '/v1/sk/edit_device_info'
    let token = cache.getToken()
    let args = {
        acceptance_ip: acceptance_ip,
        acceptance_port: acceptance_port,
        app_upload_url: app_upload_url,
        app_upload_url: app_upload_url,
        server_type: server_type,
        host_ip: host_ip,
    }
    console.log('扫描税控服务器 参数：')
    console.log(args)
    return new Promise(function (resolve, reject) {
        AxiosRequestByBearer(url, token, args, false, 1, 300000)
            .then(val => {
                console.log('扫描税控服务器 结果：')
                console.log(val)
                if (val.code === 200) {
                    resolve(val)
                } else {
                    reject(val.message)
                }
            })
    })
}

/**
 * 获取企业录入信息
 * @param {String} host_ip 税控服务器IP
 * @param {String} tax_no 税号
 * @param {String} device_no 服务器编号
 */
function getEnterpriseEnter({ host_ip, tax_no, device_no }) {
    const url = '/v1/sk/get_enterprise_enter'
    let token = cache.getToken()
    let args = {
        host_ip: host_ip,
        tax_no: tax_no,
        device_no: device_no,
    }
    console.log('获取企业录入信息 参数：')
    console.log(args)
    return new Promise(function (resolve, reject) {
        AxiosRequestByBearer(url, token, args, false, 1)
            .then(val => {
                console.log('获取企业录入信息 结果：')
                console.log(val)
                if (val.code === 200) {
                    resolve(val)
                } else {
                    reject(val.message)
                }
            })
    })
}

/**
 * 设备管理账号上架
 * 初始化设备信息，同步设备终端信息到云端
 * @param {String} host_ip 税控服务器IP
 * @param {String} tax_no 税号
 * @param {String} device_no 服务器编号
 * @param {String} ent_type 企业类型 1-一般纳税人，2-小规模纳税人，3-起征点以下纳税人
 * @param {String} ent_name 企业名称
 * @param {String} short_name 企业简称
 * @param {String} contacts 企业联系人
 * @param {String} ent_address 企业地址
 * @param {String} ent_phone 企业联系电话
 * @param {String} tax_office_code 税局代码
 * @param {String} tax_office_name 税局名称
 * @param {String} bank_account 企业银行账号
 * @param {String} bank_name 企业银行名称
 * @param {String} device_pos 设备位置
 * @param {String} device_type 设备类型 1-税控盘,5-税控ukey，默认1
 * @param {String} available_taxes 企业可用税率
 * @param {String} drawers 开票人
 * @param {String} payees 收款人
 * @param {String} province_city 省市   用- 分割
 * @param {String} reserve_mobile 上架预留企业管理者手机号
 * @param {String} reserve_name 上架预留企业管理者姓名，作为默认开票人 企业联系人
 * @param {String} reviewers 审核人
 */
function createDeviceAccount({ host_ip, tax_no, device_no, ent_type, ent_name, short_name, contacts, ent_address, ent_phone,
    tax_office_code = '', tax_office_name = '', bank_account, bank_name, device_pos = '', device_type = '',
    available_taxes, drawers, payees, province_city = '', reserve_mobile, reserve_name, reviewers = '' }) {
    const url = '/v1/sk/device_account_create'
    let token = cache.getToken()
    let mobile = JSON.parse(cache.getLocal('user')).account
    let args = {
        host_ip: host_ip,
        tax_no: tax_no,
        device_no: device_no,
        ent_type: ent_type,
        ent_name: ent_name,
        short_name: short_name,
        contacts: contacts,
        ent_address: ent_address,
        ent_phone: ent_phone,
        tax_office_code: tax_office_code,
        tax_office_name: tax_office_name,
        bank_account: bank_account,
        bank_name: bank_name,
        device_pos: device_pos,
        device_type: device_type,
        available_taxes: available_taxes,
        drawers: drawers,
        payees: payees,
        province_city: province_city,
        reserve_mobile: reserve_mobile,
        reserve_name: reserve_name,
        reviewers: reviewers,

        mobile: mobile,
    }
    console.log('设备管理账号上架 参数：')
    console.log(args)
    return new Promise(function (resolve, reject) {
        AxiosRequestByBearer(url, token, args, false, 1)
            .then(val => {
                console.log('设备管理账号上架 结果：')
                console.log(val)
                if (val.code === 200) {
                    resolve(val)
                } else {
                    reject(val.message)
                }
            })
    })
}

/**
 * 设备发票修复
 * 对增值税专用发票、增值税普通发票等进行修复
 * @param {String} host_ip 税控服务器IP
 * @param {String} tax_no 税号
 * @param {String} device_no 服务器编号
 * @param {String} start_date 起始日期
 * @param {String} end_date 终止日期
 */
function deviceInvoiceRecovery({ host_ip = '', tax_no, device_no, start_date = '', end_date = '' }) {
    const url = '/v1/sk/device_invoice_recovery'
    let token = cache.getToken()
    let args = {
        tax_no: tax_no,
        device_no: device_no,
    }
    if (host_ip != '') {
        args.host_ip = host_ip;
    }
    if (start_date != '') {
        args.start_date = start_date;
    }
    if (end_date != '') {
        args.end_date = end_date;
    }
    console.log('设备发票修复 参数：')
    console.log(args)
    return new Promise(function (resolve, reject) {
        AxiosRequestByBearer(url, token, args, false, 1)
            .then(val => {
                console.log('设备发票修复 结果：')
                console.log(val)
                if (val.code === 200) {
                    resolve(val)
                } else {
                    reject(val.message)
                }
            })
    })
}

/**
 * 查询设备上架状态
 * @param {String} host_ip 税控服务器IP
 * @param {String} deviceNos 税号
 */
 function deviceLaunchState({ host_ip, deviceNos}) {
    const url = '/v1/sk/device_launch_state'
    let token = cache.getToken()
    let args = {
        host_ip: host_ip,
        deviceNos: deviceNos,
    }
    console.log('查询设备上架状态 参数：')
    console.log(args)
    return new Promise(function (resolve, reject) {
        AxiosRequestByBearer(url, token, args, false, 1)
            .then(val => {
                console.log('查询设备上架状态 结果：')
                console.log(val)
                if (val.code === 200) {
                    resolve(val)
                } else {
                    reject(val.message)
                }
            })
    })
}

/**
 * 网上抄报税及反写清卡
 * @param {string} tax_no 税号
 * @param {string} device_no 设备编号
 */
 function taxDeclaration(tax_no,device_no) {
    const url = '/v1/sk/online_tax_declaration'
    let token = cache.getToken()
    let args = {
        tax_no: tax_no,
        device_no: device_no,
    }
    console.log('网上抄报税及反写清卡 参数：')
    console.log(args)
    return new Promise(function (resolve, reject) {
        AxiosRequestByBearer(url, token, args, false, 1)
            .then(val => {
                console.log('网上抄报税及反写清卡 结果：')
                console.log(val)
                if (val.code === 200) {
                    resolve(val)
                } else {
                    reject(val.msg)
                }
            })
    })
}

const SKServer = {
    getDeviceInfo,
    editDeviceServer,
    getEnterpriseEnter,
    createDeviceAccount,
    deviceInvoiceRecovery,
    deviceLaunchState,
    taxDeclaration
}

export default SKServer