import {FlatListReload} from "$components/flat-list/const";
import followEntrust, {EntrustType} from "$utils/request/entrust";
import socket from "$utils/socket";
import personalDetail from "$utils/control/personal-detail";
import {NoticeType} from "$utils/socket/const";

export default <LibMixins>{

    inject:['openUserContent'],

    methods:{

        fetch(obj){
            let key = 'get_'+this.type;
            if (this[key]) {
                return this[key](obj).then(function (data){
                    return obj.success(data.data);
                }).catch(obj.fail);
            } else {
                setTimeout(()=> obj.fail(),0);
            }

        },

        // 获取关注列表
        get_follow(obj){
            return this.$request({
                url:'user/get_user_follow_list',
                page:obj.data,
                token:true,
                entrust: followEntrust.getEntrust(EntrustType.follow),
            })
        },

        // 获取粉丝列表
        get_fans(obj){
            return this.$request({
                url:'user/hx_get_user_fans_list',
                page:obj.data,
                entrust: followEntrust.getEntrust(EntrustType.fans),
                token:true
            })
        },

        triggerOpenUserContent(item){

            if (item.uid !== this.uid) {
                // 获取
                let conversation = socket.getConversation(item.uid);
                // 清楚标记
                conversation.read();

                return this.openUserContent(item.uid,item.pid || 0,item);
            }

        }

    },

    created() {
        if (this.type === 'follow') {
            socket.on('popup-chat-list',(params)=>{

                if (params.uid) {
                    if (params.status) {
                        this.$refs.flat && this.$refs.flat.flat_unshift(params.uid,params.followUserInfo);
                    } else {
                        this.$refs.flat && this.$refs.flat.flat_delete(params.uid);
                    }
                }

                personalDetail.updateCache(params);

            },NoticeType.follow);
        }

    },

    beforeUnmount() {
        if (this.type === 'follow') {
            socket.off('popup-chat-list',NoticeType.follow);
        }
    }

}
