require("../../../../49539051658710DF2F35F8564970FAA0.js");

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

Page({
    data: {
        tabs: [ "评委分组和权重", "待分组的评委" ],
        active: 0,
        competition: "",
        inputGroup: !1,
        juriesGroups: [],
        juriesGroup: {},
        idxJuriesGroup: -1,
        isWorker: !1,
        contactServiceView: !1,
        config_rechargeable: !1,
        user_vip: !1
    },
    onLoad: function(a) {
        var o = this;
        this.data.id = a.id, this.getCompetition(), wx.showLoading({
            title: "正在加载",
            mask: !0
        }), t.collection("_config").doc("config").field({
            rechargeable: !0
        }).get().then(function(e) {
            o.data.config_rechargeable = e.data.rechargeable;
        }).catch(function(e) {
            console.error(e);
        }), t.collection("users").where({
            _openid: e.globalData.openid,
            vipExpiryTime: i.gt(t.serverDate())
        }).count().then(function(e) {
            1 == e.total && (o.data.user_vip = !0);
        }).catch(function(e) {
            console.log(e);
        }), wx.hideShareMenu();
    },
    onChangeTabs: function(e) {
        this.setData({
            active: e.detail.index
        }), wx.pageScrollTo({
            scrollTop: 0,
            duration: 300
        });
    },
    getCompetition: function(e) {
        var i = this;
        t.collection("competitions").doc(this.data.id).field({
            juriesGroups: !0,
            workers: !0,
            juries: !0,
            rule: !0,
            rechargeable: !0,
            title: !0,
            proExpiryTime: !0
        }).get().then(function(e) {
            var o = e.data, r = new Date();
            o.proExpiryTime && (o.rechargeable = new Date(o.proExpiryTime) > r), i.data.competition = o;
            var s = o.juriesGroups ? o.juriesGroups : [];
            t.collection("users").aggregate().addFields({
                matched: a.in([ "$_openid", o.juries ])
            }).match({
                matched: !0
            }).group({
                _id: null,
                juries: a.addToSet({
                    _openid: "$_openid",
                    avatarUrl: "$avatarUrl",
                    authorized: "$authorized",
                    nickName: "$nickName",
                    realName: "$realName"
                })
            }).end().then(function(e) {
                for (var t = e.list.length >= 1 && "example" != i.data.id ? e.list[0].juries : [], a = 0; a < t.length; a++) {
                    t[a].name = t[a].realName ? t[a].realName : t[a].nickName ? t[a].nickName : "无昵称或未授权微信登录";
                    for (var o = 0; o < s.length; o++) {
                        if (s[o] && JSON.stringify(s[o].juriesDetails).indexOf(t[a]._openid) >= 0) {
                            for (var r = 0; r < s[o].juriesDetails.length; r++) {
                                if (s[o].juriesDetails[r]._openid && s[o].juriesDetails[r]._openid == t[a]._openid) {
                                    s[o].juriesDetails[r].name = t[a].name, s[o].juriesDetails[r].avatarUrl = t[a].avatarUrl;
                                    break;
                                }
                                s[o].juriesDetails[r] || (s[o].juriesDetails.splice(r, 1), r--);
                            }
                            t.splice(a, 1), a--;
                            break;
                        }
                        s[o] || (s.splice(o, 1), o--);
                    }
                }
                t.sort(function(e, t) {
                    return e.name.localeCompare(t.name);
                });
                for (var u = 0; u < s.length; u++) s[u].juriesDetails.sort(function(e, t) {
                    return e.name.localeCompare(t.name);
                });
                wx.hideLoading(), wx.stopPullDownRefresh(), i.data.juries = t, i.setData({
                    juries: t,
                    juriesGroups: s,
                    tabs: [ "评委分组和权重", 0 == t.length ? "待分组的评委" : "待分组的评委(" + t.length + ")" ]
                });
            });
        }).catch(function(e) {});
    },
    inputGroup: function() {
        (this.data.competition.rechargeable || this.data.config_rechargeable || this.data.user_vip || 0 == this.data.juriesGroups.length) && "example" != this.data.competition._id ? (wx.pageScrollTo({
            scrollTop: 0,
            duration: 300
        }), this.setData({
            inputGroup: !this.data.inputGroup,
            juriesGroup: {}
        })) : "example" == this.data.competition._id ? wx.showToast({
            title: "比赛示例不能添加分组",
            icon: "none",
            duration: 2e3
        }) : this.setData({
            contactServiceView: !0
        });
    },
    hidrInputGroup: function() {
        this.setData({
            juriesGroup: {},
            inputGroup: !1,
            idxJuriesGroup: -1
        });
    },
    clearGroup: function() {
        this.setData({
            juriesGroup: {}
        });
    },
    formSubmit: function(e) {
        if ("example" == this.data.competition._id) wx.showToast({
            title: "比赛示例不支持添加分组",
            icon: "none",
            duration: 3e3
        }); else {
            var t = {
                title: e.detail.value.title,
                weight: parseFloat(e.detail.value.weight),
                juriesDetails: [],
                topScoresExcludedTotal: parseInt(e.detail.value.topScoresExcludedTotal),
                lowestScoresExcludedTotal: parseInt(e.detail.value.lowestScoresExcludedTotal)
            };
            t.title && t.weight >= 0 && t.weight <= 1 && t.topScoresExcludedTotal >= 0 && t.lowestScoresExcludedTotal >= 0 ? (this.data.idxJuriesGroup >= 0 ? (this.data.juriesGroups[this.data.idxJuriesGroup].title = t.title, 
            this.data.juriesGroups[this.data.idxJuriesGroup].weight = t.weight, this.data.juriesGroups[this.data.idxJuriesGroup].topScoresExcludedTotal = t.topScoresExcludedTotal, 
            this.data.juriesGroups[this.data.idxJuriesGroup].lowestScoresExcludedTotal = t.lowestScoresExcludedTotal) : this.data.juriesGroups.push(t), 
            this.setData({
                juriesGroups: this.data.juriesGroups,
                inputGroup: !1,
                idxJuriesGroup: -1
            }), this.updateJuriesGroups(this.data.juriesGroups)) : !t.weight || t.weight < 0 || t.weight > 1 ? wx.showToast({
                title: "权重为0-1的小数",
                icon: "none",
                duration: 2e3
            }) : t.title ? t.topScoresExcludedTotal < 0 ? wx.showToast({
                title: "去掉最高分个数至少为0",
                icon: "none",
                duration: 2e3
            }) : t.lowestScoresExcludedTotal < 0 && wx.showToast({
                title: "去掉最低分个数至少为0",
                icon: "none",
                duration: 2e3
            }) : wx.showToast({
                title: "请输入分组名称",
                icon: "none",
                duration: 2e3
            });
        }
    },
    checkboxChange: function(e) {
        var t = e.detail.value;
        this.data.juriesChecked = t, this.setData({
            juriesChecked: t
        });
    },
    selectJuriesGroup: function(e) {
        for (var t = e.detail.value, i = this.data.juries, a = this.data.juriesChecked, o = this.data.juriesGroups, r = function(e) {
            var r = i.findIndex(function(t) {
                return t._openid == a[e];
            });
            r >= 0 && o[t].juriesDetails.push({
                _openid: i[r]._openid,
                name: i[r].name,
                avatarUrl: i[r].avatarUrl
            });
        }, s = 0; s < a.length; s++) r(s);
        for (var u = 0; u < i.length; u++) a.indexOf(i[u]._openid) >= 0 && (i.splice(u, 1), 
        u--);
        this.setData({
            juriesGroups: o,
            juries: i,
            tabs: [ "评委分组和权重", 0 == i.length ? "待分组的评委" : "待分组的评委(" + i.length + ")" ],
            juriesChecked: []
        }), this.updateJuriesGroups(o);
    },
    selectAllJuries: function(e) {
        for (var t = [], i = this.data.juries, a = 0; a < i.length; a++) t.push(i[a]._openid), 
        i[a].selected = !0;
        this.setData({
            juriesChecked: t,
            juries: i
        });
    },
    updateJuriesGroups: function(e) {
        var t = this, i = this.data.competition, a = getCurrentPages(), o = a[a.length - 2];
        o.data.competition.juriesGroups = e, o.data.updatedJuriesGroups = !0, i.juriesGroups = e, 
        wx.cloud.callFunction({
            name: "competition",
            data: {
                operation: "update",
                competition: {
                    _id: i._id,
                    juriesGroups: i.juriesGroups,
                    rule: i.rule
                }
            }
        }).then(function(e) {
            t.setData({
                inputGroup: !1
            }), wx.cloud.callFunction({
                name: "behavior",
                data: {
                    competition_id: i._id,
                    behavior: "18评委分组"
                }
            }).then(function(e) {});
        });
    },
    showActionSheet: function(i) {
        if ("example" == this.data.competition._id) wx.showToast({
            title: "比赛示例的分组不能编辑或删除",
            icon: "none",
            duration: 2e3
        }); else if (this.data.competition.workers.indexOf(e.globalData.openid) >= 0) {
            var a = i.currentTarget.dataset.juriesgroup, o = i.currentTarget.dataset.idxjuriesgroup, r = this;
            wx.showActionSheet({
                itemList: [ "编辑分组名称和权重", "删除分组和评委" ],
                itemColor: "#767676",
                success: function(e) {
                    switch (e.tapIndex) {
                      case 0:
                        wx.pageScrollTo({
                            scrollTop: 1e4,
                            duration: 300
                        }), r.setData({
                            inputGroup: !0,
                            juriesGroup: a,
                            idxJuriesGroup: o
                        });
                        break;

                      case 1:
                        wx.showModal({
                            title: "确认删除分组和评委吗？",
                            cancelColor: "#484848",
                            cancelText: "取消",
                            confirmColor: "#FF5A5F",
                            confirmText: "确定",
                            success: function(e) {
                                e.confirm && (r.data.juries = r.data.juries.concat(r.data.juriesGroups[o].juriesDetails), 
                                t.collection("juriesGroups").where({
                                    competition_id: r.data.competition._id,
                                    juriesGroupTitle: a.title
                                }).get().then(function(e) {
                                    console.log(e), 1 == e.data.length && t.collection("juriesGroups").doc(e.data[0]._id).remove().then(function(e) {
                                        console.log("juriesGroups:", e);
                                    });
                                }).catch(function(e) {
                                    console.log(e);
                                }), r.data.juriesGroups.splice(o, 1), r.setData({
                                    juriesGroups: r.data.juriesGroups,
                                    juries: r.data.juries,
                                    tabs: [ "评委分组和权重", 0 == r.data.juries.length ? "待分组的评委" : "待分组的评委(" + r.data.juries.length + ")" ]
                                }), r.updateJuriesGroups(r.data.juriesGroups));
                            },
                            fail: function(e) {
                                console.log(e);
                            }
                        });
                    }
                }
            });
        }
    },
    showActionSheetDeleteJury: function(t) {
        if ("example" == this.data.competition._id) console.log(t), wx.showToast({
            title: "比赛示例不能删除获奖者",
            icon: "none",
            duration: 2e3
        }); else if (this.data.competition.workers.indexOf(e.globalData.openid) >= 0) {
            var i = t.currentTarget.dataset.idxjury, a = t.currentTarget.dataset.idxjuriesgroup, o = 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 && (o.data.juries.push(o.data.juriesGroups[a].juriesDetails[i]), o.data.juriesGroups[a].juriesDetails.splice(i, 1), 
                                o.setData({
                                    juriesGroups: o.data.juriesGroups,
                                    buttonText: "保存设置",
                                    juries: o.data.juries,
                                    tabs: [ "评委分组和权重", 0 == o.data.juries.length ? "待分组的评委" : "待分组的评委(" + o.data.juries.length + ")" ]
                                }), o.updateJuriesGroups(o.data.juriesGroups));
                            },
                            fail: function(e) {
                                console.log(e);
                            }
                        });
                    }
                }
            });
        }
    },
    hideContactService: function(e) {
        this.setData({
            contactServiceView: !1
        });
    },
    contactService: function(e) {
        t.collection("behaviors").add({
            data: {
                createTime: t.serverDate(),
                competition_id: this.data.competition._id,
                competition_title: this.data.competition.title,
                type: "contactService",
                page: "juriesGroups"
            }
        }).then(function(e) {}).catch(console.error), this.setData({
            contactServiceView: !1
        });
    },
    onPullDownRefresh: function() {
        this.getCompetition();
    }
});