<template>
    <div class="gridCtl" :class="type" :style="extendStyle" :id="id">
        <div class="goGridObj no-border" :id="gridId"></div>
    </div>
</template>

<script>
    import {AssertUtils} from "goingutils";
    import {_getGridWidAndHeight} from "../commonFun/CommonFun";
    import InitGridObj from "../../dx/dxInitUtils/initGridObj";
    import CalendarUtils from "../../jsCom/goForm/components/go-calendar-event";
    import GridMixins from "../ctlMixins/GridMixins";


    export default {
        name: "go-single-grid",
        mixins: [GridMixins],
        data: function () {
            return {
                gridObj: null,
                initFlag: false,
            };
        },
        props: {},
        computed: {
            extendStyle(){
                let sty={};
                if(parseFloat(this.padding)>0){
                    sty["padding"]=`0 ${this.padding}px`;
                }
                return sty;
            }
        },
        methods: {
            initGridEvent(gridObj){
                //表格空白处点击
                gridObj.attachEvent("onEmptyClick", function () {
                    //隐藏显示的日期
                    for (let obj in  CalendarUtils.calendarObj) {
                        if (AssertUtils.isNotNull(CalendarUtils.calendarObj[obj])) {
                            CalendarUtils.calendarObj[obj].hide();
                        }
                    }
                });
                //只读表格默认是可以进行复制操作的
                gridObj.enableBlockSelection();
            },
            initGridObj(pWid, pHeight) {
                let vueObj = this;
                this.initGridParams(pWid, pHeight);
                vueObj.config.methodType=this.methodType||"POST";
                let gridObj = InitGridObj(vueObj.gridId, vueObj.config, vueObj);
                this._initCommonEvent(gridObj,vueObj);
                this.initGridEvent(gridObj,vueObj);
            },
            resize() {
                let vueObj = this;
                if (vueObj.timerOut != null) {
                    clearTimeout(vueObj.timerOut);
                }
                vueObj.timerOut = setTimeout(function () {
                    vueObj.resizeGridObj();
                    vueObj.gridObj&&vueObj.gridObj._initColWidthAgain&&vueObj.gridObj._initColWidthAgain();
                }, 20);
            }
        },
        mounted() {
            this.$nextTick(()=>{
                //如果加载标志为false 表示表格还未初始化 则进行初始化操作
                if (!this.initFlag) {
                    if (AssertUtils.isNotEmpty(this.height)&&this.height>0) {
                        this.initGridObj(this._getGridWidth(), this.height);
                    } else {
                        //获取宽度 和高度
                        let {pWid, pHeight} = _getGridWidAndHeight(
                            this.id,
                            this.hasPagin,
                            this.topHeight,
                            this.hasToolBar
                        );
                        this.initGridObj(pWid, pHeight);
                    }
                }
            });
        },
        watch: {
            queryUrl(newVal){
                if(this.gridObj){
                    this.gridObj.flushGridByUrl({url:newVal});
                }
            },
            //如果是宽度变化 则需要进行resize操作
            height(newVal) {
                $("#" + this.gridId).css({height: newVal});
                if (this.gridObj != null) {
                    this.gridObj.setSizes();
                }
            }
        },
        beforeDestroy() {
            if (this.gridObj != null) {
                if(this.gridObj&&this.gridObj.destructorCus!=null){
                    this.gridObj.destructorCus();
                }

                this.gridObj.destructor();
                this.gridObj = null;
            }
        }
    };
</script>
