import Throttle from '$utils/tool/throttle';
import personalDetail from "$utils/control/personal-detail";
import socket from "$utils/socket";
import {NoticeType} from "$utils/socket/const";
export default {

    data(){
      return {
          control:{
              follow:false
          }
      }
    },

    methods:{

        setFollow(user){
            this.control.follow = !!user.is_follow;
            this._followStatus = this.control.follow;
        },

        toggleFollow(){
            this.control.follow = !this.control.follow;
            personalDetail.updateCache({
                uid: this.uid,
                status: this.control.follow ? 1 : 0
            });

            socket.triggerNotice(NoticeType.follow, {
                uid: this.uid,
                status: this.control.follow ? 1 : 0,
                followUserInfo:this.userInfo
            });

            // 设置是否关注
            return this.triggerFollow();
        },

        triggerFollow(){
            if(this.control.follow !== this._followStatus) {
                this._followStatus = this.control.follow;
                return this.$request({
                    url:this._followStatus ? 'user/follow_user' : '/user/unfollow_user',
                    data:{
                        follow_uid: this.uid
                    },
                    token:true
                });
            }
        }

    },

    created() {
        this.Throttle = new Throttle({
            delay:1000,
            first:false,
            call:this,
            handle: this.triggerFollow
        });

        socket.on('popup-chat-content',(params)=>{

            if (params.uid) {
                let status = !!params.status;
                if (this.control.follow !== status) {
                    this.control.follow = status;
                }
            }

        },NoticeType.follow);

        this.triggerFollow = this.Throttle.supper;
    },

    beforeUnmount() {
        this.Throttle && this.Throttle.destroy();
        socket.off('popup-chat-content');
    }

}