import config from "../../../config/config";
import address from "../data/address";

export default {

    data(){
        return {
            province:[],
            city:[],
            area:[],
            vValue:[],
            show: false,  // 是否显示
            use:['province','city','area'],
            footerHeight: config.phoneFooterHeight
        }
    },

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



    methods:{

        open:function(){

            if (!this.show) {

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

                this.show =true;
            }
        },

        /* 点击取消触发 */
        cancel:function(){
            if (this.show) {
                this.show =false;
            }
        },

        /* 点击确认触发 */
        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];
            this.$emit('change',{
                postcode: code,
                code: cCode,
                value: cAddress
            });

            return this.cancel();

        },

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

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

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

            return this.setUseData(draw);

        },

        setUseData(draw){
            for (let key in draw) {
                if (draw.hasOwnProperty(key)) {
                    this[key] = draw[key]
                }
            }
        },

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

            let newValue = [];
            let key = 0;
            let parentItem = address;
            for(let i=0;i<3;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 (e) {

            let newValue = e.detail.value;

            let item ={
                code:'0'
            };

            let change = false;

            let storageI = 0;

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


            for (let i=0;i<3;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;
                }
            }
            this.vValue = newValue;
            if (change) {
                this.changeLevel(item.code,storageI+1);
            }

        },

        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);
                let useValue = this.vValue;
                this.storeValue = storeValue;
                this.changeLevel('0',0);
                this.vValue = [];

                setTimeout(()=>{
                    this.vValue = useValue;
                });

            }

        }

    },

    mounted(){
        return this.setValue(this.value || []);
    }

}