import address from '../data/address';
export default {

    data(){
        return {
            data:{
                province:[],
                city:[],
                area:[],
            },
            province:[],
            city:[],
            area:[],
            vValue:[]
        }
    },

    watch:{
        value:{
            handler:function (value) {
                return this.setValue(value);
            },
            immediate:true
        }
    },

    methods:{

        open:function(){
            if (this.storeValueTarget && this.storeValueTarget.join('') !== this.vValue.join('')) {
                this.vValue = this.storeValueTarget;
                this.changeLevel('0',0);
            }
        },

        /* 点击确认触发 */
        confirm:function(){

            let code = '0';
            let cCode = [];
            let cAddress = [];
            this.vValue.map((item,index)=>{

                code = this.getCode(index,code);
                if (address[code]&&address[code][item]) {
                    cAddress.push(address[code][item].name);
                    code = address[code][item].code;
                    cCode.push(code);
                }

            });
            this.storeValue = cAddress.join('');
            this.storeValueTarget = [...this.vValue];

            if (this.$attrs.input) {
                this.$emit('input',cAddress);
            }

            if (this.$attrs.onChange) {
                this.$emit('change',{
                    postcode: code,
                    code: cCode,
                    value: cAddress
                });
            }


        },

        /* 更改展示数据 */
        changeLevel:function(code = '0',level=0){


            let parentCode = code;
            let nowCode = '';
            for (let i=level;i<this.use.length;i++) {
                nowCode = this.getCode(i,parentCode);

                if (address[nowCode]) {
                    this.data[this.use[i]] = address[nowCode] || [];
                    if (address[nowCode][this.vValue[i] || 0]){
                        parentCode = address[nowCode][this.vValue[i] || 0].code || '';
                    }else {
                        parentCode = '';
                    }
                } else {
                    this.data[this.use[i]] = [];
                }
            }

        },


        /* 获取默认选择的地址 */
        getAddressValue:function (value) {

            let newValue = [];
            let key = 0;
            let parentItem = address;
            for(let i=0;i<this.use.length;i++) {


                if (value[i]) {
                    key = i===0?this.use[i]:parentItem.code+':'+this.use[i];

                    newValue[i] = address[key].indexOf(value[i]);
                    if (newValue[i] < 0) newValue[i] = 0;
                } else {
                    newValue[i] = 0;
                }

                if (i === 0) {
                    parentItem = address['0:0'][newValue[i]];
                } else {
                    parentItem = address[this.getCode(i,parentItem.code)][newValue[i]];
                }

            }

            return newValue;
        },

        changeColumn:function (key,index,value) {

            let newValue = [...this.vValue];

            newValue[index] = value;

            let item ={
                code:'0'
            };

            let change = false;

            let storageI = 0;

            let oldValue = [...this.vValue];


            for (let i=0;i<this.use.length;i++) {

                if (change) {
                    newValue[i] = 0;
                }else {
                    item = this.getItem(this.getCode(i,item.code),newValue[i]);
                }

                if (oldValue[i] !== newValue[i]) {
                    if (!change) {
                        storageI = i;
                    }
                    change = true;
                }
            }


            if (change) {
                this.vValue = newValue;
                this.changeLevel(item.code,storageI+1);
                this.confirm();
            }

        },

        getCode:function(level,code){
            return level+':'+code;
        },

        getItem:function (code='0:0',index) {

            if (address[code]) {
                return address[code][index];
            } else {
                return {
                    code:-1
                };
            }


        },

        setValue:function (value) {
            let storeValue = value.join('');
            if (this.storeValue !== storeValue) {
                this.vValue = this.getAddressValue(value);
                this.storeValue = storeValue;
                this.changeLevel('0',0);
                this.confirm();
            }

        }

    },

    created(){
        this.open();

    }

}