var e = getApp();

Page({
    data: {
        nickName: "张三",
        sex: "男",
        userImage: "../../images/user-img0.jpg",
        money: "0",
        freeMoney: "0"
    },
    onLoad: function() {
        wx.setNavigationBarTitle({
            title: "个人中心"
        });
        var o = e.globalData.userInfo;
        null == o && wx.getUserInfo({
            success: function(a) {
                e.globalData.userInfo = a.userInfo, o = a.userInfo;
            }
        });
        var a = wx.getStorageSync("openId"), t = wx.getStorageSync("schoolName");
        console.log("schoolName:" + t), null != o && wx.request({
            url: e.url + "/applet/checkUserOrSave",
            data: {
                openId: a,
                nickName: o.nickName,
                headUrl: o.avatarUrl,
                schoolName: t
            },
            success: function(e) {}
        });
        var n = this, s = o.nickName;
        n.setData({
            nickName: s.substring(0, 4),
            sex: 1 == o.gender ? "男" : "女",
            userImage: o.avatarUrl
        });
    },
    onShow: function() {
        var o = wx.getStorageSync("openId"), a = wx.getStorageSync("schoolName"), t = this;
        wx.request({
            url: e.url + "/applet/getLeftMoney",
            data: {
                openId: o,
                schoolName: a
            },
            success: function(e) {
                if ("200" == e.data.statusCode) {
                    var o = e.data.resultObject.amount;
                    o = (o = parseFloat(o)).toFixed(2);
                    var a = e.data.resultObject.freeMoney;
                    a = (a = parseFloat(a)).toFixed(2), t.setData({
                        money: o,
                        freeMoney: a
                    });
                }
            }
        });
    },
    onPullDownRefresh: function() {
        wx.showNavigationBarLoading();
        var o = this, a = wx.getStorageSync("openId"), t = wx.getStorageSync("schoolName"), a = wx.getStorageSync("openId");
        wx.request({
            url: e.url + "/applet/getLeftMoney",
            data: {
                openId: a,
                schoolName: t
            },
            success: function(e) {
                if (console.log(e), "200" == e.data.statusCode) {
                    var a = e.data.resultObject.amount;
                    a = (a = parseFloat(a)).toFixed(2);
                    var t = e.data.resultObject.freeMoney;
                    t = (t = parseFloat(t)).toFixed(2), o.setData({
                        money: a,
                        freeMoney: t
                    });
                }
                wx.hideNavigationBarLoading(), wx.stopPullDownRefresh();
            }
        });
    },
    hideMode: function() {
        console.log("获取设备id"), wx.getSystemInfo({
            success: function(e) {
                console.log(e.platform), "android" == e.platform && wx.navigateTo({
                    url: "../discoveryDeviceId/discoveryDeviceId"
                });
            }
        });
    },
    pay: function() {
        console.log("充值界面"), wx.navigateTo({
            url: "../pay/pay"
        });
    },
    consume: function() {
        console.log("消费界面"), wx.navigateTo({
            url: "../consume/consume"
        });
    },
    payInfo: function() {
        console.log("充值明细"), wx.navigateTo({
            url: "../payInfo/payInfo"
        });
    },
    maintain: function() {
        wx.navigateTo({
            url: "../maintain/maintain"
        });
    },
    info: function() {
        wx.navigateTo({
            url: "../instruction/instruction"
        });
    },
    tel: function() {
        wx.showModal({
            title: "提示",
            content: "客服电话：1340000",
            success: function(e) {
                e.confirm ? (console.log("确认拨打"), wx.makePhoneCall({
                    phoneNumber: "1340000"
                })) : e.cancel && console.log("点击取消");
            }
        });
    },
    backMoney: function() {
        console.log("退费");
        var o = this;
        if (console.log(o.data.money), o.data.money > 0) {
            var a = wx.getStorageSync("openId"), t = wx.getStorageSync("schoolName");
            wx.request({
                url: e.url + "/applet/backMoney",
                data: {
                    openId: a,
                    money: o.data.money,
                    schoolName: t
                },
                success: function(e) {
                    console.log(e), "200" == e.data.statusCode && (console.log(e.data.resultObject), 
                    o.setData({
                        money: e.data.resultObject
                    }));
                }
            });
        }
    }
});