import {InstructionsCacheType} from "$utils/request";

export default <LibMixins>{

    inject:['getRoomInfo','openUserInfo'],

    data(){
      return {
          loadingObject:{},
          firstLoadingObject:{},
          userRankObject:{},
          time:1
      }
    },

    computed:{
        userRank(){
            return this.userRankObject[this.time] || [];
        },
        loadingStatus(){
            return this.firstLoadingObject[this.time] || false
        }
    },

    methods:{
        fetch(updated:boolean=true){
            let time = this.time;

            if (this.firstLoadingObject[time] === undefined) {
                this.firstLoadingObject[time] = true;
            }

            return this.$request({
                url:'room/get_room_rank',
                data:{
                    rid:this.roomInfo.rid,
                    time:time,
                    type:this.type
                },
                next:({status})=> {
                    this.loadingObject[time] =status;
                    if (this.firstLoadingObject[time]) {
                        this.firstLoadingObject[time] = false;
                    }
                },
                cache:{
                    type:InstructionsCacheType.storage,
                    updated
                },
                token:true,
            }).then((data)=>{
                if (data.isSuccess) {
                    data.data = data.data || {};
                    this.userRankObject[time] = data.data.list;

                    if (data.isCache) {
                        return this.fetch(true);
                    }

                }
            });
        },

        switchType(){
            this.time = this.time === 1 ? 3 : 1;
            if (this.userRankObject[this.time] === undefined && !this.loadingObject[this.time]) {
                return this.fetch(false);
            }
        }
    },

    mounted() {
        this.roomInfo = this.getRoomInfo();
        this.fetch(false);
    }

}