<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:{
            type: String,
            default: '',
        },
        pca:{
            type: Array,
            default: [],
        },
    },
    data () {
      return {
        area:'',
        code:'',
        selectedOptions: this.isValue != undefined ? [Number(this.isValue.substr(0,2)+'0000'),Number(this.isValue.substr(0,4)+'00'),Number(this.isValue)] : [],       
        // selectedOptions: [130000, 130200,130203], 数据类型必须为number
        options: this.getTreeData(this.pca),
        props: {
          value: 'id',
          label: 'name'
        }
      }
    },

    watch: {
        isValue(val){
            let areaId = []
            let value = []
            value = String(this.isValue) 
            if(value != undefined || value != '' || value != 'undefined'){
                areaId[0] = Number(value.substr(0,2)+'0000')
                areaId[1] = Number(value.substr(0,4)+'00')
                areaId[2] = Number(value)
                this.selectedOptions = areaId
                console.log("{areaId}:"+areaId)
            }else{
                this.selectedOptions = []
                console.log("{areaId}")
            }
        },
    },
    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.code = e[2]
            console.log(e)
            this.$emit('transfer',this.area)//触发transfer方法，this.area 为向父组件传递的数据 区域名
            this.$emit('transferId',this.code)//触发transferId方法，this.code 为向父组件传递的数据 区域ID 只取县级ID
            this.$emit('transfersAreaCode',thsAreaCode)//触发transferId方法，thsAreaCode 为向父组件传递的数据 区域集合
        },
    }
}
</script>
<style>

</style>