var e = require("../../../49539051658710DF2F35F8564970FAA0.js"), t = getApp(), o = wx.cloud.database(), a = (o.command, 
o.command.aggregate);

Page({
    data: {
        tabs: [ "待评分", "已评分" ],
        active: 0,
        navigateToPage: "",
        userInfo: "",
        competition: "",
        programs: [],
        loadmore: "",
        hideJudgedPrograms: !1,
        submitedScore: !1,
        orderBy: "index"
    },
    onLoad: function(e) {
        var a = this;
        wx.hideShareMenu(), this.data.id = e.id, "oRnCI5JYWT9dmVycOnBjYaX1Yx7k" != t.globalData.openid && o.collection("behaviors").add({
            data: {
                createTime: o.serverDate(),
                competition_id: e.id,
                type: "visit",
                role: "jury",
                page: "judge"
            }
        }), wx.showLoading({
            title: "正在加载",
            mask: !0
        }), o.collection("users").where({
            _openid: t.globalData.openid
        }).field({
            _id: !0,
            realName: !0,
            authorized: !0
        }).get().then(function(o) {
            a.setData({
                userInfo: o.data[0],
                userOpenid: t.globalData.openid
            }), t.globalData.authorized = o.data[0].authorized, a.getCompetition(e);
        }).catch(function(e) {
            console.log(e);
        });
    },
    onShow: function(e) {
        if ("submitScore" == this.data.navigateToPage && (this.getPrograms(), this.setData({
            submitedScore: this.data.submitedScore
        }), this.data.navigateToPage = "", this.data.competition.rule.scoringItems)) for (var t = 0; t < this.data.competition.rule.scoringItems.length; t++) this.data.competition.rule.scoringItems[t].oldScore = "";
    },
    onChangeTabs: function(e) {
        this.setData({
            active: e.detail.index,
            programs: this.data.programs
        });
    },
    editRealName: function(e) {
        var t = this.data.userInfo;
        t.realName = "", this.setData({
            userInfo: t
        });
    },
    bindinputRealName: function(e) {
        this.data.inputRealName = e.detail.value;
    },
    inputRealName: function(e) {
        var t = this;
        wx.showLoading({
            title: "正在提交",
            mask: !0
        });
        var a = this.data.inputRealName, i = this.data.userInfo;
        a ? wx.cloud.callFunction({
            name: "textSecCheck",
            data: {
                text: JSON.stringify(a)
            }
        }).then(function(e) {
            87014 == e.result.errCode ? (wx.hideLoading(), wx.showModal({
                content: "系统检测所提交的文本含有违规用词，请修改后再提交",
                showCancel: !1,
                confirmColor: "#FF5A5F",
                confirmText: "我知道了",
                success: function(e) {},
                fail: function(e) {
                    console.log(e);
                }
            })) : o.collection("users").doc(i._id).update({
                data: {
                    realName: a
                }
            }).then(function(e) {
                wx.showToast({
                    title: "已提交",
                    icon: "success",
                    duration: 1500
                }), i.realName = a, t.setData({
                    userInfo: i
                });
            }).catch(function(e) {
                console.log(e);
            });
        }) : a || wx.showToast({
            title: "请输入姓名后提交",
            icon: "none",
            duration: 2e3
        });
    },
    getCompetition: function(t) {
        var a = this;
        o.collection("competitions").doc(this.data.id).field({
            title: !0,
            poster: !0,
            rule: !0,
            startTime: !0,
            juries: !0,
            site: !0,
            sponsor: !0,
            organizer: !0,
            tags: !0,
            status: !0,
            workers: !0,
            rechargeable: !0,
            settings: !0,
            posterSize: !0
        }).get().then(function(t) {
            var o = t.data;
            "已删除" == o.status || "已关闭" == o.status ? (wx.hideLoading(), wx.showModal({
                content: "主办方" + o.status + "比赛，或联系工作人员",
                showCancel: !1,
                confirmColor: "#FF5A5F",
                confirmText: "我知道了",
                success: function(e) {
                    wx.reLaunch({
                        url: "/pages/index/index"
                    });
                },
                fail: function(e) {
                    console.log(e);
                }
            })) : (o.startTime = e.formatTime("short", o.startTime), a.setData({
                competition: o
            }), a.getPrograms());
        }).catch(function(t) {
            e.competitionNoExistShowModal(t.errCode);
        });
    },
    getPrograms: function() {
        var e = this;
        o.collection("programs").aggregate().match({
            competition_id: this.data.id
        }).group({
            _id: null,
            programs: a.addToSet({
                _id: "$_id",
                competition_id: "$competition_id",
                index: "$index",
                note: "$note",
                scores: "$scores",
                title: "$title",
                judging: "$judging",
                image: "$image",
                averageScore: "$averageScore",
                comments: "$comments",
                skipJuries: "$skipJuries"
            })
        }).end().then(function(o) {
            var a = o.list.length >= 1 ? o.list[0].programs : [];
            a.sort(function(e, t) {
                return e.index - t.index;
            }), wx.hideLoading(), wx.stopPullDownRefresh();
            for (var i = 0; i < a.length; i++) {
                if (a[i].judged = !1, a[i].judgedScore = 0, a[i].scores && a[i].scores.length >= 1) for (var r = 0; r < a[i].scores.length; r++) a[i].scores[r]._openid == t.globalData.openid && (a[i].judged = !0, 
                a[i].hideJudgedProgram = e.data.hideJudgedPrograms, a[i].judgedScore = a[i].scores[r].score, 
                e.data.submitedScore = !0);
                if (a[i].comments && a[i].comments.length >= 1) for (var s = 0; s < a[i].comments.length; s++) a[i].comments[s]._openid == t.globalData.openid && (a[i].comment = a[i].comments[s].comment);
                e.data.hideJudgedPrograms && !a[i].judging && (a[i].hideJudgedProgram = !0), a[i].skipJuries && a[i].skipJuries.indexOf(t.globalData.openid) >= 0 ? a[i].skipJudge = !0 : a[i].skipJudge = !1;
            }
            0 == a.length ? e.setData({
                loadmore: "主办方暂未添加参赛者"
            }) : e.setData({
                programs: a,
                loadmore: "全部加载",
                hideJudgedPrograms: e.data.hideJudgedPrograms
            });
        }).catch(function(e) {
            console.error(e);
        });
    },
    navSubmitScorePage: function(e) {
        var o = this.data.competition, a = this.data.userInfo;
        if (t.globalData.authorized || o.settings && o.settings.unAuthorizeJury) if (!o.rule.realName || o.rule.realName && a.realName) {
            var i = e.currentTarget.dataset.program;
            o.settings && o.settings.submitScoreV2 ? wx.navigateTo({
                url: "/pages/competition/judge/submitScoreV2/submitScoreV2",
                success: function(e) {
                    e.eventChannel.emit("acceptDataFromOpenerPage", {
                        program: i,
                        competition: o
                    });
                }
            }) : wx.navigateTo({
                url: "/pages/competition/judge/submitScore/submitScore",
                success: function(e) {
                    e.eventChannel.emit("acceptDataFromOpenerPage", {
                        program: i,
                        competition: o
                    });
                }
            });
        } else wx.showModal({
            content: "主办方已开启实名制评分，请先输入姓名再开始评分",
            cancelColor: "#484848",
            cancelText: "取消",
            confirmColor: "#FF5A5F",
            confirmText: "好的",
            success: function(e) {}
        }); else wx.showModal({
            content: "授权微信登录后方可进行评分",
            cancelColor: "#484848",
            cancelText: "以后再说",
            confirmColor: "#FF5A5F",
            confirmText: "去登录",
            success: function(e) {
                e.confirm && wx.navigateTo({
                    url: "/pages/more/authorize/authorize?page=judge"
                });
            }
        });
    },
    editScore: function(e) {
        for (var o = this.data.programs, a = this.data.competition, i = e.currentTarget.dataset.program, r = e.currentTarget.dataset.idx, s = 0; s < o[r].scores.length; s++) if (o[r].scores[s]._openid == t.globalData.openid) {
            for (var n = 0; n < a.rule.scoringItems.length; n++) a.rule.scoringItems[n].oldScore = o[r].scores[s].scoresDetails[n];
            break;
        }
        a.settings && a.settings.submitScoreV2 ? wx.navigateTo({
            url: "/pages/competition/judge/submitScoreV2/submitScoreV2",
            success: function(e) {
                e.eventChannel.emit("acceptDataFromOpenerPage", {
                    program: i,
                    competition: a
                });
            }
        }) : wx.navigateTo({
            url: "/pages/competition/judge/submitScore/submitScore",
            success: function(e) {
                e.eventChannel.emit("acceptDataFromOpenerPage", {
                    program: i,
                    competition: a
                });
            }
        });
    },
    deleteScore: function(e) {
        var o = this.data.competition, a = e.currentTarget.dataset.program, i = this;
        o.settings && o.settings.submitScoreV2 && wx.showModal({
            title: "删除该选手的已评分数吗？",
            cancelColor: "#484848",
            cancelText: "取消",
            confirmColor: "#FF5A5F",
            confirmText: "确定",
            success: function(e) {
                e.confirm && (wx.showLoading({
                    title: "正在删除",
                    mask: !0
                }), wx.cloud.callFunction({
                    name: "program",
                    data: {
                        program: {
                            _id: a._id,
                            competition_id: o._id
                        },
                        juryOpenid: t.globalData.openid,
                        operation: "deleteScore"
                    }
                }).then(function(e) {
                    wx.hideLoading(), wx.showToast({
                        title: "删除分数",
                        icon: "success",
                        duration: 1e3
                    }), i.getPrograms();
                }));
            }
        });
    },
    refresh: function() {
        wx.showLoading({
            title: "正在刷新",
            mask: !0
        }), this.data.programs = [];
        this.getCompetition({});
    },
    onPullDownRefresh: function() {
        this.data.programs = [];
        this.getCompetition({});
    },
    showAllPrograms: function() {
        wx.showLoading({
            title: "正在刷新",
            mask: !0
        }), this.data.hideJudgedPrograms = !1, this.data.programs = [], this.getPrograms();
    },
    hideJudgedPrograms: function() {
        wx.showLoading({
            title: "正在刷新",
            mask: !0
        }), this.data.hideJudgedPrograms = !0, this.data.programs = [], this.getPrograms();
    },
    changeOrderBy: function(e) {
        wx.showLoading({
            title: "切换排序",
            mask: !0
        });
        var t = this.data.programs;
        switch (this.data.orderBy) {
          case "index":
            this.data.orderBy = "judgedScore";
            for (var o = !1, a = 0; a < t.length; a++) {
                t[a].judged && (o = !0);
                break;
            }
            o ? (t.sort(function(e, t) {
                return t.judgedScore - e.judgedScore;
            }), wx.hideLoading(), wx.showToast({
                title: "已切换",
                icon: "success",
                duration: 750
            }), this.setData({
                programs: t,
                orderBy: this.data.orderBy
            })) : (this.data.orderBy = "index", wx.showToast({
                title: "您尚未提交分数",
                icon: "none",
                duration: 1500
            }));
            break;

          case "judgedScore":
            this.data.orderBy = "index", t.sort(function(e, t) {
                return e.index - t.index;
            }), wx.hideLoading(), wx.showToast({
                title: "已切换",
                icon: "success",
                duration: 750
            }), this.setData({
                programs: t,
                orderBy: this.data.orderBy
            });
        }
    },
    previewImage: function(e) {
        console.log(e);
        var t = e.currentTarget.dataset.image;
        wx.previewImage({
            current: t,
            urls: [ t ]
        });
    }
});