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

Page({
    data: {
        competition: "",
        inviteJuryWxacodePath: "",
        juries: "",
        watcherCompetition: "",
        updatedJuriesGroups: !1
    },
    onLoad: function(i) {
        var a = this;
        this.data.competition_id = i.id;
        var n = this;
        wx.showLoading({
            title: "加载评分码"
        }), o.collection("competitions").doc(i.id).field({
            title: !0,
            poster: !0,
            startTime: !0,
            site: !0,
            sponsor: !0,
            organizer: !0,
            status: !0,
            rechargeable: !0,
            rule: !0,
            juries: !0,
            workers: !0,
            juriesGroups: !0,
            inviteJuryWxacode: !0,
            posterSize: !0
        }).get().then(function(o) {
            var c = o.data;
            c.startTime = t.formatTime("short", c.startTime), a.setData({
                competition: c,
                competition_id: c._id
            }), "已关闭" == c.status ? (wx.hideLoading(), wx.hideShareMenu(), wx.showModal({
                content: "比赛为关闭状态时不能邀请评委，或返回上一页“更多操作-重新开放比赛”后再邀请",
                showCancel: !1,
                confirmColor: "#FF5A5F",
                confirmText: "返回",
                success: function(t) {
                    wx.navigateBack({
                        delta: 1
                    });
                },
                fail: function(t) {
                    console.log(t);
                }
            })) : (c.inviteJuryWxacode ? (n.setData({
                inviteJuryWxacodePath: c.inviteJuryWxacode,
                openid: e.globalData.openid
            }), setTimeout(function() {
                wx.hideLoading();
            }, 500)) : wx.cloud.callFunction({
                name: "getWxacodeV2",
                data: {
                    id: i.id,
                    role: "jury"
                }
            }).then(function(o) {
                wx.showLoading({
                    title: "即将完成加载"
                });
                var a = wx.arrayBufferToBase64(o.result.wxacode.buffer);
                t.base64src("data:image/jpeg;base64," + a).then(function(t) {
                    n.data.inviteJuryWxacodePath = t, n.setData({
                        inviteJuryWxacodePath: t,
                        openid: e.globalData.openid
                    }), wx.hideLoading(), wx.cloud.uploadFile({
                        cloudPath: "inviteJuryWxacodes/" + i.id + ".jpg",
                        filePath: t
                    }).then(function(t) {
                        wx.cloud.callFunction({
                            name: "competition",
                            data: {
                                competition: {
                                    _id: i.id,
                                    inviteJuryWxacode: "cloud://release-bu5dc.7265-release-bu5dc-1301069672/inviteJuryWxacodes/" + i.id + ".jpg"
                                },
                                operation: "update"
                            }
                        }).then(function(t) {});
                    });
                }).catch(function(t) {
                    console.log(t);
                });
            }).catch(console.error), n.getJuries(c.juries));
        }).catch(function(e) {
            t.competitionNoExistShowModal(e.errCode);
        }), wx.updateShareMenu({
            isPrivateMessage: !0
        });
    },
    onShow: function() {
        this.watcherCompetition(), this.data.updatedJuriesGroups && (this.setData({
            competition: this.data.competition
        }), this.data.updatedJuriesGroups = !1);
    },
    watcherCompetition: function() {
        var t = this;
        wx.getSystemInfo({
            success: function(e) {
                var i = e.platform;
                "windows" != i && (t.data.watcherCompetition = o.collection("competitions").doc(t.data.competition_id).watch({
                    onChange: function(e) {
                        1 == e.docs.length && "init" != e.type && e.docChanges[0].updatedFields.juries && t.getJuries(e.docs[0].juries);
                    },
                    onError: function(e) {
                        setTimeout(function() {
                            t.watcherCompetition();
                        }, 1e3), o.collection("_errorsWatcher").add({
                            data: {
                                competition_id: t.data.competition._id,
                                desc: "watcherError",
                                page: "inviteJury",
                                createtime: o.serverDate(),
                                platform: i
                            }
                        }).then(function(t) {}).catch(console.error);
                    }
                }));
            }
        });
    },
    onHide: function() {
        this.data.watcherCompetition && this.data.watcherCompetition.close();
    },
    onUnload: function() {
        this.data.watcherCompetition && this.data.watcherCompetition.close();
    },
    getJuries: function(t) {
        var a = this;
        o.collection("users").aggregate().addFields({
            matched: i.in([ "$_openid", t ])
        }).match({
            matched: !0
        }).group({
            _id: null,
            juries: i.addToSet({
                _openid: "$_openid",
                avatarUrl: "$avatarUrl",
                authorized: "$authorized",
                nickName: "$nickName",
                realName: "$realName"
            })
        }).end().then(function(t) {
            for (var o = t.list.length >= 1 ? t.list[0].juries : [], i = !1, n = 0; n < o.length; n++) o[n].authorized || o[n].avatarUrl ? o[n].name = o[n].realName ? o[n].realName : o[n].nickName ? o[n].nickName : "无昵称或未授权微信登录" : (o[n].name = "无昵称或未授权微信登录", 
            o[n].avatarUrl = "/images/user-unlogin.png"), o[n]._openid == e.globalData.openid && (i = !0);
            o.sort(function(t, e) {
                return t.name.localeCompare(e.name);
            }), a.setData({
                juries: o,
                myselfIsJury: i
            });
        });
    },
    addMyselfAsJury: function() {
        var t = this;
        wx.showModal({
            content: "确认将自己添加为评委吗？",
            cancelColor: "#484848",
            cancelText: "取消",
            confirmColor: "#FF5A5F",
            confirmText: "确定",
            success: function(e) {
                e.confirm && (wx.showLoading({
                    title: "正在添加",
                    mask: !0
                }), wx.cloud.callFunction({
                    name: "role",
                    data: {
                        competition: {
                            _id: t.data.competition._id
                        },
                        operation: "addRole",
                        role: "jury"
                    }
                }).then(function(e) {
                    wx.hideLoading(), wx.showToast({
                        title: "已添加为评委",
                        icon: "success",
                        duration: 2500
                    }), t.setData({
                        myselfIsJury: !0
                    });
                }));
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    switchChangeRefuseNewJury: function(t) {
        var e = this, o = t.detail.value;
        this.data.competition.rule.refuseNewJury = o, wx.cloud.callFunction({
            name: "competition",
            data: {
                competition: {
                    _id: this.data.competition._id,
                    rule: this.data.competition.rule
                },
                operation: "update"
            }
        }).then(function(t) {
            wx.hideLoading(), wx.showToast({
                title: "已更新",
                icon: "success",
                duration: 1500
            }), e.setData({
                competition: e.data.competition
            });
        });
    },
    deletejury: function(t) {
        var o = t.currentTarget.dataset.jury, i = this;
        "example" == i.data.competition._id ? wx.showToast({
            title: "比赛示例不能删除评委",
            icon: "none",
            duration: 1500
        }) : (this.data.competition.workers.indexOf(e.globalData.openid) >= 0 || "oRnCI5JYWT9dmVycOnBjYaX1Yx7k" == e.globalData.openid) && wx.showModal({
            content: "确认删除该评委吗?若该评委在本次比赛中提交过参赛者得分，将同步删除",
            cancelColor: "#484848",
            cancelText: "取消",
            confirmColor: "#FF5A5F",
            confirmText: "确定",
            success: function(t) {
                t.confirm && (wx.showLoading({
                    title: "正在删除",
                    mask: !0
                }), wx.cloud.callFunction({
                    name: "role",
                    data: {
                        competition: {
                            _id: i.data.competition._id,
                            juriesGroups: i.data.competition.juriesGroups
                        },
                        operation: "deleteRole",
                        role: "jury",
                        openidDeleted: o._openid
                    }
                }).then(function(t) {
                    wx.hideLoading(), wx.showToast({
                        title: "删除评委",
                        icon: "success",
                        duration: 2e3
                    }), o._openid == e.globalData.openid && i.setData({
                        myselfIsJury: !1
                    });
                }));
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    previewWXACode: function() {
        var t = this.data.inviteJuryWxacodePath;
        wx.previewImage({
            current: t,
            urls: [ t ]
        });
    },
    Addrecord: function() {
        "oRnCI5JYWT9dmVycOnBjYaX1Yx7k" != e.globalData.openid && o.collection("behaviors").add({
            data: {
                createTime: o.serverDate(),
                type: "navigatoJudge",
                page: "inviteJury"
            }
        });
    },
    navToJuriesGroups: function() {
        wx.navigateTo({
            url: "/pages/competition/inviteJury/juriesGroups/juriesGroups?id=" + this.data.competition._id
        });
    },
    navToJuriesAccounts: function() {
        wx.navigateTo({
            url: "/pages/competition/inviteJury/juriesAccounts/juriesAccounts?id=" + this.data.competition._id
        });
    },
    savePoster: function() {
        t.base64src("data:image/jpeg;base64," + this.data.inviteJuryWxacodePath).then(function(t) {
            var e = t;
            wx.getSetting({
                success: function(t) {
                    t.authSetting["scope.writePhotosAlbum"] ? wx.saveImageToPhotosAlbum({
                        filePath: e,
                        success: function(t) {
                            wx.showToast({
                                title: "保存到手机相册",
                                icon: "success",
                                mask: !0
                            });
                        }
                    }) : wx.authorize({
                        scope: "scope.writePhotosAlbum",
                        success: function() {
                            wx.saveImageToPhotosAlbum({
                                filePath: e,
                                success: function(t) {
                                    wx.showToast({
                                        title: "保存到手机相册",
                                        icon: "success",
                                        mask: !0
                                    });
                                }
                            });
                        }
                    });
                }
            });
        }).catch(function(t) {
            console.log(t);
        });
    },
    inviteJuryByGroup: function(t) {
        var e = t.detail.value, i = this.data.competition.juriesGroups, a = this;
        wx.showModal({
            content: "分组评委邀请码生成后不能更改分组名称。若更改需重新生成邀请码，否则评委加入后将无法自动分配对应的组别。",
            cancelColor: "#484848",
            cancelText: "取消",
            confirmColor: "#FF5A5F",
            confirmText: "我知道了",
            success: function(t) {
                t.confirm && (wx.showLoading({
                    title: "生成邀请码",
                    mask: !0
                }), o.collection("juriesGroups").where({
                    competition_id: a.data.competition._id,
                    juriesGroupTitle: i[e].title
                }).get().then(function(t) {
                    t.data && t.data.length >= 1 ? a.getWxacodeJuryByGroup(t.data[0]._id, i[e].title) : o.collection("juriesGroups").add({
                        data: {
                            competition_id: a.data.competition._id,
                            createtime: o.serverDate(),
                            juriesGroupTitle: i[e].title
                        }
                    }).then(function(t) {
                        a.getWxacodeJuryByGroup(t._id, i[e].title);
                    }).catch(console.error);
                }).catch(console.error));
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    getWxacodeJuryByGroup: function(e, i) {
        var a = this;
        wx.cloud.callFunction({
            name: "getWxacodeV2",
            data: {
                id: e,
                role: "juryByGroup"
            }
        }).then(function(e) {
            var o = wx.arrayBufferToBase64(e.result.wxacode.buffer);
            t.base64src("data:image/jpeg;base64," + o).then(function(t) {
                a.setData({
                    inviteJuryWxacodePath: t,
                    inviteJuriesGroupTitle: i
                }), wx.hideLoading(), wx.showToast({
                    title: "更新邀请码",
                    icon: "success",
                    mask: !0
                });
            }).catch(function(t) {
                console.log(t);
            });
        }).catch(console.error), o.collection("behaviors").add({
            data: {
                createTime: o.serverDate(),
                type: "inviteJuryByGroup",
                page: "inviteJury"
            }
        }).then(function(t) {}).catch(console.error);
    },
    onShareAppMessage: function() {
        return {
            title: "邀请您担任比赛评委@" + this.data.competition.title,
            path: "/pages/addRole/jury/jury?scene=" + this.data.competition._id
        };
    }
});