var e = getApp(), t = wx.cloud.database();

Page({
    data: {
        config_rechargeable: "",
        config_hideAudienceColumn: ""
    },
    onLoad: function(e) {
        var a = this;
        this.setData({
            dashboard: wx.getStorageSync("dashboard")
        }), t.collection("_config").doc("config").field({
            rechargeable: !0,
            hideAudienceColumn: !0
        }).get().then(function(e) {
            a.data.config_rechargeable = e.data.rechargeable, a.data.config_hideAudienceColumn = e.data.hideAudienceColumn;
        }).catch(function(e) {
            console.error(e);
        }), wx.showLoading({
            title: "正在刷新",
            mask: !0
        }), this.getActive();
    },
    getActive: function() {
        var e = this;
        wx.cloud.callFunction({
            name: "getActive"
        }).then(function(t) {
            wx.hideLoading(), wx.stopPullDownRefresh();
            var a = new Date(), i = {
                competitionsTotal: 0,
                newCompetitionsTotal: 0,
                vipTotal: 0,
                proTotal: 0,
                usersVisitTotal: 0,
                workerVisitTotal: 0,
                juryVisitTotal: 0,
                audienceVisitTotal: 0
            }, o = t.result.competitions;
            i.usersVisitTotal = t.result.usersVisitTotal;
            for (var n = new Date(a.getFullYear(), a.getMonth(), a.getDate(), 0, 0, 0), c = [], s = 0; s < o.length; s++) i.competitionsTotal++, 
            new Date(o[s].createTime).getTime() >= new Date(n).getTime() && i.newCompetitionsTotal++, 
            o[s].proExpiryTime && "example" != o[s]._id && i.proTotal++, o[s].vipExpiryTime && -1 == c.indexOf(o[s].creator) && "oRnCI5JYWT9dmVycOnBjYaX1Yx7k" != o[s].creator && c.push(o[s].creator), 
            i.workerVisitTotal = i.workerVisitTotal + o[s].workerVisitTotal, i.juryVisitTotal = i.juryVisitTotal + o[s].juryVisitTotal, 
            i.audienceVisitTotal = i.audienceVisitTotal + o[s].audienceVisitTotal;
            i.vipTotal = c.length, wx.hideLoading(), wx.stopPullDownRefresh(), e.setData({
                dashboard: i
            }), wx.setStorageSync("dashboard", i);
        });
    },
    showActionSheet: function(t) {
        if ("oRnCI5JYWT9dmVycOnBjYaX1Yx7k" == e.globalData.openid || "oRnCI5GgJ8Kc3sGye-ungAdL7r4U" == e.globalData.openid) {
            var a = this, i = a.data.config_rechargeable, o = a.data.config_hideAudienceColumn, n = [];
            n.push("hideAColumn_" + o), n.push("rech_" + i), wx.showActionSheet({
                itemList: n,
                success: function(e) {
                    switch (wx.showLoading({
                        title: "正在提交",
                        mask: !0
                    }), e.tapIndex) {
                      case 0:
                        wx.cloud.callFunction({
                            name: "updateConfig",
                            data: {
                                config: {
                                    hideAudienceColumn: !o
                                }
                            }
                        }).then(function(e) {
                            wx.hideLoading(), wx.showToast({
                                title: "rech_" + !o,
                                icon: "success",
                                duration: 1500
                            }), a.data.config_hideAudienceColumn = !o;
                        });
                        break;

                      case 1:
                        wx.cloud.callFunction({
                            name: "updateConfig",
                            data: {
                                config: {
                                    rechargeable: !i
                                }
                            }
                        }).then(function(e) {
                            wx.hideLoading(), wx.showToast({
                                title: "rech_" + !i,
                                icon: "success",
                                duration: 1500
                            }), a.data.config_rechargeable = !i;
                        });
                    }
                },
                fail: function(e) {}
            });
        }
    },
    onPullDownRefresh: function() {
        this.getActive();
    },
    onShareAppMessage: function() {
        return {
            title: "Dashboard",
            path: "/pages/dashboard/dashboard",
            imageUrl: "/images/sharePic.png"
        };
    }
});