<template>
    <a-tree-select
        :treeData="treeData"
        placeholder="请选择"
        style="min-width:100px"
        v-model="modelKey"
        :treeCheckable = "treeCheckable"
    ></a-tree-select>
</template>
<script>

import {notification} from 'ant-design-vue'




export default {
    name:"treeCheck",
    props:{
        createFun:{

        },
        treeCheckable:{
            default:undefined
        },
        isNoNone:{ //是否可以判空
            type:Boolean,
            default: false
        },
        valueName:{
            type:String,
            default:"name"
        }
    },
    data(){
        return {
            trees:{},
            treeData:[],
            modelKey:""
        }
    },
    methods:{
        async setValue(id){
            if(JSON.stringify(this.trees) == "{}"){
                await this.createList();
            }
            for(let name in this.trees){
                if(this.trees[name].id == id){
                    this.modelKey = name;
                    break;
                }
            }


        },
        async setValue2(ids){

            if(JSON.stringify(this.trees) == "{}"){

                await this.createList();
            }
            let that = this;
            let list = ids.split(",");
            let list2 = new Array();

            list.forEach(id => {
                 for(let name in that.trees){
                    if(that.trees[name].id == id &&that.trees[name]["isChild"]){
                        list2.push(name);
                        break;
                    }
                }
            });

            this.modelKey = list2;
        },
        async setValue3(ids){
            if(JSON.stringify(this.trees) == "{}"){

                await this.createList();
            }
            let that = this;
            let list = ids.split(",");
            let list2 = new Array();
            list.forEach(id => {
                for(let name in that.trees){
                    if(that.trees[name].id == id){
                        if(that.trees[name]["isChild"]){
                            list2.push(name);
                            break;
                        }

                    }
                }
            });
            if(list2.length>0){
                //this.setValue2(ids)
                this.modelKey = list2;
                return;
            }
            list2 = new Array();
            list.forEach(id => {
                for(let name in that.trees){
                    if(that.trees[name].id == id){
                            list2.push(name);
                            break;
                    }
                }
            });


            this.modelKey = list2;
        },
         createList(){
             return new Promise(resolve => {
                 this.createFun().then(res =>{
                     this.trees = {};
                     let list2 = this.toTree(res);
                     this.treeData =list2;
                     resolve();
                 })
             })

        },
         toTree(list, text,plist) {
            if (!text) {
                text = '0-'
            }

            let list2 = new Array();
            for (let i = 0; i < list.length; i++) {
                let item = list[i]
                item['plen'] = list.length
                item['key'] = text + i
                item['value'] = text + i
                item['title'] = item[this.valueName];//item['name']
                if(plist){
                    item['plist'] = plist
                }else{
                    plist = new Array();
                }


                if (item.children) {
                    let plist2 = JSON.parse(JSON.stringify(plist));
                    plist2.push(item.id);
                    item.children = this.toTree(item.children, text + i + '-',plist2)
                }else{
                    item["isChild"] = true;
                }
                this.trees[text + i] = item
                list2.push(item)
            }
            return list2
        },
        onChange (value) {

        },
        //检查是否为空
        checkNone(text){
            if(this.isNoNone){
                if((!this.treeCheckable) && this.modelKey == ""){
                    notification.warning({ message: '系统提示', description: (text?text:"必选项不能为空")})
                    return false
                }else if(this.treeCheckable && this.modelKey.length == 0|| this.modelKey == ""){
                    notification.warning({ message: '系统提示', description: (text?text:"必选项不能为空")})
                    return false
                }
                return true
            }
        }
    },
    created(){
        this.createList();
    },
    watch:{
        modelKey(value) {
            let that = this;
            if(this.treeCheckable){
                let list = new Array();
                value.forEach(element => {
                    list.push(that.trees[element]);
                });
                this.$emit("selectcheck",{list});
            }else{
                this.$emit("select",that.trees[value]);
            }

        }
    }

}
</script>

