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

Page({
    data: {
        competition_id: "",
        competition: {},
        juriesAccounts: [],
        loadmore: "",
        contactServiceView: !1,
        config_rechargeable: !1,
        user_vip: !1
    },
    onLoad: function(e) {
        var n = this;
        this.data.competition_id = e.id, this.getCompetition(e.id), o.collection("_config").doc("config").field({
            rechargeable: !0
        }).get().then(function(e) {
            n.data.config_rechargeable = e.data.rechargeable;
        }).catch(function(e) {
            console.error(e);
        }), o.collection("users").where({
            _openid: t.globalData.openid,
            vipExpiryTime: i.gt(o.serverDate())
        }).count().then(function(e) {
            1 == e.total && (n.data.user_vip = !0);
        }).catch(function(e) {
            console.log(e);
        });
    },
    getCompetition: function(i) {
        var n = this;
        o.collection("competitions").doc(i).field({
            title: !0,
            workers: !0,
            juries: !0,
            rechargeable: !0,
            status: !0,
            proExpiryTime: !0,
            settings: !0,
            juriesGroups: !0
        }).get().then(function(e) {
            var o = e.data, i = new Date();
            o.proExpiryTime && (o.rechargeable = new Date(o.proExpiryTime) > i), n.data.competition = o;
            var c = "oRnCI5JYWT9dmVycOnBjYaX1Yx7k" == t.globalData.openid || n.data.competition.workers.indexOf(t.globalData.openid) >= 0;
            n.setData({
                competition: o,
                isWorker: c
            }), n.getJuriesAccounts();
        }).catch(function(t) {
            e.competitionNoExistShowModal(t.errCode);
        });
    },
    getJuriesAccounts: function() {
        var e = this;
        o.collection("juriesAccounts").aggregate().match({
            competition_id: this.data.competition._id
        }).group({
            _id: null,
            juriesAccounts: n.addToSet({
                _id: "$_id",
                name: "$name",
                password: "$password",
                user_openid: "$user_openid"
            })
        }).end().then(function(t) {
            console.log(t);
            var o = t.list.length >= 1 ? t.list[0].juriesAccounts : [];
            o.sort(function(e, t) {
                return e.name - t.name;
            }), wx.hideLoading(), wx.stopPullDownRefresh(), 0 == o.length ? e.setData({
                loadmore: "暂未导入评委账号"
            }) : e.setData({
                juriesAccounts: o,
                loadmore: "全部加载"
            });
        }).catch(function(e) {
            console.error(e);
        });
    },
    importJuriesAccounts: function(e) {
        var t = this;
        wx.showModal({
            title: "选择Excel表文件导入添加",
            content: "Excel表无需第1行标题，第1列为评委姓名，第2列为密码。先把Excel表发送到手机微信的“文件传输助手”方可选择导入，电脑端不能使用导入功能。",
            cancelColor: "#484848",
            cancelText: "取消",
            confirmColor: "#FF5A5F",
            confirmText: "选择文件",
            success: function(e) {
                e.confirm && ((t.data.competition.rechargeable || t.data.config_rechargeable || t.data.user_vip) && "example" != t.data.competition._id ? wx.chooseMessageFile({
                    count: 1,
                    type: "file",
                    success: function(e) {
                        var o = e.tempFiles[0].name.split(".");
                        if ("xlsx" == o[1] || "xls" == o[1]) {
                            var i = e.tempFiles[0].path;
                            wx.showLoading({
                                title: "上传Excel表",
                                mask: !0
                            }), wx.cloud.uploadFile({
                                cloudPath: "juriesAccountsExcels/" + t.data.competition._id + ".xlsx",
                                filePath: i
                            }).then(function(e) {
                                wx.showLoading({
                                    title: "导入添加",
                                    mask: !0
                                });
                                var o = e.fileID;
                                wx.cloud.callFunction({
                                    name: "importJuriesAccounts",
                                    data: {
                                        fileID: o,
                                        competition_id: t.data.competition._id
                                    }
                                }).then(function(e) {
                                    wx.showToast({
                                        title: "完成添加",
                                        icon: "success",
                                        duration: 1500
                                    }), t.data.juriesAccounts = [], t.getJuriesAccounts(), wx.cloud.deleteFile({
                                        fileList: [ o ]
                                    }).then(function(e) {});
                                });
                            }).catch(function(e) {});
                        } else wx.showToast({
                            title: "不是Excel文件",
                            icon: "none",
                            duration: 1500
                        });
                    }
                }) : "example" == t.data.competition._id ? wx.showToast({
                    title: "比赛示例不能添加评委账号",
                    icon: "none",
                    duration: 1500
                }) : t.setData({
                    contactServiceView: !0
                }));
            },
            fail: function(e) {
                console.log(e);
            }
        });
    },
    showActionSheet: function(e) {
        console.log(e);
        var t = e.currentTarget.dataset.idx, i = e.currentTarget.dataset.id, n = e.currentTarget.dataset.user_openid ? e.currentTarget.dataset.user_openid : "", c = this;
        wx.showActionSheet({
            itemList: [ "解除微信绑定(同步删除评委)", "删除该账号" ],
            success: function(e) {
                if ("example" == c.data.competition._id) wx.showToast({
                    title: "比赛示例不能使用此功能",
                    icon: "none",
                    duration: 3e3
                }); else switch (e.tapIndex) {
                  case 0:
                    o.collection("juriesAccounts").doc(i).update({
                        data: {
                            user_openid: ""
                        }
                    }).then(function(e) {
                        console.log("解除微信绑定", e), wx.showToast({
                            title: "解除微信绑定",
                            icon: "success",
                            duration: 1500
                        }), c.data.juriesAccounts[t].user_openid = "", c.setData({
                            juriesAccounts: c.data.juriesAccounts
                        }), wx.cloud.callFunction({
                            name: "role",
                            data: {
                                competition: {
                                    _id: c.data.competition._id,
                                    juriesGroups: c.data.competition.juriesGroups
                                },
                                operation: "deleteRole",
                                role: "jury",
                                openidDeleted: n
                            }
                        }).then(function(e) {
                            console.log("移除评委", e);
                        });
                    }).catch(function(e) {
                        console.error(e);
                    });
                    break;

                  case 1:
                    o.collection("juriesAccounts").doc(i).remove().then(function(e) {
                        console.log("删除该账号", e), wx.showToast({
                            title: "删除账号",
                            icon: "success",
                            duration: 1500
                        }), c.data.juriesAccounts.splice(t, 1), c.setData({
                            juriesAccounts: c.data.juriesAccounts
                        });
                    }).catch(function(e) {
                        console.error(e);
                    }), n && wx.cloud.callFunction({
                        name: "role",
                        data: {
                            competition: {
                                _id: c.data.competition._id,
                                juriesGroups: c.data.competition.juriesGroups
                            },
                            operation: "deleteRole",
                            role: "jury",
                            openidDeleted: n
                        }
                    }).then(function(e) {
                        console.log("移除评委", e);
                    });
                }
            },
            fail: function(e) {}
        });
    },
    onPullDownRefresh: function() {
        this.data.juriesAccounts = [], this.getJuriesAccounts();
    }
});