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

export default <LibMixins>{

    data(){
        return {
            photoWallNumber:4
        }
    },

    methods:{

        updateAvatar(obj){

            let data = this.getInfoValues();

            data.head_pic = obj.data;

            return this.$request({
                url:'user/modify_user_info',
                data,
                token:true,
                message:true
            }).then((data)=>{
                if (data.isSuccess) {
                    return obj.success();
                } else {
                    return obj.fail();
                }
            }).catch(obj.fail);

        },

        updateVoice(obj){
            return this.updateUserInfo(obj,{
                voicefile: obj.data
            });
        },

        // 队列
        updatePhotoWallPool(index,obj){

            if (this.updateUserWillPool.length > 0) {
                this.updateUserWillPool.push({
                    index,obj
                });
            } else {
                return this.updatePhotoWall(index,obj);
            }

        },

        updatePhotoWall(index,obj){

            let userPhotoWall = JSON.parse(JSON.stringify(this.$store.getters.photoWall || []));

            userPhotoWall[index] = obj.data;

            return this.updateUserInfo(obj,{
                images:userPhotoWall.join(',')
            },()=>{

                if (this.updateUserWillPool.length > 0) {
                    let resultData = this.updateUserWillPool.unshift();

                    return this.updatePhotoWall(...resultData);

                }

            });

        },

        updateUserInfo(obj,resultData,callback?:Function){
            return this.$request({
                url:'hxuser/profile',
                token:true,
                data:resultData,
                message:InstructionsMessageType.other
            }).then((data)=>{
                callback && callback();
                if (data.isSuccess) {
                    this.user = resultData;
                    return obj.success();
                } else {
                    return obj.fail();
                }
            }).catch(()=>{
                callback && callback();
                return obj.fail();
            });
        }

    },

    created(){
        this.updateUserWillPool = [];
    }

}