import socket from '$utils/socket';
import {NoticeType} from "$utils/socket/const";

import personalDetail from '$utils/control/personal-detail';

import popup from "$utils/tool/popup";

import followEntrust, {EntrustType} from "$utils/request/entrust";

export default <LibMixins>{

    methods:{

        fetch(obj){
          return this.$request({
              url:'user/get_user_follow_list',
              page:obj.data,
              token:true,
              entrust: followEntrust.getEntrust(EntrustType.follow),
          }).then(function (data){
              return obj.success(data.data);
          }).catch(obj.fail);
        },

        // 取消关注
        cancelFollow(item){

            if (this.$refs.flat) {

                popup.$confirm({
                   title:'是否取消关注?',
                   confirm(){
                       // 设置状态为取消
                       return socket.triggerNotice(NoticeType.follow, {
                           uid: item.uid,
                           status:0
                       });
                   }
                });

            }
        },

        openUserDetail(item){
            return personalDetail.openDetail({
                uid: item.uid
            });
        }

    },

    created() {
        socket.on('my-follow-user',(params)=>{

            if (params.uid && !params.status) {

                this.$request({
                    url:'user/unfollow_user',
                    data:{
                        follow_uid: params.uid
                    },
                    token:true
                });

                this.$refs.flat && this.$refs.flat.flat_delete(params.uid);
            }

            personalDetail.updateCache(params);

        },NoticeType.follow);
    },

    beforeUnmount() {
        socket.off('my-follow-user',NoticeType.follow);
    }

}