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

Page({
    data: {
        competition: "",
        inputScoringItem: !1,
        buttonText: "",
        contactServiceView: !1,
        config_rechargeable: !1,
        user_vip: !1,
        idxEditedScoringItem: -1,
        updatedJuriesGroups: !1
    },
    onLoad: function(t) {
        var a = this;
        this.data.id = t.id, wx.showLoading({
            title: "正在加载",
            mask: !0
        }), this.getCompetition(t.id), wx.hideShareMenu(), i.collection("_config").doc("config").field({
            rechargeable: !0
        }).get().then(function(t) {
            a.data.config_rechargeable = t.data.rechargeable;
        }).catch(function(t) {
            console.error(t);
        }), i.collection("users").where({
            _openid: e.globalData.openid,
            vipExpiryTime: o.gt(i.serverDate())
        }).count().then(function(t) {
            1 == t.total && (a.data.user_vip = !0);
        }).catch(function(t) {
            console.log(t);
        });
        this.setData({
            weightExamples: [ {
                title: "评分项计算示例一",
                scoringItems: [ {
                    title: "评分项1",
                    fullScore: 30,
                    weight: 1
                }, {
                    title: "评分项2",
                    fullScore: 70,
                    weight: 1
                } ],
                note: "每位参赛者满分100分，某评委提交时分别输入25和60，则该评委提交的分数为25*1+60*1=85分"
            }, {
                title: "评分项计算示例二",
                scoringItems: [ {
                    title: "评分项1",
                    fullScore: 100,
                    weight: .3
                }, {
                    title: "评分项2",
                    fullScore: 100,
                    weight: .7
                } ],
                note: "每位参赛者满分100分，某评委提交时分别输入80和90，则该评委提交的分数为80*0.3+90*0.7=87分"
            }, {
                title: "评分项计算示例三",
                scoringItems: [ {
                    title: "第1轮或初赛节目",
                    fullScore: 100,
                    weight: .4
                }, {
                    title: "第2轮或决赛节目",
                    fullScore: 100,
                    weight: .6
                } ],
                note: "比赛分为两轮或者初赛和决赛，每位参赛者满分100分，某评委提交时分别输入80和90，则该评委提交的分数为80*0.4+90*0.6=86分"
            } ]
        });
    },
    onShow: function() {
        this.data.updatedJuriesGroups && (this.setData({
            competition: this.data.competition
        }), this.data.updatedJuriesGroups = !1);
    },
    getCompetition: function(e) {
        var o = this;
        i.collection("competitions").doc(e).field({
            title: !0,
            rule: !0,
            workers: !0,
            rechargeable: !0,
            juriesGroups: !0,
            proExpiryTime: !0
        }).get().then(function(t) {
            wx.hideLoading(), wx.stopPullDownRefresh();
            var e = t.data, i = new Date();
            e.proExpiryTime && (e.rechargeable = new Date(e.proExpiryTime) > i), e.rule.scoringItems || (e.rule.scoringItems = []), 
            o.setData({
                competition: e
            });
        }).catch(function(e) {
            t.competitionNoExistShowModal(e.errCode);
        });
    },
    formSubmit: function(t) {},
    formReset: function() {},
    inputFullScore: function(t) {
        this.data.competition.rule.scoringItems && this.data.competition.rule.scoringItems.length > 0 ? wx.showToast({
            title: "满分值根据评分项系统自动计算",
            icon: "none",
            duration: 3e3
        }) : (this.data.competition.rule.fullScore = t.detail.value, this.setData({
            buttonText: "保存设置"
        }));
    },
    inputLowestScore: function(t) {
        var e = t.detail.value;
        e >= 0 ? (this.data.competition.rule.lowestScore = e, this.setData({
            buttonText: "保存设置",
            competition: this.data.competition
        })) : isNaN(e) || wx.showToast({
            title: "最低分须大于等于0",
            icon: "none",
            duration: 2e3
        });
    },
    inputDecimalPlaces: function(t) {
        var e = parseInt(t.detail.value);
        e >= 0 && e <= 5 ? (this.data.competition.rule.decimalPlaces = e, this.setData({
            buttonText: "保存设置"
        })) : isNaN(e) || wx.showToast({
            title: "小数点位数范围0-5位",
            icon: "none",
            duration: 3e3
        });
    },
    inputTopScoresExcludedTotal: function(t) {
        var e = parseInt(t.detail.value);
        e >= 0 ? (this.data.competition.rule.topScoresExcludedTotal = e, this.setData({
            buttonText: "保存设置"
        })) : isNaN(e) || wx.showToast({
            title: "个数至少为0(为0时不去掉最高分)",
            icon: "none",
            duration: 3e3
        });
    },
    inputLowestScoresExcludedTotal: function(t) {
        var e = parseInt(t.detail.value);
        e >= 0 ? (this.data.competition.rule.lowestScoresExcludedTotal = e, this.setData({
            buttonText: "保存设置"
        })) : isNaN(e) || wx.showToast({
            title: "个数至少为0(为0时不去掉最低分)",
            icon: "none",
            duration: 3e3
        });
    },
    switchChangeRealName: function(t) {
        var e = this;
        i.collection("programs").aggregate().match({
            competition_id: this.data.competition._id
        }).group({
            _id: "$competition_id",
            scoresTotal: a.sum(a.size("$scores"))
        }).project({
            _id: 1,
            scoresTotal: 1
        }).end().then(function(i) {
            (i.list.length >= 1 ? i.list[0].scoresTotal : 0) > 0 && !e.data.competition.rule.realName ? (e.data.competition.rule.realName = !1, 
            e.setData({
                competition: e.data.competition
            }), wx.showModal({
                title: "系统提示",
                content: "匿名评过分后不能开启实名制，须先删除所有参赛者的分数，操作：查看实时成绩-更多操作-删除所有已评分数",
                cancelColor: "#484848",
                cancelText: "取消",
                confirmColor: "#FF5A5F",
                confirmText: "知道了",
                success: function(t) {}
            })) : e.data.competition.rechargeable || e.data.config_rechargeable || e.data.user_vip ? (e.data.competition.rule.realName = t.detail.value, 
            e.setData({
                buttonText: "保存设置"
            })) : e.setData({
                contactServiceView: !0,
                competition: e.data.competition
            });
        });
    },
    switchChangeAnonymousProgramInfo: function(t) {
        this.data.competition.rechargeable || this.data.config_rechargeable || this.data.user_vip ? (this.data.competition.rule.anonymousProgramInfo = t.detail.value, 
        this.setData({
            buttonText: "保存设置"
        })) : this.setData({
            contactServiceView: !0,
            competition: this.data.competition
        });
    },
    switchChangeAllowJuryEditScore: function(t) {
        this.data.competition.rule.allowJuryEditScore = t.detail.value, this.setData({
            buttonText: "保存设置"
        });
    },
    inputScoringRubic: function(t) {
        this.data.competition.rule.scoringRubic = t.detail.value, this.setData({
            buttonText: "保存设置"
        });
    },
    inputScoringItem: function(t) {
        this.data.competition.rechargeable || this.data.competition.rule.scoringItems.length < 2 || this.data.config_rechargeable || this.data.user_vip ? this.setData({
            inputScoringItem: !this.data.inputScoringItem
        }) : this.setData({
            contactServiceView: !0
        });
    },
    hideInputScoringItem: function(t) {
        this.setData({
            scoringItem: {},
            inputScoringItem: !1,
            idxEditedScoringItem: -1
        });
    },
    clearScoringItem: function(t) {
        this.setData({
            scoringItem: {}
        });
    },
    inputScoringItemFormSubmit: function(t) {
        if ("example" == this.data.competition._id) wx.showToast({
            title: "比赛示例不支持添加评分项",
            icon: "none",
            duration: 3e3
        }); else {
            var e = {
                title: t.detail.value.title,
                fullScore: parseFloat(t.detail.value.fullScore),
                weight: parseFloat(t.detail.value.weight)
            };
            if (e.title && e.fullScore > 0 && e.weight >= 0 && e.weight <= 1) {
                this.data.idxEditedScoringItem >= 0 ? this.data.competition.rule.scoringItems[this.data.idxEditedScoringItem] = e : this.data.competition.rule.scoringItems.push(e);
                for (var i = this.data.competition.rule.scoringItems, o = 0, a = 0; a < i.length; a++) o += i[a].fullScore * i[a].weight;
                this.data.competition.rule.fullScore = o, this.setData({
                    competition: this.data.competition,
                    scoringItem: {},
                    inputScoringItem: !1,
                    idxEditedScoringItem: -1,
                    buttonText: "保存设置"
                }), wx.showToast({
                    title: "记得保存设置哦",
                    icon: "none",
                    duration: 1500
                });
            } else e.title ? e.fullScore ? e.weight ? e.fullScore < 0 ? wx.showToast({
                title: "评分项满分值应该大于0",
                icon: "none",
                duration: 2500
            }) : (e.weight < 0 || e.weight > 1) && wx.showToast({
                title: "评分项权重为0-1的数",
                icon: "none",
                duration: 2500
            }) : wx.showToast({
                title: "输入评分项权重",
                icon: "none",
                duration: 2e3
            }) : wx.showToast({
                title: "输入评分项满分值",
                icon: "none",
                duration: 2e3
            }) : wx.showToast({
                title: "输入评分项名称",
                icon: "none",
                duration: 2e3
            });
        }
    },
    deleteScoringItem: function(t) {
        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 i = t.currentTarget.dataset.index, o = this;
            wx.showActionSheet({
                itemList: [ "编辑", "删除" ],
                itemColor: "#767676",
                success: function(t) {
                    switch (t.tapIndex) {
                      case 0:
                        wx.showModal({
                            content: "比赛开始后更改评分项的满分值或权重，评委已提交分数无法同步更新，需在查看实时成绩中删除分数后评委重新提交。",
                            cancelColor: "#484848",
                            cancelText: "取消",
                            confirmColor: "#FF5A5F",
                            confirmText: "知道了",
                            success: function(t) {
                                t.confirm && o.setData({
                                    scoringItem: o.data.competition.rule.scoringItems[i],
                                    inputScoringItem: !0,
                                    idxEditedScoringItem: i
                                });
                            }
                        });
                        break;

                      case 1:
                        o.data.competition.rule.scoringItems.splice(i, 1);
                        var e = o.data.competition.rule.scoringItems, a = 0;
                        if (e.length > 0) for (var n = 0; n < e.length; n++) a += e[n].fullScore * e[n].weight; else a = 100;
                        o.data.competition.rule.fullScore = a, o.setData({
                            competition: o.data.competition,
                            buttonText: "保存设置"
                        }), wx.showToast({
                            title: "记得保存设置哦",
                            icon: "none",
                            duration: 1500
                        });
                    }
                }
            });
        } else wx.showToast({
            title: "仅工作人员有权限编辑",
            icon: "none",
            duration: 2500
        });
    },
    updateRule: function() {
        var t = this;
        if ("example" == this.data.competition._id) wx.showToast({
            title: "比赛示例不支持设置评分规则",
            icon: "none",
            duration: 2e3
        }); else {
            var e = {
                _id: this.data.competition._id,
                rule: {
                    scoringRubic: this.data.competition.rule.scoringRubic,
                    fullScore: this.data.competition.rule.fullScore,
                    realName: this.data.competition.rule.realName,
                    lowestScore: this.data.competition.rule.lowestScore > 0 ? this.data.competition.rule.lowestScore : 0,
                    decimalPlaces: this.data.competition.rule.decimalPlaces,
                    scoringItems: this.data.competition.rule.scoringItems,
                    lowestScoresExcludedTotal: this.data.competition.rule.lowestScoresExcludedTotal,
                    topScoresExcludedTotal: this.data.competition.rule.topScoresExcludedTotal,
                    showScoringItemsForJury: this.data.competition.rule.showScoringItemsForJury,
                    allowJuryEditScore: this.data.competition.rule.allowJuryEditScore,
                    anonymousProgramInfo: this.data.competition.rule.anonymousProgramInfo
                }
            };
            e.rule.fullScore > 0 && e.rule.fullScore > e.rule.lowestScore && e.rule.decimalPlaces >= 0 && e.rule.decimalPlaces <= 5 && e.rule.lowestScoresExcludedTotal >= 0 && e.rule.topScoresExcludedTotal >= 0 ? (wx.showLoading({
                title: "更新评分规则",
                mask: !0
            }), wx.cloud.callFunction({
                name: "competition",
                data: {
                    competition: e,
                    operation: "update"
                }
            }).then(function(i) {
                wx.hideLoading(), wx.showToast({
                    title: "更新成功",
                    icon: "success",
                    duration: 2500
                }), t.setData({
                    buttonText: ""
                }), wx.cloud.callFunction({
                    name: "behavior",
                    data: {
                        competition_id: e._id,
                        behavior: "17设置规则"
                    }
                }).then(function(t) {});
            })) : !e.rule.fullScore || e.rule.fullScore < 0 ? wx.showToast({
                title: "满分值须大于0",
                icon: "none",
                duration: 2e3
            }) : e.rule.fullScore <= e.rule.lowestScore ? wx.showToast({
                title: "满分值须大于最低分",
                icon: "none",
                duration: 2e3
            }) : e.rule.decimalPlaces < 0 || e.rule.decimalPlaces > 5 ? wx.showToast({
                title: "小数点位数范围0-5位",
                icon: "none",
                duration: 2e3
            }) : e.rule.lowestScoresExcludedTotal < 0 ? wx.showToast({
                title: "去掉最低分个数至少为0(为0时不去掉最低分)",
                icon: "none",
                duration: 2e3
            }) : e.rule.topScoresExcludedTotal < 0 && wx.showToast({
                title: "去掉最高分个数至少为0(为0时不去掉最高分)",
                icon: "none",
                duration: 2e3
            });
        }
    },
    navToJuriesGroups: function() {
        wx.navigateTo({
            url: "/pages/competition/inviteJury/juriesGroups/juriesGroups?id=" + this.data.competition._id
        });
    },
    onPullDownRefresh: function() {
        this.getCompetition(this.data.competition._id);
    },
    hideContactService: function(t) {
        this.setData({
            contactServiceView: !1
        });
    },
    contactService: function(t) {
        i.collection("behaviors").add({
            data: {
                createTime: i.serverDate(),
                competition_id: this.data.competition._id,
                competition_title: this.data.competition.title,
                type: "contactService",
                page: "rules"
            }
        }).then(function(t) {}).catch(console.error), this.setData({
            contactServiceView: !1
        });
    }
});