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

t.command;

Page({
    data: {
        competition: {},
        showAuthorizeButton: !1,
        showJuriesAccountForm: !1
    },
    onLoad: function(e) {
        var n = this;
        wx.hideShareMenu(), t.collection("competitions").doc(e.scene).field({
            title: !0,
            startTime: !0,
            site: !0,
            sponsor: !0,
            organizer: !0,
            poster: !0,
            juries: !0,
            status: !0,
            juriesUnfocus: !0,
            rule: !0,
            settings: !0
        }).get().then(function(e) {
            var t = e.data;
            n.data.competition = t, t.startTime = o.formatTime("short", t.startTime), n.setData({
                competition: t
            }), wx.getStorageSync("getUserProfile") || t.settings && t.settings.unAuthorizeJury ? n.identifyJury(t, wx.getStorageSync("openid")) : n.setData({
                showAuthorizeButton: !0
            });
        }).catch(function(e) {
            o.competitionNoExistShowModal(e.errCode);
        });
    },
    getUserProfile: function() {
        var o = this;
        wx.getUserProfile({
            desc: "用于展示头像和昵称",
            lang: "zh_CN",
            success: function(t) {
                var n = t.userInfo;
                wx.setStorageSync("getUserProfile", !0), wx.showLoading({
                    title: "授权登录",
                    mask: !0
                }), wx.cloud.callFunction({
                    name: "updateUserInfo",
                    data: {
                        userInfo: n
                    }
                }).then(function(t) {
                    e.globalData.authorized = !0, e.globalData.userInfo = {
                        nickName: n.nickName,
                        avatarUrl: n.avatarUrl
                    }, e.globalData.openid = t.result.openid, o.identifyJury(o.data.competition, t.result.openid), 
                    o.setData({
                        showAuthorizeButton: !1
                    });
                });
            }
        });
    },
    identifyJury: function(o, e) {
        var n = this;
        wx.showLoading({
            title: "正在加载",
            mask: !0
        }), "已删除" == o.status || "已关闭" == o.status ? (wx.hideLoading(), wx.showModal({
            content: "主办方" + o.status + "比赛，或联系工作人员",
            showCancel: !1,
            confirmColor: "#FF5A5F",
            confirmText: "我知道了",
            success: function(o) {
                wx.reLaunch({
                    url: "/pages/index/index"
                });
            },
            fail: function(o) {
                console.log(o);
            }
        })) : o.juries.indexOf(e) >= 0 && -1 == o.juriesUnfocus.indexOf(e) ? setTimeout(function() {
            wx.redirectTo({
                url: "/pages/competition/judge/judge?id=" + o._id
            });
        }, 1500) : o.rule.refuseNewJury ? (wx.hideLoading(), wx.showModal({
            content: "主办方已关闭新评委加入通道，或联系工作人员",
            showCancel: !1,
            confirmColor: "#FF5A5F",
            confirmText: "我知道了",
            success: function(o) {
                wx.reLaunch({
                    url: "/pages/index/index"
                });
            },
            fail: function(o) {
                console.log(o);
            }
        })) : t.collection("juriesAccounts").where({
            competition_id: o._id
        }).count().then(function(e) {
            0 == e.total ? (console.log("未导入评委账号"), n.addRole(o)) : (console.log("导入了评委账号需要输入后验证"), 
            wx.hideLoading(), n.setData({
                showJuriesAccountForm: !0
            }));
        });
    },
    submitJuriesAccount: function(o) {
        var n = this, i = this.data.competition, a = {
            name: o.detail.value.name,
            password: o.detail.value.password
        };
        a.name ? a.password ? (wx.showLoading({
            title: "正在验证",
            mask: !0
        }), t.collection("juriesAccounts").where({
            competition_id: i._id,
            name: a.name,
            password: a.password
        }).get().then(function(o) {
            console.log(o.data), 0 == o.data.length ? wx.showToast({
                title: "请先重新确认姓名和密码",
                icon: "none",
                duration: 1500
            }) : o.data[0].user_openid ? o.data[0].user_openid == e.globalData.openid ? (setTimeout(function() {
                wx.redirectTo({
                    url: "/pages/competition/judge/judge?id=" + i._id
                });
            }, 1500), wx.hideLoading(), n.setData({
                showJuriesAccountForm: !1
            })) : wx.showToast({
                title: "该账号已被他人绑定，或联系工作人员解绑",
                icon: "none",
                duration: 2e3
            }) : (n.setData({
                showJuriesAccountForm: !1
            }), n.addRole(i), t.collection("juriesAccounts").doc(o.data[0]._id).update({
                data: {
                    user_openid: e.globalData.openid
                }
            }).then(function(o) {
                console.log("评委账号绑定微信", o);
            }).catch(function(o) {
                console.error(o);
            }), t.collection("users").where({
                _openid: e.globalData.openid
            }).field({
                _id: !0
            }).get().then(function(o) {
                t.collection("users").doc(o.data[0]._id).update({
                    data: {
                        realName: a.name
                    }
                }).then(function(o) {
                    console.log("评委信息user加入真实姓名");
                }).catch(function(o) {
                    console.log(o);
                });
            }).catch(function(o) {
                console.log(o);
            }));
        })) : wx.showToast({
            title: "请先输入密码",
            icon: "none",
            duration: 1e3
        }) : wx.showToast({
            title: "请先输入姓名",
            icon: "none",
            duration: 1e3
        });
    },
    addRole: function(o) {
        wx.cloud.callFunction({
            name: "role",
            data: {
                competition: {
                    _id: o._id
                },
                operation: "addRole",
                role: "jury"
            }
        }).then(function(e) {
            "addRole_crawler" == e.result.errMsg ? (wx.showToast({
                title: "检测到用户身份异常",
                icon: "none",
                duration: 1500
            }), setTimeout(function() {
                wx.reLaunch({
                    url: "/pages/index/index"
                });
            }, 1500)) : (wx.hideLoading(), wx.showToast({
                title: "已成为评委",
                icon: "success",
                duration: 2500
            }), setTimeout(function() {
                wx.redirectTo({
                    url: "/pages/competition/judge/judge?id=" + o._id
                });
            }, 1500));
        });
    }
});