<template> 
<div>
    <Row type="flex"  :gutter="8">
        <Col span='4'>
            <ControlBox  ref="field"  :config="fieldConfig"  ></ControlBox>
        </Col>
        <Col span='6'>
            <ControlBox  v-if="hackReset" ref="editors"  :config="editorsConfig"  ></ControlBox>
        </Col>
        <Col>
            <Button icon="ios-search"  @click="search" >查询</Button>
        </Col>
        <Col>
            <Button icon="ios-refresh" @click="clear">{{clearInfo}}</Button>
        </Col>
        <Col>
            <Button icon="ios-search" type="primary" @click="showHighSearch">高级查询</Button>
        </Col>
    </Row>
    <SearchGrid ref="searchgrid" :config="config" :dictData="searchDictData" :dataType="dataType" @search="search"/>
</div>
</template>
<script>
    import Base from './conditionbase.vue';
    import SearchGrid from './searchgrid.vue';
    export default {
        name: 'searchconditionbox',
        extends: Base,
        components:{
            SearchGrid
        },
        data () {
            return {
                fieldConfig: {
                    name:'field',
                    type:'ComboBox',
                    codeWithName:false,
                    valueChanged:this.fieldChanged
                },
                editorsConfig:{
                    name:'editors',
                    type:'TextBox',
                    readOnly:true
                },
                searchDictData:{}
            };
        },
        methods:{
            customInit() {
                this.initField();
                this.fieldConfig.data = this.conditionPrior;
                if (this.dictData != null) {
                    this.searchDictData = this.$Method.copy(this.dictData);
                }
                this.searchDictData.FIELD =  this.condition;
            },
            
            fieldChanged(e) {
                const index = this.conditionPrior.findIndex(el => el.code === e.value);
                if (index > -1) {
                    this.editorsConfig = Object.assign({},{},this.conditionPrior[index].config);
                } else {
                    this.editorsConfig = {
                        name:'editors',
                        type:'TextBox',
                        readOnly:true
                    };
                }
                this.hackReset = false;
                this.$nextTick(() =>{
                    this.hackReset = true;
                });
                
            },
            customClear () {
                this.$refs.field.setValue('');
                let e = {};
                e.value = '';
                this.fieldChanged(e);
                this.$refs.searchgrid.clearCondition();
                this.search();
            },
            customGetQuerySearchParam() {
                let searchParam = {};
                let obj = {};
                let el = {};
                el.field = this.$refs.field.getValue();
                el.editors = this.$refs.editors.getValue();
                el.calculator = '==';
                if (el.field && el.editors) {
                    if (this.dataType[el.field].calculator) {
                        el.calculator = this.dataType[el.field].calculator;
                    }
                    obj = this.getConditionValue(el.field,el.editors,el.calculator,this.dataType[el.field]);
                }
                let highObj = this.$refs.searchgrid.getResultCondition();
                if (highObj['$and']) {
                    if (obj !== {}) {
                        highObj['$and'].push(obj);
                    }
                    searchParam = Object.assign({},{},highObj);
                } else {
                    searchParam = Object.assign({},obj,highObj);
                }
                return searchParam;
            },
            showHighSearch() {
                this.$refs.searchgrid.show();
            }
          
        }
        
    };
</script>