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

e.command.aggregate;

Page({
    data: {
        program: {},
        competition: {},
        submittingScore: !1,
        inputtingScore: "",
        inputtingScoringItemsScoreTotal: 0
    },
    onLoad: function(t) {
        var e = this;
        e.getOpenerEventChannel().on("acceptDataFromOpenerPage", function(t) {
            if (t.competition.rule.scoringItems && t.competition.rule.scoringItems.length > 0) for (var o = 0; o < t.competition.rule.scoringItems.length; o++) t.competition.rule.scoringItems[o].inputtingScore = t.competition.rule.scoringItems[o].oldScore >= 0 ? t.competition.rule.scoringItems[o].oldScore : "";
            e.setData({
                program: t.program,
                competition: t.competition
            });
        });
        var o = getCurrentPages();
        o[o.length - 2].data.navigateToPage = "submitScore", wx.hideShareMenu();
    },
    inputtingScore: function(t) {
        var e = t.target.dataset.type, o = t.detail.value, n = 0;
        n = "slider" == e ? parseFloat(o.toFixed(1)) : o >= 0 ? o : 0, this.setData({
            inputtingScore: n
        });
    },
    inputtingScoringItemsScore: function(t) {
        var e = t.target.dataset.index, o = t.target.dataset.type, n = t.detail.value;
        this.data.competition.rule.scoringItems[e].inputtingScore = "slider" == o ? parseFloat(n.toFixed(1)) : n >= 0 ? n : 0;
        for (var i = this.data.competition.rule.scoringItems, a = 0, c = 0; c < i.length; c++) i[c].inputtingScore > 0 && (a += i[c].inputtingScore * i[c].weight);
        this.setData({
            inputtingScoringItemsScoreTotal: a,
            competition: this.data.competition
        });
    },
    submitScore: function(n) {
        var i = this;
        if (i.data.submittingScore) wx.showToast({
            title: "正在提交成绩",
            icon: "none",
            duration: 750
        }); else {
            i.data.submittingScore = !0, wx.showLoading({
                title: "校验成绩",
                mask: !0
            });
            var a = "";
            n.detail.value.comment && (a = {
                comment: n.detail.value.comment,
                _openid: t.globalData.openid
            });
            var c = i.data.competition, r = i.data.program, s = getCurrentPages(), l = s[s.length - 2], d = 0, u = [], g = !0;
            if (c.rule.scoringItems && c.rule.scoringItems.length > 0) {
                for (var m = n.detail.value, p = 0; p < c.rule.scoringItems.length; p++) {
                    var h = parseFloat(m["input" + p]);
                    if (d += h * c.rule.scoringItems[p].weight, u.push(parseFloat(h.toFixed(5))), !m["input" + p]) {
                        wx.showToast({
                            title: "请输入第" + (p + 1) + "项得分",
                            icon: "none",
                            duration: 2e3
                        }), g = !1;
                        break;
                    }
                    if (m["input" + p] < 0) {
                        wx.showToast({
                            title: "第" + (p + 1) + "项得分不能小于0",
                            icon: "none",
                            duration: 2e3
                        }), g = !1;
                        break;
                    }
                    if (m["input" + p] > c.rule.scoringItems[p].fullScore) {
                        wx.showToast({
                            title: "第" + (p + 1) + "项超出该项最高分",
                            icon: "none",
                            duration: 2e3
                        }), g = !1;
                        break;
                    }
                }
                d < c.rule.lowestScore && (wx.showToast({
                    title: "小于最低分(" + c.rule.lowestScore + "分)",
                    icon: "none",
                    duration: 2e3
                }), g = !1);
            } else d = parseFloat(n.detail.value.score), isNaN(d) || null == d ? (wx.showToast({
                title: "请先输入得分再提交",
                icon: "none",
                duration: 2e3
            }), g = !1) : d < c.rule.lowestScore ? (wx.showToast({
                title: "小于最低分(" + c.rule.lowestScore + "分)",
                icon: "none",
                duration: 2e3
            }), g = !1) : d > c.rule.fullScore && (wx.showToast({
                title: "超过最高分(" + c.rule.fullScore + "分)",
                icon: "none",
                duration: 2e3
            }), g = !1);
            var w = {};
            g ? (d = parseFloat(d.toFixed(5)), w = c.rule.scoringItems && c.rule.scoringItems.length > 0 ? {
                score: d,
                scoresDetails: u,
                _openid: t.globalData.openid
            } : {
                score: d,
                _openid: t.globalData.openid
            }, e.collection("competitions").doc(c._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
            }).get().then(function(n) {
                "已关闭" == (c = n.data).status || "已删除" == c.status ? (wx.showToast({
                    title: "主办方" + c.status + "比赛",
                    icon: "none",
                    duration: 1500
                }), i.data.submittingScore = !1) : e.collection("programs").doc(r._id).get().then(function(n) {
                    if (n.data.judging && -1 == JSON.stringify(n.data.scores).indexOf(t.globalData.openid)) {
                        wx.hideLoading();
                        var s = "得分" + d + "分";
                        wx.showModal({
                            title: s,
                            content: c.rule.allowJuryEditScore ? "确认提交分数吗？" : "分数提交后无法修改，确认提交吗？",
                            confirmColor: "#FF5A5F",
                            confirmText: "确认提交",
                            cancelColor: "#484848",
                            cancelText: "取消",
                            success: function(n) {
                                n.confirm ? e.collection("competitions").where({
                                    _id: i.data.id,
                                    juries: t.globalData.openid
                                }).count().then(function(t) {
                                    0 == t.total ? wx.showModal({
                                        content: "系统检测到你不是该比赛的评委，请联系工作人员重新邀请成为评委。",
                                        showCancel: !1,
                                        confirmColor: "#FF5A5F",
                                        confirmText: "我知道了",
                                        success: function(t) {
                                            wx.reLaunch({
                                                url: "/pages/index/index"
                                            });
                                        },
                                        fail: function(t) {
                                            console.log(t);
                                        }
                                    }) : (wx.showLoading({
                                        title: "正在提交",
                                        mask: !0
                                    }), e.collection("programs").doc(r._id).update({
                                        data: {
                                            scores: o.addToSet(w)
                                        }
                                    }).then(function(t) {
                                        wx.hideLoading(), wx.showToast({
                                            title: "提交成绩",
                                            icon: "success",
                                            duration: 1500
                                        }), i.data.submittingScore = !1, a.comment && wx.cloud.callFunction({
                                            name: "textSecCheck",
                                            data: {
                                                text: JSON.stringify(a.comment)
                                            }
                                        }).then(function(t) {
                                            87014 == t.result.errCode || e.collection("programs").doc(r._id).update({
                                                data: {
                                                    comments: o.addToSet(a)
                                                }
                                            }).then(function(t) {
                                                console.log("comment_addToSet", t);
                                            });
                                        }), l.data.submitedScore = !0, wx.cloud.callFunction({
                                            name: "behavior",
                                            data: {
                                                competition_id: i.data.program.competition_id,
                                                behavior: "27评委评分"
                                            }
                                        }).then(function(t) {}), setTimeout(function() {
                                            wx.navigateBack({
                                                delta: 1
                                            });
                                        }, 1e3);
                                    }));
                                }).catch(function(t) {
                                    console.error(t);
                                }) : n.cancel && (i.data.submittingScore = !1);
                            },
                            fail: function(t) {
                                console.log(t);
                            }
                        });
                    } else if (n.data.judging) if (JSON.stringify(n.data.scores).indexOf(t.globalData.openid) >= 0) if (c.rule.allowJuryEditScore) {
                        wx.hideLoading();
                        var u = "得分" + d + "分";
                        wx.showModal({
                            title: u,
                            content: "确认提交分数吗？",
                            confirmColor: "#FF5A5F",
                            confirmText: "确认提交",
                            cancelColor: "#484848",
                            cancelText: "取消",
                            success: function(n) {
                                n.confirm ? e.collection("competitions").where({
                                    _id: i.data.id,
                                    juries: t.globalData.openid
                                }).count().then(function(n) {
                                    0 == n.total ? wx.showModal({
                                        content: "系统检测到你不是该比赛的评委，请联系工作人员重新邀请成为评委。",
                                        showCancel: !1,
                                        confirmColor: "#FF5A5F",
                                        confirmText: "我知道了",
                                        success: function(t) {
                                            wx.reLaunch({
                                                url: "/pages/index/index"
                                            });
                                        },
                                        fail: function(t) {
                                            console.log(t);
                                        }
                                    }) : (wx.showLoading({
                                        title: "正在提交",
                                        mask: !0
                                    }), e.collection("programs").doc(r._id).update({
                                        data: {
                                            scores: o.pull({
                                                _openid: o.eq(t.globalData.openid)
                                            }),
                                            comments: o.pull({
                                                _openid: o.eq(t.globalData.openid)
                                            })
                                        }
                                    }).then(function(t) {
                                        e.collection("programs").doc(r._id).update({
                                            data: {
                                                scores: o.addToSet(w)
                                            }
                                        }).then(function(t) {
                                            wx.hideLoading(), wx.showToast({
                                                title: "提交成绩",
                                                icon: "success",
                                                duration: 1500
                                            }), i.data.submittingScore = !1, a.comment && wx.cloud.callFunction({
                                                name: "textSecCheck",
                                                data: {
                                                    text: JSON.stringify(a.comment)
                                                }
                                            }).then(function(t) {
                                                87014 == t.result.errCode || e.collection("programs").doc(r._id).update({
                                                    data: {
                                                        comments: o.addToSet(a)
                                                    }
                                                }).then(function(t) {
                                                    console.log("comment_addToSet", t);
                                                });
                                            });
                                            for (var n = 0; n < c.rule.scoringItems.length; n++) c.rule.scoringItems[n].inputtingScore = "";
                                            l.data.submitedScore = !0, wx.cloud.callFunction({
                                                name: "behavior",
                                                data: {
                                                    competition_id: i.data.program.competition_id,
                                                    behavior: "27评委评分"
                                                }
                                            }).then(function(t) {}), setTimeout(function() {
                                                wx.navigateBack({
                                                    delta: 1
                                                });
                                            }, 1e3);
                                        });
                                    }));
                                }).catch(function(t) {
                                    console.error(t);
                                }) : n.cancel && (i.data.submittingScore = !1);
                            },
                            fail: function(t) {
                                console.log(t);
                            }
                        });
                    } else wx.showToast({
                        title: "已提交过成绩",
                        icon: "none",
                        duration: 1500
                    }), i.data.submittingScore = !1; else wx.showToast({
                        title: "刷新后重试",
                        icon: "none",
                        duration: 1500
                    }), i.data.submittingScore = !1; else wx.showToast({
                        title: "主办方已暂停该参赛者的评分",
                        icon: "none",
                        duration: 2e3
                    }), i.data.submittingScore = !1;
                });
            })) : i.data.submittingScore = !1;
        }
    },
    previewImage: function(t) {
        console.log(t);
        var e = t.currentTarget.dataset.image;
        wx.previewImage({
            current: e,
            urls: [ e ]
        });
    }
});