<template>
  <Card :padding="0"    dis-hover :bordered="false"  :style="'background:#fff;overflow:hidden;height:' + clientHeight +'px'">
    <div ref="toolbarcard" >
         <Card :padding="0" style="padding-top:14px;padding-left:16px;padding-right:16px;border-radius:0px;" :bordered="false" dis-hover>
            <ListBtnToolbar ref="menutoolbar" :config="menuToolbar"  :moduleMethod="moduleMethod"   @doAction="doAction"/>
            <Divider style="margin-top:16px;margin-bottom:0px;" />
        </Card>
    </div>
    <div ref="formcard">
    <Card :padding="0"  dis-hover :bordered="false"  style="padding-left:14px;padding-right:14px;padding-top:0px;padding-bottom:0px">
        <Formcards  ref="form"  :config="myConfig.formConfig" :dictData="dictData"/>
    </Card>
    </div>
    <Tabs v-if="myConfig.tabFlag" class="tabmode" v-model='curTab' @on-click="tabClick">
            <TabPane v-for="(item,index) in myConfig.tabConfig" :label="item.title"  style="background:#fff" :name="item.code" :key="index">
                 <SimpleViewGrid :ref="item.code" :config="myConfig[item.gridName]"  :dictData="dictData"  @dbclick="dbclick" />
            </TabPane>
        </Tabs>
    <Card v-else :padding="0"  style="padding:14px 16px;" :bordered="false" dis-hover>
        <SimpleViewGrid  ref="grid" :config="myConfig.gridConfig"  :dictData="dictData"  @dbclick="dbclick" />
    </Card>
  </Card>
</template>

<script>
import ListBtnToolbar from './listbtntoolbar.vue';
import Formcards from './formcard.vue';
import elementResizeDetectorMaker from 'element-resize-detector';
export default {
    name:'mreport',
    components:{ListBtnToolbar,Formcards},
    data () {
        return {
            myConfig:{
                titleType:2,
                divStyle:'padding:5px',
                title:'查询条件',
                showMaxRow:2,
                tabFlag:false,
                showAsyncImportExport:false,
                tabConfig:[]
            },
            curTab:'',
            menuToolbar:{},
            gridToolbar:{},
            hideItems:[],
            frontBtn: [{
                name:'searchData',text:'查询',icon:'custom-search'
            },{
                name:'clearData',text:'清除',icon:'custom-clean'
            }]
        };
    },
    props:{
        config: {
            type:Object,
            default:null
        },
        dictData: {
            type: Object,
            default:null
        },
        moduleMethod:{
            type:Array,
            default: () => {
                return [];      
            }
        }
    },
    computed: {
        clientHeight () {
            return this.$Store.state.app.clientHeight;
        }
    },
    created () {
        this.myConfig =  Object.assign({},this.myConfig, this.config);
        if (this.myConfig.tabFlag) {
            this.myConfig.tabConfig.forEach((el,index) => {
                if (index === 0) {
                    this.curTab = el.code;
                }
                this.initGridConfig(el.gridName);
            });
        } else {
            this.initGridConfig('gridConfig');
        }
        this.myConfig.formConfig.titleType = this.myConfig.titleType;
        if (this.myConfig.formConfig.title === undefined) {
            this.myConfig.formConfig.title = this.myConfig.title;
        }
        if (this.myConfig.menuToolbar && this.myConfig.menuToolbar.items) {
            this.menuToolbar = this.$Method.copy(this.myConfig.menuToolbar);     
        } 
        let frontBtn = []
        this.frontBtn.forEach(el => {
            let index = this.menuToolbar.items.findIndex(el2 => el2.name === el);
            if (index === -1) {
                frontBtn.push(el);
            }
        });
        this.menuToolbar.items = frontBtn.concat(this.menuToolbar.items);
        this.menuToolbar.mainButton = 'searchData';
        this.menuToolbar.mainButtonText ='查询';
        this.menuToolbar.ignoreModule = ['searchData','clearData'];
        let lineNum = this.myConfig.formConfig.lineNum;
        let culLineNum = 0;
        let flag = true;
        
        this.myConfig.formConfig.items.forEach(el => {
            if (el.colNum) {
                culLineNum = culLineNum + Number(el.colNum)
            } else {
                culLineNum++;
            }
            if (flag && culLineNum > Number(lineNum)* Number(this.myConfig.showMaxRow)) {
                flag = false
            } 
            if (!flag){
                el.visible = false;
                this.hideItems.push(el);
            }
        });
        if (this.hideItems.length > 0) {
            this.myConfig.formConfig.items.push({
                type:'Divider',
                orientation:'right',
                hide:true,
                style: 'margin:2px 0',
                dividerName:'hidedivider',
                doHide:this.doHide
            });
        }
    },
    methods: {
        doAction (obj) {
            const method = obj.name;
            if (this.$parent[method]) {
                this.$parent[method](obj);
            } else {
                if (this[method]) {
                    this[method](obj);
                }
            }
        },
        dbclick (e) {
            let obj = {};
            obj.name ='gridDbClick';
            obj.rowinfo = {};
            obj.rowinfo.row = e;
            if (this.myConfig.tabFlag) {
                obj.rowinfo.gridObject = this.$refs[this.curTab][0];
            } else {
                  obj.rowinfo.gridObject = this.$refs.grid;
            }
          
            this.doAction(obj);
        },
        initGridConfig(name) {
            if (!this.myConfig[name]) {
                return;
            }
            this.myConfig[name].headStyle='';
            this.myConfig[name].showCondition = false;
            this.myConfig[name].onSelectionChange = this.gridOnSelectionChange;
            if (this.myConfig[name].resources === undefined) {
                this.myConfig[name].resources =this.config.resources;
            }
            if (this.myConfig[name].method === undefined) {
                this.myConfig[name].method = this.config.classPrefix + '.' +'query';
            }
            if (this.myConfig[name].response === undefined) {
                this.myConfig[name].response =  this.config.response;
            }
            this.myConfig[name].height = 1;
            this.myConfig[name].width = 1;
            this.myConfig[name].adjustHeight = 0;
            this.myConfig[name].adjustWidth = 0;
            this.myConfig[name].pageSize='small'; 
            
            if (this.myConfig[name].pageSizeNum === undefined) {
                this.myConfig[name].pageSizeNum = 20;
            }
            this.myConfig[name].items.forEach(el => {
                if (el.type !== 'CheckBox') {
                    if (el.tooltip === undefined) {
                        el.tooltip = true;
                    }
                }
            });
        },
        tabClick(e) {
            if (this.$parent.tabClick) {
                this.$parent.tabClick(e);
            }
        },
        clearData () {
            this.$refs.form.clearData();
            this.$refs.grid.clearSearchBeforeSearchParam();
            this.$refs.grid.clearData();
        },
        doHide(obj) {
            let items = [];
            this.hideItems.forEach(el => {
                let item = {};
                if (el.sname) {
                    item.sname = el.sname;
                }
                if (el.name) {
                    item.name = el.name;
                }
                el.visible = obj.value;
                item.visible = obj.value;
                items.push(item);
            });
            this.$refs.form.updateItemsConfig(items);
        },
        searchData() {
            this.$refs.form.checkValidate();
            let flag = this.$refs.form.isError();
            if (flag) {
                this.alert('必填项不允许为空');
                return false;
            }
            if (this.$parent.searchCheck) {
                let tmpFlag = true;
                tmpFlag = this.$parent.searchCheck();
                if (!tmpFlag) {
                    return false;
                }
            }
            
            let data = this.$refs.form.getData();
            let searchParam = {};
            for (let pro in data) {
                if (data[pro] || data[pro] === 0) {
                    searchParam[pro] = data[pro];
                }
            }
            if (this.myConfig.overrideSearchParam) {
                searchParam =  this.myConfig.overrideSearchParam(searchParam);
            } 
            if (this.myConfig.tabFlag) {
                let gridName = this.myConfig.tabConfig[0].code;
                if (this.$refs[gridName] && this.$refs[gridName][0]) {
                    this.$refs[gridName][0].clearSearchBeforeSearchParam();
                    this.$refs[gridName][0].setSearchBeforeSearchParam(searchParam);
                    this.$refs[gridName][0].refurbish();
                    this.curTab = gridName;
                }
            } else {
                this.$refs.grid.clearSearchBeforeSearchParam();
                this.$refs.grid.setSearchBeforeSearchParam(searchParam);
                this.$refs.grid.refurbish();
            }
           
        },
        exportData() {
            if (this.myConfig.tabFlag) {
                if (this.$refs[this.curTab] && this.$refs[this.curTab][0]) {
                    let grid = this.$refs[this.curTab][0];
                    this.commonExport(grid);
                }
            } else {
               this.commonExport(this.$refs.grid);
            }
        },
        setTab(name) {
            this.curTab = name;
        },
        commonExport(grid) {
            let data = grid.getData();
            if (data.length  === 0) {
                this.alert('请先查询数据');
                return false;
            }
            if (grid.config.dynamicColumnFlag) {
                 grid.dynamicExportExcel();
            } else {
                if (this.myConfig.showAsyncImportExport) {
                    if (this.$parent.asyncExport) {
                        this.$parent.asyncExport(grid)
                        return;
                    }
                } else {
                    if (this.myConfig.defaultExport) {
                        let _param = grid.getSearchParam();
                        let  moduleCode = this.getModulecode();
                        if (moduleCode) {
                            _param.moduleCode = moduleCode;
                        } else {
                            this.alert('模块号不能为空');
                            return;
                        }
                        _param.exporttype = 'excel';
                        let _baseUrl = grid.config.resources.replace('rest', 'exportReport');
                        let  _self  = this;
                        this.doExportReport(_baseUrl, grid.config.method, _param, _self);
                    } else {
                        grid.export();
                    }
                }
            }
           
           
        },
        refurbish () {
            this.searchData();
        },
        getGrid (name) {
            if (this.myConfig.tabFlag) {
                if (this.$refs[name] && this.$refs[name][0]) {
                    return this.$refs[name][0];
                }
            } else {
                return this.$refs.grid;
            }
        },
        getForm () {
            return this.$refs.form;
        },
        setGridHeight() {
            let height = this.$refs.toolbarcard.offsetHeight  + this.$refs.formcard.offsetHeight + 43;
            if (this.hideItems && this.hideItems.length > 0) {
                height = height + 20;
            }
            if (this.myConfig.tabFlag) {
                height = height + 60;
                this.myConfig.tabConfig.forEach(el => {
                    if (this.$refs[el.code] && this.$refs[el.code][0]) {
                        this.$refs[el.code][0].setOutsideHeight(height);
                    }
                })
            } else {
                if (this.$refs.grid.showSummary) {
                    height = height + 36;
                }
                this.$refs.grid.setOutsideHeight(height);
            }
        },
        setHideDividerValue(value) {
            this.$refs.form.setHideDividerValue(value);
        }
    },
    mounted () {
        this.observer = elementResizeDetectorMaker();
        if (this.$refs.formcard) {
            this.observer.listenTo(this.$refs.formcard, this.setGridHeight);
        }
    },
    beforeUnmount() {
        if (this.$refs.formcard) {
            this.observer.removeListener(this.$refs.formcard, this.setGridHeight);
        }
    }
};
</script>

