import test from '@/utils/test';

export default {

    data(){
      return {
          modal:false,
          useItem:{}
      }
    },

    methods:{
        trigger(item){
            return this[item.trigger] && this[item.trigger](item);
        },

        updateMobile(){
            return this.$router.navigateTo({
                name:'update-mobile'
            })
        },

        openModal(item){

            this.useItem = {
                ...item,
                value: this.user[item.key],
                placeholder:'请输入'+item.label,
                label: '修改'+item.label,
                rules: item.rules || ''
            };

            if (!this.modal) {
                this.modal = true;
            }

        },

        closeModal(){
            if (this.modal) {
                this.modal = false;
            }
        },

        triggerSubmit(){

            test.verificationPromise(this.useItem,true).then((data)=>{
                this.closeModal();

                if(this.user[this.useItem.key] !== data[this.useItem.key]){
                    this.update(data);
                }

            });

        },

        // 上传头像
        uploadAvatar(){
            if (this.uploadStatus) return;
            uni.chooseImage({
                count: 1,
                sizeType:'compressed',
                success: (e)=> {
                    this.$request({
                        url:'common/upload',
                        filePath:e.tempFilePaths[0],
                        name:'file',
                        token:true,
                        loading:'更换中',
                        message:2,
                        failMessage:true,
                        next:({status})=> this.uploadStatus = status
                    }).then((data)=>{
                        if (data.isSuccess) {
                            this.update({
                               avatar:data.data.url
                            },{
                                avatar:data.data.fullurl
                            });
                        }
                    });
                }
            });
        },

        update(data,userData){

            this.updateUserInfo(userData || data);

            return this.$request({
                url:'user/profile',
                data,
                token:true
            })
        }

    }

}