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

/**
 * 分页查询短信服务信息
 */
function findEntFeaturesBySMS() {
    const url = '/marketing/ent/features/findPage'
    let token = cache.getToken()
    let company = JSON.parse(cache.getSession('company'))
    let args = {
        entName: company.entName,
        entTaxId: company.entTaxId,
        featureId: 'F0000001',
        featureName: '短信服务',
        pageNum: 1,
        pageSize: 10
    }
    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) {
                    let result = null;
                    for (const item of val.data.records) {
                        if (item.featureId === 'F0000001') {
                            result = item;
                            break;
                        }
                    }
                    resolve(result)
                } else {
                    reject(val.msg)
                }
            })
    })
}

/**
 * 分页查询企业开通服务列表
 * @param {string} entName 企业名称
 * @param {string} entTaxId 企业税号
 * @param {string} deviceType 设备类型
 * @param {string} taxDiscId 盘号
 * @param {string} featureId 对应服务功能的id 如短信服务，勾选认证服务，扫码开票服务等
 * @param {string} featureName 对应服务功能的名称 如短信服务，勾选认证服务，扫码开票服务等
 * @param {number} pageNum 当前页码  默认为1
 * @param {number} pageSize 每页数量  默认为10
 */
function findEntFeatures({ entName = '', entTaxId, deviceType, taxDiscId, featureId = '', featureName = '', pageNum = 1, pageSize = 10 }) {
    const url = '/marketing/ent/features/findPage'
    let token = cache.getToken()
    let args = {
        entTaxId: entTaxId,
        deviceType: deviceType,
        taxDiscId: taxDiscId,
        pageNum: pageNum,
        pageSize: pageSize,
    };
    if (entName) {
        args.entName = entName;
    }
    if (featureId) {
        args.featureId = featureId;
    }
    if (featureName) {
        args.featureName = featureName;
    }

    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} productName 产品名称
 * @param {string} type 产品类型
 * @param {string} tags 产品标签
 * @param {string} featureId 对应服务功能的id 如短信服务，勾选认证服务，扫码开票服务等
 * @param {string} featureName 对应服务功能的名称 如短信服务，勾选认证服务，扫码开票服务等
 * @param {Boolean} enable 产品是否有效  默认查询有效的产品
 * @param {Number} pageNum 当前页码  默认为1
 * @param {Number} pageSize 每页数量  默认为10
 */
function findProduct({ productName = '', type = '', tags = '', featureId = '', featureName = '', enable = '', pageNum = 1, pageSize = 10 }) {
    const url = '/marketing/feature/product/findPage'
    let token = cache.getToken()
    let ispId = JSON.parse(cache.getSession('company')).ispId; // 服务商Id
    let args = {
        ispId: ispId,
        pageNum: pageNum,
        pageSize: pageSize
    };
    if (productName) {
        args.productName = productName;
    }
    if (type) {
        args.type = type;
    }
    if (tags) {
        args.tags = tags;
    }
    if (featureId) {
        args.featureId = featureId;
    }
    if (featureName) {
        args.featureName = featureName;
    }
    if (enable != '' && enable != undefined && enable != null) {
        args.enable = enable;
    }
    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} productId 产品id
 * @param {string} entTaxId 企业税号
 * @param {string} deviceType 设备类型
 * @param {string} taxDiscId 盘号
 * @param {string} payType 支付方式  W01 微信扫码  A01 支付宝扫码
 * @param {string} trxAmt 支付交易的金额
 * @param {string} entCouponRelId 企业优惠券关联id
 */
function transPay({ productId, entTaxId, deviceType, taxDiscId, payType, trxAmt, entCouponRelId }) {
    const url = '/marketing/trans/pay'
    let token = cache.getToken()
    let args = {
        productId: productId,
        entTaxId: entTaxId,
        deviceType: deviceType,
        taxDiscId: taxDiscId,
        payType: payType,
        trxAmt: trxAmt,
        entCouponRelId: entCouponRelId,
        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)
                }
            })
    })
}

/**
 * 根据交易id 查询交易信息
 * @param {string} transId 本地交易id
 */
function transInfo(transId) {
    const url = '/marketing/trans/info'
    let token = cache.getToken()
    let args = {
        transId,
    };
    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)
                }
            })
    })
}

/**
 * 分页查询所有支付交易记录
 * @param {string} entName 企业名称
 * @param {string} entTaxId 企业税号
 * @param {string} deviceType 设备类型
 * @param {string} taxDiscId 设备盘号
 * @param {string} productId 产品id
 * @param {string} productName 产品名称
 * @param {string} payType 支付方式  W01  微信支付  A01  支付宝支付
 * @param {string} payTimeBegin 支付时间范围查询 yyyy-MM-dd  HH:mm:ss
 * @param {string} payTimeEnd 支付时间范围查询 yyyy-MM-dd  HH:mm:ss
 * @param {string} transStatus 交易状态  1;未付款  2：已付款
 * @returns
 */
function findAllTrans({ entName = '', entTaxId = '', deviceType = '', taxDiscId = '', productId = '', productName = '', payType = '', payTimeBegin = '', payTimeEnd = '', transStatus = '', pageNum = 1, pageSize = 10 }) {
    const url = '/marketing/trans/findAllTrans'
    let token = cache.getToken()
    let args = {
        pageNum,
        pageSize
    };
    if (entName) {
        args.entName = entName;
    }
    if (entTaxId) {
        args.entTaxId = entTaxId;
    }
    if (deviceType) {
        args.deviceType = deviceType;
    }
    if (taxDiscId) {
        args.taxDiscId = taxDiscId;
    }
    if (productId) {
        args.productId = productId;
    }
    if (productName) {
        args.productName = productName;
    }
    if (payType) {
        args.payType = payType;
    }
    if (payTimeBegin) {
        args.payTimeBegin = payTimeBegin;
    }
    if (payTimeEnd) {
        args.payTimeEnd = payTimeEnd;
    }
    if (transStatus) {
        args.transStatus = transStatus;
    }

    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} transId 交易id
 * @param {string} customerType 客户抬头类型
 * @param {string} interactType 发票交付方式交付方式  1:邮箱  2:短信
 * @param {string} interactTypeDetail 交付方式的详情数据  邮箱账号之类
 * @param {string} invoiceCategory 发票种类
 * @param {string} customerName 抬头名称
 * @param {string} customerEntTaxId 企业抬头税号
 * @returns
 */
function transInvoiceCommit({ transId, customerType, interactType, interactTypeDetail, invoiceCategory, customerName, customerEntTaxId, address = "", contactPhone = "", bankName = "", bankAccountNo = "" }) {
    const url = '/marketing/trans/invoice/commit'
    let token = cache.getToken()
    let args = {
        transId,
        customerType,
        interactType,
        interactTypeDetail,
        invoiceCategory,
        customerName,
        customerEntTaxId,
        reqChannel: 4
    }
    args.address = address
    args.contactPhone = contactPhone
    args.bankName = bankName
    args.bankAccountNo = bankAccountNo
    // 电子专票
    // if (invoiceCategory == 2) {
    //     args.address = address;
    //     args.contactPhone = contactPhone;
    //     args.bankName = bankName;
    //     args.bankAccountNo = bankAccountNo;
    // }

    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)
                }
            })
    })
}

const MarketingService = {
    findEntFeaturesBySMS,
    findEntFeatures,
    findProduct,
    transPay,
    transInfo,
    findAllTrans,
    transInvoiceCommit
}

export default MarketingService;
