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

Page({
    data: {
        competition: "",
        inviteWorkerWxacodePath: "",
        workers: "",
        watcherCompetition: ""
    },
    onLoad: function(e) {
        var o = this;
        this.data.id = e.id, wx.showLoading({
            title: "加载邀请码"
        }), wx.cloud.callFunction({
            name: "getWxacodeV2",
            data: {
                id: e.id,
                role: "worker"
            }
        }).then(function(e) {
            wx.showLoading({
                title: "即将完成加载"
            });
            var a = wx.arrayBufferToBase64(e.result.wxacode.buffer);
            t.base64src("data:image/jpeg;base64," + a).then(function(t) {
                o.setData({
                    inviteWorkerWxacodePath: t
                }), wx.hideLoading();
            }).catch(function(t) {
                console.log(t);
            });
        }).catch(console.error), wx.updateShareMenu({
            isPrivateMessage: !0
        });
    },
    onShow: function() {
        this.getCompetition();
    },
    getCompetition: function() {
        var e = this;
        o.collection("competitions").doc(this.data.id).field({
            title: !0,
            startTime: !0,
            site: !0,
            sponsor: !0,
            organizer: !0,
            poster: !0,
            status: !0,
            workers: !0,
            creator: !0,
            posterSize: !0
        }).get().then(function(o) {
            var a = o.data;
            a.startTime = t.formatTime("short", a.startTime), e.setData({
                competition: a
            }), e.getWorkers(a.workers), e.watcherCompetition();
        }).catch(function(e) {
            t.competitionNoExistShowModal(e.errCode);
        });
    },
    watcherCompetition: function() {
        var t = this;
        wx.getSystemInfo({
            success: function(e) {
                var a = e.platform;
                "windows" != a && (t.data.watcherCompetition = o.collection("competitions").doc(t.data.id).watch({
                    onChange: function(e) {
                        1 == e.docs.length && "init" != e.type && e.docChanges[0].updatedFields.workers && t.getWorkers(e.docs[0].workers);
                    },
                    onError: function(e) {
                        setTimeout(function() {
                            t.watcherCompetition();
                        }, 1e3), o.collection("_errorsWatcher").add({
                            data: {
                                competition_id: t.data.id,
                                desc: "watcherError",
                                page: "inviteWorker",
                                platform: a,
                                createtime: o.serverDate()
                            }
                        }).then(function(t) {}).catch(console.error);
                    }
                }));
            }
        });
    },
    getWorkers: function(t) {
        var e = this;
        o.collection("users").aggregate().addFields({
            matched: a.in([ "$_openid", t ])
        }).match({
            matched: !0
        }).group({
            _id: null,
            workers: a.addToSet({
                _openid: "$_openid",
                avatarUrl: "$avatarUrl",
                authorized: "$authorized",
                nickName: "$nickName",
                realName: "$realName"
            })
        }).end().then(function(o) {
            t = o.list.length >= 1 ? o.list[0].workers : [];
            for (var a = 0; a < t.length; a++) t[a].authorized || (t[a].nickName = "无昵称或未授权微信登录", 
            t[a].avatarUrl = "/images/user-unlogin.png");
            e.setData({
                workers: t
            });
        });
    },
    onHide: function() {
        this.data.watcherCompetition && this.data.watcherCompetition.close();
    },
    onUnload: function() {
        this.data.watcherCompetition && this.data.watcherCompetition.close();
    },
    showActionSheet: function(t) {
        if (this.data.competition.workers.indexOf(e.globalData.openid) >= 0) {
            var o = t.currentTarget.dataset.worker, a = t.currentTarget.dataset.index, i = this;
            wx.showActionSheet({
                itemList: [ "移除该工作人员" ],
                itemColor: "#767676",
                success: function(t) {
                    switch (t.tapIndex) {
                      case 0:
                        i.data.competition.creator == o._openid ? wx.showToast({
                            title: "无法删除创建者本人，或考虑删除比赛",
                            icon: "none",
                            duration: 2500
                        }) : wx.showModal({
                            title: "确认移除该工作人员吗",
                            cancelColor: "#484848",
                            cancelText: "取消",
                            confirmColor: "#FF5A5F",
                            confirmText: "确定",
                            success: function(t) {
                                t.confirm && (wx.showLoading({
                                    title: "正在删除",
                                    mask: !0
                                }), wx.cloud.callFunction({
                                    name: "role",
                                    data: {
                                        competition: i.data.competition,
                                        operation: "deleteRole",
                                        role: "worker",
                                        openidDeleted: o._openid
                                    }
                                }).then(function(t) {
                                    wx.hideLoading(), wx.showToast({
                                        title: "移除工作人员",
                                        icon: "success",
                                        duration: 2e3
                                    }), i.data.workers.splice(a, 1), i.setData({
                                        workers: i.data.workers
                                    }), o._openid == e.globalData.openid && wx.navigateBack({
                                        delta: 2
                                    });
                                }));
                            },
                            fail: function(t) {
                                console.log(t);
                            }
                        });
                    }
                }
            });
        }
    },
    onShareAppMessage: function() {
        return {
            title: "邀请成为工作人员@" + this.data.competition.title,
            path: "/pages/addRole/worker/worker?scene=" + this.data.competition._id
        };
    }
});