import infoData from '../data/info';

import { LibComponent } from "$const/component";

import $utils from '$utils/tool/utils';

import verification from '$utils/verification';

export default <LibMixins>{

    data(){
        return {
            infoData,
            infoDataValue:{},
            infoEdit:false,
            LibComponent
        }
    },

    methods:{

        getInfoValue(item,format:boolean=true){
            if (format && item.format) {
                return item.format(this.getInfoValue(item,false),item);
            } else if (item.type === 'getters') {
                return  this.$store.getters[item.key];
            } else {
                return  this.user[item.key];
            }
        },

        setInfoData(){

            let infoDataValue = {};

            this.infoData.map((item,index)=>{
                infoDataValue[item.key] = {
                    label: this.getInfoValue(item,true),
                    value: this.getInfoValue(item,false)
                }
            });

            this.infoDataValue = infoDataValue;
        },

        getInfoValues():Record<string, any>{
            let infoDataValue = {};
            this.infoData.map((item,index)=>{
                infoDataValue[item.key] = this.getInfoValue(item,false)
            });
            return infoDataValue;
        },

        changeValue(item,value){

            if (typeof value === 'object') {
                value = value.format('YYYY-MM-DD');

                if (this.infoDataValue.constellation) {
                    this.infoDataValue.constellation.value = $utils.constellation(value);
                }


            }

            if (this.infoDataValue[item.key]) {
                this.infoDataValue[item.key].value = value;
            }

        },

        changeAddress(item,address){
            let value = address.value[address.value.length -1];
            if (value && this.infoDataValue[item.key] !== value) {
                this.infoDataValue[item.key].value = value;
            }
        },

        // 转变加载模式
        switchEditMode(){

            if (!this.infoEdit) {
                this.infoEdit = true;
                this.storageData = JSON.stringify(this.getInfoValues());
            } else {

                // 设置校验data
                let resultData = [];
                this.infoData.map((item,index)=>{
                    resultData.push({
                        rules: item.rules,
                        placeholder:item.placeholder,
                        key: item.key,
                        exportKey: item.exportKey,
                        value: this.infoDataValue[item.key].value
                    });
                });

                verification.verificationPromise(resultData,true).then((data)=>{

                    if (JSON.stringify(data) !== this.storageData) {
                        this.$request({
                            url:'user/modify_user_info',
                            data,
                            token:true,
                            loading:'保存中',
                            message:true,
                            failMessage:true
                        }).then((resultData)=>{
                            if (resultData.isSuccess) {
                                this.exitInfoEdit();
                                this.setInfoData();
                            }
                        }).catch(()=> this.exitInfoEdit());
                    } else {
                        this.exitInfoEdit();
                    }

                });
            }

        },

        exitInfoEdit(){
            if (this.infoEdit) {
                this.infoEdit = false;
            }
        }

    },

    created(){
        // 设置values
        this.setInfoData();
    }

}