<script>
    import Base from './conditionbase.vue';
    export default {
        name: 'queryconditionboxbase',
        extends: Base,
        data () {
            return {
                myConfig: {
                },
                formConfig:{
                    lineNum:1,
                    labelWidth:100,
                    labelPosition:'right',
                    items:[]
                },
                showFormBox:true,
                showClearBtn:true,
                maxLineNum:3,
            };
        },
        methods:{
            customInit() {
                if (this.myConfig.formConfig !== undefined) {
                    Object.assign(this.formConfig, this.myConfig.formConfig);
                } else {
                    this.initField();

                    this.condition.forEach(el => {
                        this.formConfig.items.push(this.$Method.copy(el.config));
                    });

                    if (this.myConfig.labelWidth) {
                        this.formConfig.labelWidth = this.myConfig.labelWidth;
                    }
                    if (this.myConfig.labelPosition) {
                        this.formConfig.labelPosition = this.myConfig.labelPosition;
                    }
                    if (this.myConfig.maxLineNum) {
                        this.maxLineNum = this.myConfig.maxLineNum;
                    }

                    if (this.modelType != 'treeSearch') {
                        if (this.myConfig.lineNum) {
                            this.formConfig.lineNum = this.myConfig.lineNum;
                        } else {
                            let num = this.formConfig.items.length;
                            if (num > this.maxLineNum) {
                                this.formConfig.lineNum = this.maxLineNum;
                            } else {
                                this.formConfig.lineNum = num;
                            }
                        }
                    }
                } 
                if (this.formConfig.items.length === 0) {
                    this.showFormBox = false;
                } else {
                    this.showFormBox = true;
                }
            },
            customClear() {
                if (this.$refs.form) {
                    this.$refs.form.clearData();
                }
            },
            customGetQuerySearchParam() {
                let result={};
                let form = this.$refs.form.getData();
                if (this.myConfig.formConfig === undefined) {
                    for (let temp in this.dataType) {
                        if (form[temp]) {
                            if (this.dataType[temp].calculator) {
                                let obj = this.getConditionValue(temp,form[temp],this.dataType[temp].calculator,this.dataType[temp]);
                                result = Object.assign({},result,obj);
                            } else {
                                result[temp] = form[temp];
                            }
                        }
                    }
                } else {
                    let obj = {};
                    obj['$and'] = [];
                    this.formConfig.items.forEach(el => {
                        if (form[el.name]) {
                            let oper = '==';
                            if (el.name.indexOf('name') !== -1) {
                                oper = 'LIKE';
                            }
                            if (el.oper) {
                                oper = el.oper;
                            } 
                            let temp = {};
                            temp[el.name] = {};
                            if (oper === 'LIKE') {
                                temp[el.name][oper] = '%' + form[el.name]+'%';
                            } else {
                                temp[el.name][oper] = form[el.name];
                            }
                            obj['$and'].push(temp);
                        }
                    });
                    if (obj['$and'].length > 0) {
                        Object.assign(result, obj);
                    }
                }
                return result;
            },
            
            getValue(name) {
                return this.$refs.form.getValue(name);
            },

            setValue(name, value) {
                return this.$refs.form.setValue(name,value);
            },
            doClear() {
                this.clear();
                this.search();
            }
          
        }
        
    };
</script>