<template>
    <el-cascader
        v-model="value"
        :options="data"
        :key='num'
        :filterable="properties.filterable"
        :show-all-levels="properties.showAllLevels"
        :disabled="properties.disabled"
        :props="properties.props"
        @change="onChange"
        collapse-tags
        :placeholder="properties.placeholder"
        clearable
    ></el-cascader>
</template>

<script>
import mixin from "./mixin";
import handleSelectTree from "./handleSelectTree";
    export default {
        name: "treeCheckSelect",
        mixins: [mixin],
        data() {
            return {
                data:[],
                value:[],
                num:Math.random(),//用来刷新列表，解决level is not undefined的报错
                properties:{}
            };
        },
        created() {
            this.initProperties({
                leafOnly:false,//true父节点即是选中也不返回
                placeholder:'请输入',
                filterable:false,
                disabled:false,
                props: {
                    multiple: true,
                    emitPath:false,
                    checkStrictly: false,
                    disabled: false,
                    expandTrigger: 'hover',
                    value: 'id',
                    label: 'name',
                    children: 'children'
                },
                showAllLevels: true,    // true 输入框中显示选中项所在的完整路径 false 仅显示最后一级
            })
        },
        methods: {
            init(value){
                let vm=this;
                this.getData().then(function(data){
                    vm.num = Math.random();
                    vm.data=data;
                    vm.value=value||[];
                });
            },

            onChange(){
                let model;
                console.log(this.value)
                if (this.properties.props.checkStrictly) {
                    model = this.value
                } else {
                    model=this.properties.leafOnly?this.value:handleSelectTree(this.data,this.value,this.properties.props.value);
                }
                console.log( model)
                this.$emit("update:model",model);
                this.$emit("change",model)
            }
        }
    }
</script>

<style scoped>

</style>
