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

/**
 * 保存及修改邮寄信息
 * @param {String} sendId 邮寄ID
 * @param {String} getWay 领取方式：1=自行领取，2=邮件快递
 * @param {String} receiverName 收件人姓名
 * @param {String} receiverAddress 快递地址
 * @param {String} mobilePhoneNumber 移动电话
 * @param {String} zipCode 邮编
 * @param {String} fixedPhoneNumber 固定电话
 */
function saveOrUpdate({ sendId = "", getWay, receiverName = "", receiverAddress = "", mobilePhoneNumber = "", zipCode = "", fixedPhoneNumber = "" }) {
    const url = '/sys/send/save'
    let token = cache.getToken()
    let mobile = JSON.parse(cache.getSession('user')).account
    let company = JSON.parse(cache.getSession('company'))
    let entTaxId = company.entTaxId
    let entName = company.entName
    let args = {
        getWay: getWay,
        entTaxId: entTaxId,
        entName: entName,
        mobile: mobile,
        reqChannel: 4,
    }
    if (sendId != "") {
        args.sendId = sendId;
    }
    if (getWay == 2) {
        args.receiverName = receiverName;
        args.receiverAddress = receiverAddress;
        args.mobilePhoneNumber = mobilePhoneNumber;
        args.zipCode = zipCode;
        args.fixedPhoneNumber = fixedPhoneNumber;
    }
    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)
                }
            })
    })
}

/**
 * 查询邮寄信息
 */
function getSendInfo() {
    const url = '/sys/send/getSendInfo'
    let token = cache.getToken()
    let company = JSON.parse(cache.getSession('company'))
    let entTaxId = company.entTaxId
    let args = {
        entTaxId: entTaxId,
    }
    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)
                }
            })
    })
}

const SEND = {
    saveOrUpdate,
    getSendInfo
}

export default SEND