<script>
    import Base from '@/components/tree/basetree.vue';
    export default {
        name:'treedatabase',
        extends: Base,
        props:{
            isPop: {
                type: Boolean,
                default:false
            }
        } ,
        computed:{
            style() {
                let height = this.currentHeight;
                if (this.extraHeight) {
                    height =  height - Number(this.extraHeight);
                }
                if (this.outsideHeight) {
                    height =  height - Number(this.outsideHeight);
                }
                return 'height:' + height + 'px';
            }
        
        },
        methods: {
            customInit() {
                if (this.myConfig.showCondition === undefined) {
                    if (this.myConfig.items && this.myConfig.items.length >0) {
                        let flag = false;
                        this.myConfig.items.forEach(el=> {
                            if (el.condition || el.conditionPrior) {
                                flag = true;
                                return;
                            }
                        });
                        this.showCondition = flag;
                    }
                } else {
                    this.showCondition = this.myConfig.showCondition;
                }
                if (this.isPop) {
                    this.myConfig.showTitle = false;
                }

                if (this.myConfig.gridConfig) {
                    this.gridConfig = Object.assign({}, this.gridConfig, this.myConfig.gridConfig);
                }
                if (this.myConfig.formConfig && this.myConfig.formConfig.items && this.myConfig.formConfig.items.length > 0) {
                    if (!this.gridConfig.items || this.gridConfig.items.length === 0) {
                        this.gridConfig.items = this.$Method.copy(this.myConfig.formConfig.items);
                    }
                    this.gridConfig.items.forEach(el => {
                        if (el.tooltip === undefined) {
                            el.tooltip = true;
                        }
                    });
                    this.gridConfig.response = this.myConfig.response;
                    this.gridConfig.method = this.myConfig.method;
                    this.gridConfig.resources = this.myConfig.resources;
                    if (this.myConfig.searchParam) {
                        this.gridConfig.searchParam = this.myConfig.searchParam;
                    }
                    this.gridConfig.headStyle = '';
                    if (this.gridConfig.height === undefined) {
                        this.gridConfig.height = 360;
                    }
                    this.gridConfig.tableSize = 'small';
                    this.gridConfig.showBorder = false;
                    this.gridConfig.pageSize = 'small';
                    this.gridConfig.pageShowElevator = false;
                    this.gridConfig.pageShowTotal = false;
                    this.gridConfig.pageShowSizer = false;
                    if (this.myConfig.multiSelect !== undefined) {
                        this.gridConfig.multiSelect = this.myConfig.multiSelect;
                    } else {
                        this.gridConfig.multiSelect = false;
                    }
                    if (this.gridConfig.pageSizeNum === undefined) {
                        this.gridConfig.pageSizeNum = 50;
                    }
                    this.showGrid = true;
                }
                
            },
            getSearchParam(para) {
                let searchParam = Object.assign({},this.myConfig.searchParam,this.searchBeforeSearchParam);
                if (!searchParam) {
                    searchParam = {};
                }
                if (this.myConfig.response === 'report') {
                    searchParam = Object.assign({},searchParam,this.conditionSearchParam);
                } else {
                    let tempSearchParam = {};
                    for (let el in searchParam) {
                        if (this.conditionSearchParam[el]) {
                            tempSearchParam[el]={'$and':[]}; 
                            tempSearchParam[el]['$and'].push(searchParam[el]);
                            tempSearchParam[el]['$and'].push(this.conditionSearchParam[el]);
                        }
                    }
                    searchParam = Object.assign({},searchParam,this.conditionSearchParam);
                    for (let el in tempSearchParam) {
                        delete searchParam[el];
                        searchParam['$and'] = el['$and'];
                    }
                }
                if (para) {
                    Object.assign(searchParam, para);
                }
                if (this.myConfig.overrideSearchParam) {
                    return this.myConfig.overrideSearchParam(searchParam);
                } else {
                    return searchParam;
                }
            },
            setWhereParam(para) {
                this.conditionSearchParam = para;
            },
            gridRefurbish (obj) {
                this.$refs.grid.clearSearchBeforeSearchParam();
                this.$refs.grid.setSearchBeforeSearchParam(obj);
                this.$refs.grid.refurbish();
                this.$refs.grid.refurbish();
            },
            conditionAction(temp) {
                let data=[];
                switch (temp.action) {
                    case 'condition_search' :
                        if (JSON.stringify(temp.para) !== '{}') {
                            if (this.showGrid) {
                                this.showTree = false;
                                this.gridRefurbish(temp.para);
                            } else {
                                this.conditionSearchParam = temp.para;
                                this.refurbish();
                            }
                        } else {
                            this.showTree = true;
                            this.conditionSearchParam = {};
                            this.refurbish();
                        }
                        break;
                    case 'condition_clear' :
                        this.conditionSearchParam = {};
                        this.refurbish();
                        break;
                    case 'condition_ok' :
                        data = this.getData();
                        if (data.length === 0) {
                            this.alert('请选择');
                        } else {
                            temp.rows = data;
                            this.$emit('conditionAction',temp);
                        }
                        break;
                    case 'condition_cancel' :
                        this.$emit('conditionAction',temp);
                        break;
                }
            },
            getSelection() {
                if (this.showTree) {
                    return this.getData();
                } else {
                    return this.$refs.grid.getSelection();
                }
               
            },
            dbclick (e) {
                let temp = {};
                temp.action= 'condition_ok';
                temp.rows = [];
                temp.rows.push(e);
                this.$emit('conditionAction',temp);
            },
            clearCondition() {
                if (this.$refs.condition && this.$refs.condition.clear) {
                    this.$refs.condition.clear();
                }
                this.conditionSearchParam = {};
                this.showTree = true;
                this.refurbish();

            },
        },
    };
</script>


