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

Page({
    data: {
        users: [],
        filtrating: !1,
        inputShowed: !1,
        inputVal: "",
        keyword: ""
    },
    onLoad: function(e) {
        this.setData({
            userOpenid: t.globalData.openid
        });
    },
    showInput: function() {
        this.setData({
            inputShowed: !0
        });
    },
    hideInput: function() {
        wx.pageScrollTo({
            scrollTop: 0,
            duration: 300
        }), this.setData({
            users: [],
            loadmore: "",
            keyword: "",
            inputVal: "",
            inputShowed: !1
        });
    },
    clearInput: function() {
        this.setData({
            inputVal: "",
            users: [],
            loadmore: ""
        });
    },
    inputTyping: function(e) {
        this.setData({
            inputVal: e.detail.value,
            users: []
        });
    },
    inputFinish: function(e) {
        if ("" == e.detail.value.replace(/\s*/g, "").length) wx.showToast({
            title: "请输入关键词",
            icon: "none",
            duration: 1e3
        }); else {
            var t = e.detail.value.replace(/\s*/g, "");
            this.data.keyword = t, this.searchUsers(t), wx.pageScrollTo({
                scrollTop: 0,
                duration: 300
            });
        }
    },
    searchUsers: function(i) {
        var n = this;
        wx.showLoading({
            title: "正在搜索",
            mask: !0
        });
        var s = this.data.users.length;
        a.collection("users").where(o.or([ {
            nickName: a.RegExp({
                regexp: i,
                options: "i"
            })
        }, {
            realName: a.RegExp({
                regexp: i,
                options: "i"
            })
        } ])).orderBy("createTime", "desc").field({
            _openid: !0,
            nickName: !0,
            realName: !0,
            avatarUrl: !0,
            vipExpiryTime: !0,
            vipType: !0,
            createTime: !0,
            authorized: !0
        }).skip(s).get().then(function(a) {
            console.log("searchUsers", a);
            var o = a.data;
            if ("oRnCI5JYWT9dmVycOnBjYaX1Yx7k" == t.globalData.openid && "oRnCI5GgJ8Kc3sGye-ungAdL7r4U" == t.globalData.openid || 0 != o.length) {
                wx.hideLoading();
                for (var i = new Date(), r = 0; r < o.length; r++) o[r].createTime = e.formatTime("full", new Date(o[r].createTime)), 
                o[r].vipExpiryTime && (o[r].vipExpiryTime = new Date(o[r].vipExpiryTime) < i ? "" : e.formatTime("full", new Date(o[r].vipExpiryTime)));
                n.setData({
                    users: 0 == s ? o : n.data.users.concat(o),
                    loadmore: 20 == o.length ? "上拉加载更多" : "全部加载"
                });
            } else wx.showToast({
                title: "搜索结果为空",
                icon: "none",
                duration: 1e3
            }), n.setData({
                loadmore: "搜索结果为空",
                users: []
            });
        }).catch(function(e) {
            wx.hideLoading(), console.log(e);
        });
    },
    vipActionSheet: function(e) {
        var t = e.currentTarget.dataset.openid, a = this;
        wx.showActionSheet({
            itemList: [ "VP-1", "VP-100" ],
            success: function(e) {
                switch (e.tapIndex) {
                  case 0:
                    wx.showModal({
                        content: "VP",
                        confirmColor: "#FF5A5F",
                        confirmText: "确认",
                        cancelColor: "#484848",
                        cancelText: "取消",
                        success: function(e) {
                            e.confirm && (wx.showLoading({
                                title: "正在提交",
                                mask: !0
                            }), wx.cloud.callFunction({
                                name: "updateVipV2",
                                data: {
                                    creator_openid: t,
                                    years: 1,
                                    operation: "updateUser"
                                }
                            }).then(function(e) {
                                wx.hideLoading(), wx.showToast({
                                    title: "VP-1",
                                    icon: "success",
                                    duration: 1500
                                }), a.searchUsers(a.data.keyword);
                            }));
                        },
                        fail: function(e) {}
                    });
                    break;

                  case 1:
                    wx.showModal({
                        content: "VP",
                        confirmColor: "#FF5A5F",
                        confirmText: "确认",
                        cancelColor: "#484848",
                        cancelText: "取消",
                        success: function(e) {
                            e.confirm && (wx.showLoading({
                                title: "正在提交",
                                mask: !0
                            }), wx.cloud.callFunction({
                                name: "updateVipV2",
                                data: {
                                    creator_openid: t,
                                    years: 100,
                                    operation: "updateUser"
                                }
                            }).then(function(e) {
                                wx.hideLoading(), wx.showToast({
                                    title: "VP-100",
                                    icon: "success",
                                    duration: 1500
                                }), a.searchUsers(a.data.keyword);
                            }));
                        },
                        fail: function(e) {}
                    });
                }
            },
            fail: function(e) {}
        });
    },
    onReachBottom: function() {
        "上拉加载更多" == this.data.loadmore && (this.setData({
            loadmore: "正在加载"
        }), this.searchUsers(this.data.keyword));
    }
});