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

wx.cloud.database();

Page({
    data: {
        competitions: "",
        loadmore: "",
        filtrating: !1,
        inputShowed: !1,
        inputVal: "",
        keyword: ""
    },
    onLoad: function(t) {
        this.setData({
            userOpenid: e.globalData.openid
        }), t.nickName && t.openid ? this.getCreatorCompetitions(t.nickName, t.openid) : this.getActive();
    },
    getActive: function() {
        var e = this;
        wx.showLoading({
            title: "正在加载",
            mask: !0
        }), wx.cloud.callFunction({
            name: "getActive"
        }).then(function(i) {
            console.log(i), wx.hideLoading(), wx.stopPullDownRefresh();
            for (var o = new Date(), a = i.result.competitions, n = 0; n < a.length; n++) a[n].startTimeStr = t.formatTime("full", new Date(a[n].startTime)), 
            a[n].vipExpiryTime && (a[n].vipExpiryTime = new Date(a[n].vipExpiryTime) < o ? "" : t.formatTime("full", new Date(a[n].vipExpiryTime))), 
            a[n].proExpiryTime && (a[n].rechargeable = new Date(a[n].proExpiryTime) > o, a[n].proExpiryTime = t.formatTime("full", new Date(a[n].proExpiryTime)));
            a.length >= 1 ? wx.setNavigationBarTitle({
                title: "今日赛事概况(" + a.length + ")"
            }) : wx.setNavigationBarTitle({
                title: "今日赛事概况"
            }), e.setData({
                competitions: a,
                loadmore: 0 == a.length ? "今日没有比赛" : "全部加载"
            });
        });
    },
    changeOrderBy: function(t) {
        var e = this.data.competitions;
        wx.pageScrollTo({
            scrollTop: 0,
            duration: 300
        });
        var i = this;
        wx.showActionSheet({
            alertText: "切换排序",
            itemList: [ "比赛开始时间", "主办访问数", "评委访问数", "比赛状态" ],
            success: function(t) {
                switch (wx.showLoading({
                    title: "切换排序",
                    mask: !0
                }), t.tapIndex) {
                  case 0:
                    e.sort(function(t, e) {
                        return new Date(e.startTime).getTime() - new Date(t.startTime).getTime();
                    });
                    break;

                  case 1:
                    e.sort(function(t, e) {
                        return e.workerVisitTotal - t.workerVisitTotal;
                    });
                    break;

                  case 2:
                    e.sort(function(t, e) {
                        return e.juryVisitTotal - t.juryVisitTotal;
                    });
                    break;

                  case 3:
                    e.sort(function(t, e) {
                        return t.status.localeCompare(e.status);
                    });
                }
            },
            complete: function(t) {
                wx.hideLoading(), i.setData({
                    competitions: e
                });
            }
        });
    },
    searchCreator: function(t) {
        this.getCreatorCompetitions(t.currentTarget.dataset.nickname, t.currentTarget.dataset.openid);
    },
    getCreatorCompetitions: function(e, i) {
        var o = this;
        wx.showLoading({
            title: "正在加载",
            mask: !0
        }), this.setData({
            inputVal: e,
            inputShowed: !0
        }), wx.cloud.callFunction({
            name: "getActive",
            data: {
                creatorOpenid: i
            }
        }).then(function(e) {
            wx.hideLoading(), wx.pageScrollTo({
                scrollTop: 0,
                duration: 300
            });
            for (var i = new Date(), a = e.result.competitions, n = 0; n < a.length; n++) a[n].startTime = t.formatTime("full", new Date(a[n].startTime)), 
            a[n].vipExpiryTime && (a[n].vipExpiryTime = new Date(a[n].vipExpiryTime) < i ? "" : t.formatTime("full", new Date(a[n].vipExpiryTime))), 
            a[n].proExpiryTime && (a[n].rechargeable = new Date(a[n].proExpiryTime) > i);
            a.length >= 1 && wx.setNavigationBarTitle({
                title: "筛选(" + a.length + ")"
            }), o.setData({
                competitions: a,
                loadmore: "全部加载"
            });
        });
    },
    showInput: function() {
        this.setData({
            inputShowed: !0
        });
    },
    hideInput: function() {
        wx.pageScrollTo({
            scrollTop: 0,
            duration: 300
        }), this.setData({
            competitions: [],
            loadmore: "",
            keyword: "",
            inputVal: "",
            inputShowed: !1
        }), this.getActive();
    },
    clearInput: function() {
        this.setData({
            inputVal: ""
        });
    },
    inputTyping: function(t) {
        this.setData({
            inputVal: t.detail.value
        });
    },
    inputFinish: function(e) {
        var i = this;
        "" == e.detail.value.replace(/\s*/g, "").length ? wx.showToast({
            title: "请输入关键词",
            icon: "none",
            duration: 1e3
        }) : (this.data.keyword = e.detail.value.replace(/\s*/g, ""), wx.showLoading({
            title: "正在加载",
            mask: !0
        }), wx.cloud.callFunction({
            name: "getActive",
            data: {
                keyword: this.data.keyword
            }
        }).then(function(e) {
            wx.hideLoading(), wx.pageScrollTo({
                scrollTop: 0,
                duration: 300
            });
            for (var o = new Date(), a = e.result.competitions, n = 0; n < a.length; n++) a[n].startTime = t.formatTime("full", new Date(a[n].startTime)), 
            a[n].vipExpiryTime && (a[n].vipExpiryTime = new Date(a[n].vipExpiryTime) < o ? "" : t.formatTime("full", new Date(a[n].vipExpiryTime)));
            a.length >= 1 && wx.setNavigationBarTitle({
                title: "搜索(" + a.length + ")"
            }), i.setData({
                competitions: a,
                loadmore: "全部加载"
            });
        }));
    },
    refresh: function() {
        wx.pageScrollTo({
            scrollTop: 0,
            duration: 300
        }), this.getActive();
    },
    onPullDownRefresh: function() {
        this.setData({
            userOpenid: e.globalData.openid
        }), this.data.inputShowed ? wx.stopPullDownRefresh() : this.getActive();
    },
    copyID: function(t) {
        var e = t.currentTarget.dataset.id;
        wx.setClipboardData({
            data: "_id:" + e,
            success: function(t) {
                wx.getClipboardData({
                    success: function(t) {}
                });
            }
        });
    },
    showActionSheet: function(t) {
        var e = t.currentTarget.dataset.competition._id, i = this;
        wx.showActionSheet({
            itemList: [ "标记为正常", "标记为开放", "标记为不开放", "标记为宝职", "重置评委邀请码字段" ],
            success: function(t) {
                switch (t.tapIndex) {
                  case 0:
                    wx.showLoading({
                        title: "正在提交",
                        mask: !0
                    }), wx.cloud.callFunction({
                        name: "competition",
                        data: {
                            competition: {
                                _id: e,
                                status: "正常"
                            },
                            operation: "update"
                        }
                    }).then(function(t) {
                        wx.hideLoading(), wx.showToast({
                            title: "标记为正常状态",
                            icon: "success",
                            duration: 1500
                        }), i.getActive();
                    });
                    break;

                  case 1:
                    wx.showLoading({
                        title: "正在提交",
                        mask: !0
                    }), wx.cloud.callFunction({
                        name: "competition",
                        data: {
                            competition: {
                                _id: e,
                                note: ""
                            },
                            operation: "update"
                        }
                    }).then(function(t) {
                        wx.hideLoading(), wx.showToast({
                            title: "标记为开放状态",
                            icon: "success",
                            duration: 1500
                        }), i.getActive();
                    });
                    break;

                  case 2:
                    wx.showLoading({
                        title: "正在提交",
                        mask: !0
                    }), wx.cloud.callFunction({
                        name: "competition",
                        data: {
                            competition: {
                                _id: e,
                                note: "不开放"
                            },
                            operation: "update"
                        }
                    }).then(function(t) {
                        wx.hideLoading(), wx.showToast({
                            title: "标记为不开放",
                            icon: "success",
                            duration: 1500
                        }), i.getActive();
                    });
                    break;

                  case 3:
                    wx.showLoading({
                        title: "正在提交",
                        mask: !0
                    }), wx.cloud.callFunction({
                        name: "competition",
                        data: {
                            competition: {
                                _id: e,
                                note: "宝职"
                            },
                            operation: "update"
                        }
                    }).then(function(t) {
                        wx.hideLoading(), wx.showToast({
                            title: "标记为宝职比赛",
                            icon: "success",
                            duration: 1500
                        }), i.getActive();
                    });
                    break;

                  case 4:
                    wx.showLoading({
                        title: "正在提交",
                        mask: !0
                    }), wx.cloud.callFunction({
                        name: "competition",
                        data: {
                            competition: {
                                _id: e,
                                inviteJuryWxacode: ""
                            },
                            operation: "update"
                        }
                    }).then(function(t) {
                        wx.hideLoading(), wx.showToast({
                            title: "重置评委邀请码字段",
                            icon: "success",
                            duration: 1500
                        });
                    });
                }
            },
            fail: function(t) {}
        });
    },
    vipActionSheet: function(t) {
        var e = t.currentTarget.dataset.competition._id, i = t.currentTarget.dataset.competition.creator, o = this;
        wx.showActionSheet({
            itemList: [ "Pro", "unPro", "Settings", "VP-1", "VP-100" ],
            success: function(t) {
                switch (t.tapIndex) {
                  case 0:
                    wx.showLoading({
                        title: "正在提交",
                        mask: !0
                    }), wx.cloud.callFunction({
                        name: "updateVipV2",
                        data: {
                            competition_id: e,
                            operation: "updateCompetition"
                        }
                    }).then(function(t) {
                        wx.hideLoading(), wx.showToast({
                            title: "Pro",
                            icon: "success",
                            duration: 1500
                        }), o.getActive();
                    });
                    break;

                  case 1:
                    wx.showLoading({
                        title: "正在提交",
                        mask: !0
                    }), wx.cloud.callFunction({
                        name: "updateVipV2",
                        data: {
                            competition_id: e,
                            operation: "updateCompetitionUnPro"
                        }
                    }).then(function(t) {
                        wx.hideLoading(), wx.showToast({
                            title: "Pro",
                            icon: "success",
                            duration: 1500
                        }), o.getActive();
                    });
                    break;

                  case 2:
                    wx.navigateTo({
                        url: "/pages/dashboard/active/compSettings/compSettings?id=" + e
                    });
                    break;

                  case 3:
                    wx.showModal({
                        content: "vip",
                        confirmColor: "#FF5A5F",
                        confirmText: "确认",
                        cancelColor: "#484848",
                        cancelText: "取消",
                        success: function(t) {
                            t.confirm && (wx.showLoading({
                                title: "正在提交",
                                mask: !0
                            }), wx.cloud.callFunction({
                                name: "updateVipV2",
                                data: {
                                    creator_openid: i,
                                    operation: "updateUser",
                                    years: 1
                                }
                            }).then(function(t) {
                                wx.hideLoading(), wx.showToast({
                                    title: "VP-1",
                                    icon: "success",
                                    duration: 1500
                                }), o.getActive();
                            }));
                        },
                        fail: function(t) {}
                    });
                    break;

                  case 4:
                    wx.showModal({
                        content: "vip",
                        confirmColor: "#FF5A5F",
                        confirmText: "确认",
                        cancelColor: "#484848",
                        cancelText: "取消",
                        success: function(t) {
                            t.confirm && (wx.showLoading({
                                title: "正在提交",
                                mask: !0
                            }), wx.cloud.callFunction({
                                name: "updateVipV2",
                                data: {
                                    creator_openid: i,
                                    operation: "updateUser",
                                    years: 100
                                }
                            }).then(function(t) {
                                wx.hideLoading(), wx.showToast({
                                    title: "VP-100",
                                    icon: "success",
                                    duration: 1500
                                }), o.getActive();
                            }));
                        },
                        fail: function(t) {}
                    });
                }
            },
            fail: function(t) {}
        });
    },
    onShareAppMessage: function() {
        return {
            title: "今日赛事概况",
            path: "/pages/dashboard/active/active",
            imageUrl: "/images/sharePic.png"
        };
    }
});