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

Page({
    data: {
        competition: "",
        contactServiceView: !1,
        config_rechargeable: !1,
        user_vip: !1,
        copyCompetitionTotal: 0
    },
    onLoad: function(t) {
        var n = this;
        this.data.id = t.id, "oRnCI5JYWT9dmVycOnBjYaX1Yx7k" != o.globalData.openid && i.collection("behaviors").add({
            data: {
                createTime: i.serverDate(),
                competition_id: t.id,
                type: "visit",
                role: "worker",
                page: "competition"
            }
        }), wx.showLoading({
            title: "正在加载",
            mask: !0
        }), i.collection("_config").doc("config").field({
            rechargeable: !0
        }).get().then(function(t) {
            n.data.config_rechargeable = t.data.rechargeable;
        }).catch(function(t) {
            console.error(t);
        }), i.collection("users").where({
            _openid: o.globalData.openid,
            vipExpiryTime: e.gt(i.serverDate())
        }).count().then(function(t) {
            1 == t.total && (n.data.user_vip = !0);
        }).catch(function(t) {
            console.log(t);
        });
    },
    onShow: function() {
        this.getCompetition();
    },
    getCompetition: function() {
        var o = this, e = [ {
            url: "edit/edit?id=",
            title: "编辑比赛信息",
            iconSrc: "/images/icons/intro.png",
            note: "更新信息或背景图片等"
        }, {
            url: "programs/programs?id=",
            title: "添加参赛者",
            iconSrc: "/images/icons/intro.png",
            note: "按上场顺序添加"
        }, {
            url: "rules/rules?id=",
            title: "设置评分规则",
            iconSrc: "/images/icons/intro.png",
            note: "评委分组/评分项"
        }, {
            url: "inviteJury/inviteJury?id=",
            title: "邀请评委评分",
            iconSrc: "/images/icons/intro.png",
            note: "评委扫码即可评分"
        }, {
            url: "newestScores/newestScores?id=",
            title: "查看实时成绩",
            iconSrc: "/images/icons/intro.png",
            note: "评委提交分数后自动统计成绩"
        }, {
            url: "awards/awards?id=",
            title: "获奖名单",
            iconSrc: "/images/icons/intro.png",
            note: "设置奖项生成获奖名单"
        }, {
            url: "vote/vote?id=",
            title: "观众互动",
            iconSrc: "/images/icons/intro.png",
            note: "分享最终成绩/加油打Call"
        }, {
            title: "更多操作",
            iconSrc: "/images/icons/intro.png",
            note: "导出成绩/切换比赛状态等"
        } ];
        i.collection("competitions").doc(this.data.id).field({
            title: !0,
            startTime: !0,
            site: !0,
            sponsor: !0,
            organizer: !0,
            juries: !0,
            poster: !0,
            creator: !0,
            status: !0,
            tags: !0,
            notice: !0,
            rechargeable: !0,
            rule: !0,
            juriesGroups: !0,
            proExpiryTime: !0,
            programsTotal: !0,
            posterSize: !0
        }).get().then(function(a) {
            wx.hideLoading(), wx.stopPullDownRefresh();
            var c = a.data, s = new Date();
            c.proExpiryTime && (c.rechargeable = new Date(c.proExpiryTime) > s), c.startTime = t.formatTime("short", c.startTime), 
            i.collection("programs").where({
                competition_id: o.data.id
            }).count().then(function(t) {
                c.programsTotal = t.total, i.collection("users").aggregate().addFields({
                    matched: n.in([ "$_openid", c.juries ])
                }).match({
                    matched: !0
                }).group({
                    _id: null,
                    juries: n.addToSet({
                        _openid: "$_openid"
                    })
                }).end().then(function(t) {
                    for (var i = t.list.length >= 1 ? t.list[0].juries.length : 0, n = 0; n < e.length; n++) "添加参赛者" == e[n].title && (e[n].note = c.programsTotal > 0 ? c.programsTotal : "按上场顺序添加"), 
                    "邀请评委评分" == e[n].title && "example" != o.data.id && (e[n].note = i > 0 ? i + "位" : "评委扫评分邀请码即可评分");
                    var a = 0;
                    if (c.juriesGroups && c.juriesGroups.length >= 1) for (var s = 0; s < c.juriesGroups.length; s++) a += c.juriesGroups[s].juriesDetails.length;
                    !c.notice && a > 0 && a < i && (c.notice = "提醒：评委分组中有评委尚未分组，未分组评委的分数无法纳入成绩计算，请及时将评委分到对应的组别!"), 
                    o.setData({
                        competition: c,
                        listMenu: e
                    });
                });
            });
        });
    },
    closeNotice: function(t) {
        var o = this;
        this.data.competition.notice && wx.cloud.callFunction({
            name: "competition",
            data: {
                competition: {
                    _id: this.data.competition._id,
                    notice: ""
                },
                operation: "update"
            }
        }).then(function(t) {
            o.data.competition.notice = "", o.setData({
                competition: o.data.competition
            });
        });
    },
    navToGuide: function() {
        wx.navigateTo({
            url: "/pages/more/guide/guide"
        });
    },
    showActionSheet: function() {
        var t = this;
        wx.showActionSheet({
            itemList: [ "复制比赛", "添加工作人员", "已关闭" == t.data.competition.status ? "重新开放比赛(评委观众可见)" : "关闭比赛(仅主办可见/评委观众不可见)", "删除比赛" ],
            success: function(i) {
                if ("example" == t.data.competition._id) wx.showToast({
                    title: "比赛示例不能使用此功能",
                    icon: "none",
                    duration: 3e3
                }); else {
                    var e = {
                        _id: t.data.competition._id,
                        status: 2 == i.tapIndex ? "已关闭" == t.data.competition.status ? "正常" : "已关闭" : 3 == i.tapIndex ? "已删除" : "正常"
                    };
                    switch (i.tapIndex) {
                      case 0:
                        t.copyCompetition();
                        break;

                      case 1:
                        wx.navigateTo({
                            url: "/pages/competition/inviteWorker/inviteWorker?id=" + t.data.competition._id
                        });
                        break;

                      case 2:
                        wx.showLoading({
                            title: "正在提交",
                            mask: !0
                        }), wx.cloud.callFunction({
                            name: "competition",
                            data: {
                                competition: e,
                                operation: "update"
                            }
                        }).then(function(o) {
                            wx.hideLoading(), wx.showToast({
                                title: "已关闭" == e.status ? "已关闭比赛" : "正常状态",
                                icon: "success",
                                duration: 1500
                            }), t.data.competition.status = "已关闭" == e.status ? "已关闭" : "正常", t.setData({
                                competition: t.data.competition
                            }), wx.cloud.callFunction({
                                name: "behavior",
                                data: {
                                    competition_id: e._id,
                                    behavior: "31切换状态"
                                }
                            }).then(function(t) {});
                        });
                        break;

                      case 3:
                        t.data.competition.creator == o.globalData.openid ? wx.showModal({
                            title: "温馨提示",
                            content: "比赛活动删除后不可恢复，确认删除吗？",
                            confirmColor: "#FF5A5F",
                            confirmText: "确定删除",
                            cancelColor: "#484848",
                            cancelText: "取消",
                            success: function(t) {
                                t.confirm && (wx.showLoading({
                                    title: "正在提交",
                                    mask: !0
                                }), wx.cloud.callFunction({
                                    name: "competition",
                                    data: {
                                        competition: e,
                                        operation: "update"
                                    }
                                }).then(function(t) {
                                    wx.hideLoading(), wx.showToast({
                                        title: "已删除",
                                        icon: "success",
                                        duration: 1500,
                                        complete: function(t) {
                                            setTimeout(function() {
                                                wx.navigateBack({
                                                    delta: 1
                                                });
                                            }, 1500);
                                        }
                                    }), wx.cloud.callFunction({
                                        name: "behavior",
                                        data: {
                                            competition_id: e._id,
                                            behavior: "35删除比赛"
                                        }
                                    }).then(function(t) {
                                        console.log("35删除比赛", t);
                                    });
                                }));
                            },
                            fail: function(t) {}
                        }) : wx.showModal({
                            content: "仅创建者有权限删除比赛，可选择退出比赛，退出后不再显示。",
                            confirmColor: "#FF5A5F",
                            confirmText: "退出比赛",
                            cancelColor: "#484848",
                            cancelText: "取消",
                            success: function(t) {
                                t.confirm && (wx.showLoading({
                                    title: "正在提交",
                                    mask: !0
                                }), wx.cloud.callFunction({
                                    name: "role",
                                    data: {
                                        competition: e,
                                        operation: "deleteRole",
                                        role: "worker",
                                        openidDeleted: o.globalData.openid
                                    }
                                }).then(function(t) {
                                    wx.showToast({
                                        title: "退出比赛",
                                        icon: "success",
                                        duration: 1500,
                                        complete: function(t) {
                                            setTimeout(function() {
                                                wx.navigateBack({
                                                    delta: 1
                                                });
                                            }, 1500);
                                        }
                                    });
                                }));
                            },
                            fail: function(t) {}
                        });
                    }
                }
            },
            fail: function(t) {}
        });
    },
    copyCompetition: function(t) {
        var o = this;
        wx.showActionSheet({
            title: "选择复制的内容",
            itemList: [ "复制比赛信息+默认评分规则", "复制比赛信息+所有评分规则", "复制比赛信息+所有评分规则+参赛者" ],
            success: function(t) {
                if ("example" == o.data.competition._id) wx.showToast({
                    title: "比赛示例不能使用此功能",
                    icon: "none",
                    duration: 3e3
                }); else {
                    var i = t.tapIndex;
                    0 == i && o.data.competition.rechargeable || o.data.user_vip || o.data.config_rechargeable ? (wx.showLoading({
                        title: "正在复制",
                        mask: !0
                    }), o.data.copyCompetitionTotal++, wx.cloud.callFunction({
                        name: "copyCompetition",
                        data: {
                            competition_id: o.data.competition._id,
                            type: 0 == i ? "modelV1" : 1 == i ? "modelV2" : "modelV3",
                            copyCompetitionTotal: o.data.copyCompetitionTotal
                        }
                    }).then(function(t) {
                        wx.hideLoading();
                        var o = t.result.competition_id;
                        wx.showModal({
                            title: "已复制比赛",
                            content: "是否查看新比赛？",
                            confirmColor: "#FF5A5F",
                            confirmText: "查看",
                            cancelColor: "#767676",
                            cancelText: "暂不",
                            success: function(t) {
                                t.confirm && wx.navigateTo({
                                    url: "/pages/competition/competition?id=" + o
                                });
                            },
                            fail: function(t) {
                                console.log(t);
                            }
                        });
                    })) : o.setData({
                        contactServiceView: !0
                    });
                }
            },
            fail: function(t) {}
        });
    },
    unfocusCompetitionExample: function(t) {
        wx.showModal({
            title: "",
            content: "比赛示例方便用户体验赛易评的功能，建议熟悉功能后再移除示例。",
            confirmColor: "#FF5A5F",
            confirmText: "移除示例",
            cancelColor: "#484848",
            cancelText: "保留示例",
            success: function(t) {
                t.confirm && (wx.showLoading({
                    title: "正在提交",
                    mask: !0
                }), wx.cloud.callFunction({
                    name: "focusExample",
                    data: {
                        operation: "unfocusExample"
                    }
                }).then(function(t) {
                    wx.hideLoading(), wx.showToast({
                        title: "不再显示示例",
                        icon: "success",
                        duration: 1500
                    }), setTimeout(function() {
                        wx.navigateBack({
                            delta: 1
                        });
                    }, 1500);
                }));
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    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: "competition"
            }
        }).then(function(t) {}).catch(console.error), this.setData({
            contactServiceView: !1
        });
    },
    onPullDownRefresh: function() {
        this.getCompetition();
    },
    onShareAppMessage: function() {
        return {
            title: "查看成绩一起加油打Call",
            path: "/pages/competition/vote/vote?id=" + this.data.competition._id
        };
    }
});