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

e.command.aggregate;

Page({
    data: {
        minHeight: 1600,
        notice: "",
        isShowTip: !1,
        shortcut: "",
        menu: [ {
            title: "组织比赛",
            url: "/pages/worker/worker",
            desc: "邀请评委&实时成绩",
            imageSrc: "/images/index/worker.svg"
        }, {
            title: "我的评分",
            url: "/pages/jury/jury",
            desc: "微信扫邀请码后评分",
            imageSrc: "/images/index/jury.svg"
        }, {
            title: "更多",
            url: "/pages/more/more",
            desc: "使用指南&联系客服",
            imageSrc: "/images/index/more.svg"
        } ]
    },
    onLoad: function(a) {
        var i = this, o = new Date();
        this.setData({
            c: t.globalData.c,
            platform: "windows" == t.globalData.platform || "mac" == t.globalData.platform ? "pc" : "",
            currentYear: o.getFullYear()
        }), e.collection("_config").where({
            _id: "config"
        }).field({
            notice: !0,
            hideAudienceColumn: !0
        }).get().then(function(t) {
            t.data[0].notice != wx.getStorageSync("notice") && (i.setData({
                notice: t.data[0].notice
            }), t.data[0].notice || wx.removeStorageSync("notice")), t.data[0].hideAudienceColumn || (i.data.menu.splice(2, 0, {
                title: "比赛精选",
                url: "/pages/audience/audience",
                desc: "查看成绩&加油打Call",
                imageSrc: "/images/index/audience.svg"
            }), i.setData({
                menu: i.data.menu
            }));
        }).catch(function(t) {
            console.error(t);
        }), wx.getStorageSync("isShowTip") || this.setData({
            isShowTip: !0
        }), this.getShortcut();
    },
    onShow: function() {
        if (this.data.shortcut.startTime) {
            var t = this.data.shortcut, e = new Date(), a = new Date(t.startTime.getTime() + 54e5);
            ((t.statusNote = t.startTime <= e) || (t.statusNote = a < e)) && this.getShortcut();
        }
    },
    hideMaskStatus: function(t) {
        this.setData({
            maskStatus: !1
        });
    },
    closeNotice: function(t) {
        var e = this;
        wx.getSystemInfo({
            success: function(a) {
                var i = a.screenWidth / 375;
                t.detail.x / i >= 340 && wx.setStorageSync("notice", e.data.notice);
            }
        });
    },
    hideTip: function() {
        this.setData({
            isShowTip: !1
        }), wx.setStorageSync("isShowTip", !0);
    },
    getShortcut: function() {
        var i = this, o = {
            title: "",
            status: "",
            statusNote: "",
            url: "",
            startTime: ""
        };
        wx.cloud.callFunction({
            name: "getOpenid",
            data: {
                page: "app"
            }
        }).then(function(n) {
            t.globalData.openid = n.result, e.collection("competitions").where({
                status: a.and(a.neq("已删除"), a.neq("已关闭")),
                startTime: a.and(a.gt(e.serverDate({
                    offset: -288e5
                })), a.lt(e.serverDate({
                    offset: 864e5
                })))
            }).where(a.or([ {
                juries: t.globalData.openid
            }, {
                workers: t.globalData.openid
            }, {
                audiences: t.globalData.openid
            } ])).orderBy("startTime", "desc").limit(1).field({
                title: !0,
                startTime: !0,
                juries: !0,
                workers: !0,
                status: !0
            }).get().then(function(e) {
                if (wx.hideLoading(), wx.stopPullDownRefresh(), e.data.length >= 1 && "example" != e.data[0]._id) {
                    var a = e.data[0];
                    o.title = a.title, o.startTime = a.startTime, o.status = a.status, a.workers && a.workers.length >= 1 && a.workers.indexOf(t.globalData.openid) >= 0 ? o.url = "/pages/competition/competition?id=" + a._id : a.juries && a.juries.length >= 1 && a.juries.indexOf(t.globalData.openid) >= 0 ? o.url = "/pages/competition/judge/judge?id=" + a._id : o.url = "/pages/competition/vote/vote?id=" + a._id, 
                    i.setData({
                        shortcut: o
                    });
                } else i.setData({
                    shortcut: {
                        title: "",
                        status: "",
                        statusNote: "",
                        url: "",
                        startTime: ""
                    }
                });
            }).catch(function(t) {
                console.error(t);
            });
        });
    },
    navigateToDashboard: function(e) {
        "oRnCI5JYWT9dmVycOnBjYaX1Yx7k" != t.globalData.openid && "oRnCI5GgJ8Kc3sGye-ungAdL7r4U" != t.globalData.openid || wx.navigateTo({
            url: "/pages/dashboard/dashboard"
        });
    },
    navigateToActive: function(e) {
        "oRnCI5JYWT9dmVycOnBjYaX1Yx7k" != t.globalData.openid && "oRnCI5GgJ8Kc3sGye-ungAdL7r4U" != t.globalData.openid || wx.navigateTo({
            url: "/pages/dashboard/active/active"
        });
    },
    onPullDownRefresh: function() {
        this.getShortcut();
    },
    Addrecord: function() {
        "oRnCI5JYWT9dmVycOnBjYaX1Yx7k" != t.globalData.openid && e.collection("behaviors").add({
            data: {
                createTime: e.serverDate(),
                type: "shortcut",
                page: "index"
            }
        });
    },
    onShareAppMessage: function() {
        return {
            title: "我发现了一款高效好用的比赛评分小程序",
            path: "/pages/index/index",
            imageUrl: "/images/sharePic.png"
        };
    }
});