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

o.command;

Page({
    data: {
        competition_id: "",
        maskStatus: !1
    },
    onLoad: function(t) {
        this.data.competition_id = t.scene, wx.getStorageSync("getUserProfile") ? this.addRole(t.scene, wx.getStorageSync("openid")) : this.setData({
            maskStatus: !0
        }), wx.hideShareMenu();
    },
    getUserProfile: function() {
        var t = this;
        t.hideMaskStatus(), wx.getUserProfile({
            desc: "用于展示头像和昵称",
            lang: "zh_CN",
            success: function(o) {
                var i = o.userInfo;
                wx.setStorageSync("getUserProfile", !0), wx.showLoading({
                    title: "授权登录",
                    mask: !0
                }), wx.cloud.callFunction({
                    name: "updateUserInfo",
                    data: {
                        userInfo: i
                    }
                }).then(function(o) {
                    e.globalData.authorized = !0, e.globalData.userInfo = {
                        nickName: i.nickName,
                        avatarUrl: i.avatarUrl
                    }, t.addRole(t.data.competition_id, o.result.openid);
                });
            }
        });
    },
    hideMaskStatus: function(t) {
        this.setData({
            maskStatus: !1
        });
    },
    addRole: function(e, i) {
        wx.showLoading({
            title: "正在加载",
            mask: !0
        }), o.collection("competitions").doc(e).field({
            workers: !0,
            status: !0
        }).get().then(function(t) {
            var o = t.data;
            "已删除" == o.status ? (wx.hideLoading(), wx.showModal({
                content: "主办方" + o.status + "比赛，或联系工作人员",
                showCancel: !1,
                confirmColor: "#FF5A5F",
                confirmText: "我知道了",
                success: function(t) {
                    wx.reLaunch({
                        url: "/pages/index/index"
                    });
                },
                fail: function(t) {
                    console.log(t);
                }
            })) : o.workers.indexOf(i) >= 0 ? setTimeout(function() {
                wx.redirectTo({
                    url: "/pages/competition/competition?id=" + e
                });
            }, 1500) : wx.cloud.callFunction({
                name: "role",
                data: {
                    competition: {
                        _id: e
                    },
                    operation: "addRole",
                    role: "worker"
                }
            }).then(function(t) {
                "addRole_crawler" == t.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/competition?id=" + e
                    });
                }, 2500));
            });
        }).catch(function(e) {
            t.competitionNoExistShowModal(e.errCode);
        });
    }
});