//验证付费凭证,通过后跳转
const evidence = item => {
    const isLeader = wx.getStorageSync('isLeader');
    console.log(item)
    return new Promise((resolve) => {
        //应用是否开通，开通直接跳转
        if (item.isneedpay) {
            update(false).then(res => {
                let p = null;
                try {
                    if (item.systemappid) {
                        let app = getApp().globalData.queryPer.query(wx.getStorageSync('userauth'), ["E-订单"], ["商品档案"])[0].apps
                        p = app.find(v => v.name == item.name)
                    } else {
                        p = res.find(v => v.vame == item.vame);
                    }
                } catch (error) {
                    p = null;
                }
                if (p.isneedpay) {
                    wx.showModal({
                        title: '提示',
                        content: `当前模块未付费，是否付费使用？`,
                        confirmText: "去付费",
                        cancelText: isLeader ? "下次再说" : "提醒老板",
                        complete: (res) => {
                            if (res.confirm) createOrder();
                            if (res.cancel && !isLeader) sendMessage();
                            resolve(false)
                        }
                    })
                } else {
                    resolve(true)
                }
            })
        } else {
            resolve(true)
        }
    })
};
//创建新的订单
const createOrder = (query = "") => {
    getApp().globalData.http.basic({
        "classname": "system.payorder.payorder",
        "method": "createOrder",
        "content": {},
    }).then(res => {
        console.log("新建订单", res)
        if (res.msg != '成功') return wx.showModal({
            title: '提示',
            content: res.msg,
            showCancel: false,
        })
        wx.navigateTo({
            url: '/pages/teams/addOrder?sys_payorderid=' + res.data.sys_payorderid + query
        })
    })
}
//查询版本到期情况
const vIndate = () => {
    const http = getApp().globalData.http;
    const isLeader = wx.getStorageSync('isLeader');
    return http.basic({
        "classname": "sysmanage.develop.paymentrules.paymentrules",
        "method": "queryRemind",
        "content": {
            nocache: true,
            siteid: wx.getStorageSync('siteP').siteid,
            pageSize: 999
        }
    }, false).then(res => {
        console.log("版本到期情况", res)
        if (res.msg != '成功') return;
        let data = res.data;
        let content = data.map(v => {
            return `【${v.partitionname}】将于${v.enddate}日到期`
        }).join(";") + '，请尽快续费！'
        if (data.length) {
            wx.showModal({
                title: '提示',
                content,
                confirmText: "去付费",
                cancelText: isLeader ? "下次再说" : "提醒老板",
                complete: (res) => {
                    if (res.confirm) createOrder(`&vid=${data[0].sys_site_systempartitionid}`);
                    if (res.cancel && !isLeader) {
                        sendMessage();
                    } else {
                        uIndate()
                    }
                }
            })
        } else {
            if (isLeader) uIndate();
        }
    })
}
//查询人员到期情况
const uIndate = () => {
    const http = getApp().globalData.http;
    http.basic({
        "classname": "sysmanage.develop.paymentrules.paymentrules",
        "method": "queryRemindUser",
        "content": {
            siteid: wx.getStorageSync('siteP').siteid,
        }
    }).then(res => {
        console.log("人员到期情况", res)
        if (res.msg != '成功') return;
        let data = res.data;
        let content = data.map(v => v.name).join(",")
        if (data.length) wx.showModal({
            title: '提示',
            content: `${content}申请账号付费，请前往处理！`,
            confirmText: "去付费",
            cancelText: "下次再说",
            complete: (res) => {
                if (res.confirm) createOrder(`&users=${JSON.stringify(data.map(v => v.userid + ''))}`);
            }
        })
    })
}
//发送提醒付费消息发送到主账号
const sendMessage = (sys_payorderid = '') => getApp().globalData.http.basic({
    "classname": "system.payorder.payorder",
    "method": "sendMessage",
    "content": {
        sys_payorderid
    }
}).then(s => wx.showToast({
    title: '已发送消息到主体主账号',
    icon: "none"
}));
//更新付费权限
const update = (total = true) => {
    return new Promise((resolve) => {
        getApp().globalData.http.basic({
            "classname": "sysmanage.develop.userauth.userauth",
            "method": "query_userauth",
            content: {
                nocache: true
            }
        }).then(res => {
            console.log('更新付费信息', res)
            if (res.msg != '成功') return resolve(false)
            wx.setStorageSync('userauth', res.data);
            if (total) wx.showToast({
                title: '应用付费信息已更新',
                icon: "none"
            })
            try {
                let page = getCurrentPages().find(v => v.__route__ == 'pages/index/index')
                resolve(page ? page.refreshData() : getApp().globalData.refreshData())
            } catch (error) {
                resolve([])
            }
        });
    })
}
export {
    evidence,
    sendMessage,
    createOrder,
    vIndate,
    uIndate,
    update
}