var t = getApp(), e = require("../../utils/login.js");

Page({
    data: {
        showLoding: !0,
        list: [],
        user: {},
        page: {
            next_page: 1
        },
        servicetel: "",
        edition: 1,
        selectItemID: null,
        loading: !1,
        provision: {},
        tips: [],
        showTips: !1,
        approve: !0
    },
    onLoad: function (a) {
        var i = this;
        e.pageStart(t).then(function (e) {
            i.getList(), i.setData({
                servicetel: t.globalData.servicetel
            });
        });
    },
    onShow: function (e) {
        t.globalData.isneedUpload && (this.getList(), t.globalData.isneedUpload = !1);
    },
    onPullDownRefresh: function () {
        this.setData({
            list: []
        }), this.getList();
    },
    onReachBottom: function () {
        this.getList();
    },
    onShareAppMessage: function () { },
    getList: function () {
        var a = this;
        if (this.data.loading) return !1;
        this.setData({
            loading: !0
        }), wx.showLoading({
            title: ""
        }), e.request(t.globalData.config.recharge.init, {
            page: this.data.page.next_page
        }, !0).then(function (t) {
            if (wx.hideLoading(), a.setData({
                loading: !1
            }), 200 != t.code) return wx.showModal({
                title: "提示",
                content: t.message
            }), !1;
            a.setData({
                showLoding: !1,
                list: t.data.list,
                user: t.data.user,
                protocol: t.data.protocol,
                tips: t.data.tips.split("\n")
            }, function () { });
        }).catch(function (t) {
            wx.hideLoading();
        });
    },
    tobuy: function (a) {
        var i = a.currentTarget.dataset.id;
        wx.showLoading({
            title: ""
        }), e.request(t.globalData.config.recharge.create, {
            id: i
        }, !0).then(function (t) {
            if (wx.hideLoading(), 200 != t.code) return wx.showModal({
                title: "提示",
                content: t.message
            }), !1;
            var e = t.data.result;

            console.log('tobuy', e, t);

            wx.requestPayment({
                timeStamp: e.timeStamp,
                nonceStr: e.nonceStr,
                package: e.package,
                signType: e.signType,
                paySign: e.paySign,
                success: function (t) {

                    // 加载回调接口
                    e.request(t.data.notify_url, {
                        orderid: t.data.orderid
                    }, !0).then(function (t) {
                        console.log('pay_success', t);
                    }).catch(function (t) {
                        console.log('pay_success', t);
                    });

                    getCurrentPages().length > 1 ? wx.navigateBack({}) : wx.redirectTo({
                        url: "/pages/index/index"
                    });
                }
            });
        }).catch(function (t) {
            wx.hideLoading();
        });
    },
    selectItem: function (t) {
        var e = t.currentTarget.dataset.id;
        this.setData({
            selectItemID: e
        });
    },
    isSubmit: !1,
    tobuyNow: function (a) {
        if (!this.data.approve) return wx.showToast({
            title: "请勾选同意相关条款",
            icon: "none"
        }), !1;
        if (!this.data.selectItemID || this.isSubmit) return wx.showToast({
            title: "请选择充值金额~",
            icon: "none"
        }), !1;
        this.isSubmit = !0, wx.showLoading({
            title: ""
        });
        var i = this;
        e.request(t.globalData.config.recharge.create, {
            id: i.data.selectItemID
        }, !0).then(function (t) {
            if (wx.hideLoading(), i.isSubmit = !1, 200 != t.code) return wx.showModal({
                title: "提示",
                content: t.message
            }), !1;
            var r = t.data.result;
            wx.requestPayment({
                timeStamp: r.timeStamp,
                nonceStr: r.nonceStr,
                package: r.package,
                signType: r.signType,
                paySign: r.paySign,
                success: function (o) {

                    if (t.data.notify_url) {
                        // 加载回调接口
                        e.request(t.data.notify_url, {
                            orderid: t.data.orderid
                        }, !0).then(function (t) {
                            console.log('pay_success', t);
                        }).catch(function (t) {
                            console.log('pay_success', t);
                        });
                    }

                    getCurrentPages().length > 1 ? wx.navigateBack({}) : wx.redirectTo({
                        url: "/pages/index/index"
                    });
                }
            });
        }).catch(function (t) {
            wx.hideLoading(), i.isSubmit = !1;
        });
    },
    showprovision: function () {
        this.setData({
            showTips: !this.data.showTips
        });
    },
    change: function (t) {
        var e = t.detail.value.length;
        this.setData({
            approve: 1 == e
        });
    }
});