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

r.command, r.command.aggregate;

Page({
    data: {
        competition: "",
        program: "",
        juries: ""
    },
    onLoad: function(a) {
        var o = this;
        o.getOpenerEventChannel().on("acceptDataFromOpenerPage", function(a) {
            for (var t = 0; t < a.program.unjudgedJuries.length; t++) a.program.unjudgedJuries[t].name = a.program.unjudgedJuries[t].realName ? a.program.unjudgedJuries[t].realName : a.program.unjudgedJuries[t].nickName ? a.program.unjudgedJuries[t].nickName : "该用户无昵称";
            a.program.unjudgedJuries.sort(function(e, r) {
                return e.name.localeCompare(r.name);
            });
            for (var n = 0; n < a.program.scores.length; n++) a.program.scores[n].name = a.program.scores[n].realName ? a.program.scores[n].realName : a.program.scores[n].nickName ? a.program.scores[n].nickName : "无昵称或未授权微信登录";
            if (a.program.scores.sort(function(e, r) {
                return e.name.localeCompare(r.name);
            }), a.program.scores.sort(function(e, r) {
                return r.score - e.score;
            }), a.competition.settings.submitComment && a.program.comments) for (var i = function(e) {
                var r = a.juries.findIndex(function(r) {
                    return r._openid == a.program.comments[e]._openid;
                });
                r >= 0 && (a.program.comments[e].name = a.juries[r].realName ? a.juries[r].realName : a.juries[r].nickName ? a.juries[r].nickName : "无昵称或未授权微信登录", 
                a.program.comments[e].avatarUrl = a.juries[r].avatarUrl);
            }, s = 0; s < a.program.comments.length; s++) i(s);
            o.data.juries = a.juries, o.setData({
                competition: a.competition,
                program: a.program,
                calculateScoresByJuriesGroups: a.calculateScoresByJuriesGroups
            }), r.collection("_config").where({
                _id: "config"
            }).field({
                v: !0,
                adProgramDetails: !0
            }).get().then(function(r) {
                o.setData({
                    c: e.globalData.c,
                    v: a.competition.settings && !a.competition.settings.showVote ? "0.0.0" : r.data[0].v
                });
            }).catch(function(e) {
                console.error(e);
            });
        }), wx.hideShareMenu();
    },
    showActionSheet: function(r) {
        if (this.data.competition.workers.indexOf(e.globalData.openid) >= 0) if ("example" == this.data.competition._id) wx.showToast({
            title: "比赛示例的参赛者不能编辑或删除",
            icon: "none",
            duration: 2e3
        }); else {
            var a = r.currentTarget.dataset.score, o = r.currentTarget.dataset.index, t = this;
            wx.showActionSheet({
                itemList: [ "删除该评委的分数" ],
                itemColor: "#767676",
                success: function(e) {
                    switch (e.tapIndex) {
                      case 0:
                        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: t.data.program._id,
                                            competition_id: t.data.program.competition_id
                                        },
                                        juryOpenid: a._openid,
                                        operation: "deleteScore"
                                    }
                                }).then(function(e) {
                                    if (wx.hideLoading(), wx.showToast({
                                        title: "删除分数",
                                        icon: "success",
                                        duration: 1e3
                                    }), t.data.program.scores.splice(o, 1), t.data.program.unjudgedJuries.push({
                                        avatarUrl: a.avatarUrl,
                                        name: a.name
                                    }), t.data.program.comments && t.data.program.comments.length > 0) {
                                        var r = t.data.program.comments.findIndex(function(e) {
                                            return e._openid == a._openid;
                                        });
                                        r >= 0 && t.data.program.comments.splice(r, 1);
                                    }
                                    t.setData({
                                        program: t.data.program
                                    });
                                }));
                            },
                            fail: function(e) {
                                console.log(e);
                            }
                        });
                    }
                }
            });
        } else wx.showToast({
            title: "仅工作人员有权限",
            icon: "none",
            duration: 2500
        });
    }
});