import {
    getOtherUser,
    setFans
} from '~/api/user';
import event from '~/mixins/event'
import reachBottom from '~/mixins/reachBottom';
Page({
    behaviors: [reachBottom, event],
    data: {
        text: '',
        placeholderText:'查找昵称/学号/手机号',
        historySearch: [],
        localUid: wx.getStorageSync('uid')
    },
    onShow() {
        // isEachOther 是否互相关注 isFans是否被关注    isFansOther是否关注别人
        this.setData({
            historySearch: wx.getStorageSync('userSearch')
        });
        this.resetData();
    },
    setSearch({
        detail
    }) {
        if (!detail.value) {
            this.setData({
                nullList: false,
                list: []
            });
        }
        this.setData({
            text: detail.value
        });
    },
    searchUser({
        currentTarget
    }) {
        if (currentTarget.dataset.text) {
            this.setData({
                text: currentTarget.dataset.text
            });
        }
        if (!this.data.text) {
            this.setData({
                list: []
            });
            return;
        }
        this.resetData();
        if (!this.data.historySearch.includes(this.data.text)) {
            this.setData({
                historySearch: [this.data.text, ...this.data.historySearch].slice(0, 20)
            });
        }
        wx.setStorageSync('userSearch', this.data.historySearch);
    },
    deleteHistory({
        currentTarget
    }) {
        let newList = this.data.historySearch.filter(item => {
            return item != currentTarget.dataset.text;
        });
        this.setData({
            historySearch: newList.slice(0, 20)
        });
        wx.setStorageSync('userSearch', this.data.historySearch);
    },
    clearHistory() {
        wx.showModal({
            title: '温馨提示',
            content: '历史记录清除后无法恢复,是否清除全部记录',
            success: res => {
                if (res.confirm) {
                    this.setData({
                        historySearch: []
                    });
                    wx.setStorageSync('search', this.data.historySearch);
                }
            }
        });
    },
    loadMore() {
        if (!this.data.text) {
            return;
        }
        this.getData(getOtherUser, {
            query: this.data.text
        });
    },
    jumpUserInfo({
        currentTarget
    }) {
        let uid = currentTarget.dataset.uid;
        wx.navigateTo({
            url: `/pages/personal/index?uid=${uid}`
        });
    },
    async setFans({
        currentTarget
    }) {
        if (!currentTarget.dataset.iseachother) {
            await setFans({
                uid: currentTarget.dataset.uid
            });
            let listCopy = JSON.parse(JSON.stringify(this.data.list));
            listCopy.forEach(item => {
                if (item.uid == currentTarget.dataset.uid) {
                    item.isEachOther = true;
                }
            });
            this.setData({
                list: listCopy
            });
            wx.showToast({
                title: '已关注',
                icon: 'none'
            });
        }
    },
    sendMsg({
        currentTarget
    }) {
        let user = currentTarget.dataset.user
        let {
            nickName,
            eid,
            uid
        } = user
        if (this.data.localUid == uid) {
            return wx.showToast({
                title: '不可以给自己发私信哦~',
                icon: 'none'
            })
        }
        wx.navigateTo({
            url: `/pages/chat/index?title=${nickName||eid}&uid=${uid}`,
        })
    },
    clipboar({
        currentTarget
    }) {
        wx.setClipboardData({
            data: currentTarget.dataset.eid,
            success: function (res) { //成功回调函数
                wx.showToast({
                    title: '已复制',
                    icon: "none"
                })
            }
        })
    },
    cleanPlaceholder() {
        this.setData({
            placeholderText: ''
        })
    },
    setPlaceholder() {
        this.setData({
            placeholderText: '请输入搜索内容'
        })
    },
    onReachBottom() {
        this.loadMore();
    }
});