<template>
	<el-cascader
		:options="options"
        :props="props"
		ref="cascaderAddr"
        v-model="selectedOptions"
		@change="handleChange"
        placeholder= '请选择'
        >
 	</el-cascader>
</template>

<script>
// let pca = require('../../../utils/pca-code.json')
// import pca from "@/utils/pca-code.json"

export default {
    props: {
        isValue:'',
        pca:{
            type: Array,
            default: [],
        },
    },
    data () {
      return {
        area:'',
        selectedOptions:this.isValue != undefined ? this.isValue.split('/') : [],
        options: this.getTreeData(this.pca),
        props: {
          value: 'name',
          label: 'name'
        }
      }
    },

    watch: {
        isValue(val){
            this.selectedOptions = (this.isValue != undefined ? this.isValue.split('/') : [])
        },
    },
    methods: {
        //处理数据函数
        getTreeData(data) {
            data.forEach((ele) => {
                ele.value = ele.id; //创建一个value字段
                if(ele.children != undefined){
                    if (ele.children.length === 0) {
                        //如果children为空，则删除children
                        delete ele.children;
                    } else {
                        //children不为空表示里面仍然有子节点，继续遍历
                        this.getTreeData(ele.children);
                    }
                }
            });
            return data;
        },
        // 获取省市区地址级联
        handleChange(e, form, thsAreaCode) {
            thsAreaCode = this.$refs['cascaderAddr'].getCheckedNodes()[0].pathLabels// 注意2： 获取label值
            this.area = thsAreaCode[0] + '/' + thsAreaCode[1] + '/'  + thsAreaCode[2]
            this.$emit('transfer',this.area)//触发transfer方法，this.area 为向父组件传递的数据 区域名
        },
    }
}
</script>
<style>

</style>