<template>
    <div class="gridCtl" :class="type" :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 GridMixins from "../ctlMixins/GridMixins";



    function _getGridWidth() {
        let _width = "100%";
        if (this.width !== 0) {
            _width = this.width;
        }
        return _width;
    }

    export default {
        name: "go-editor-grid",
        mixins: [GridMixins],
        data: function () {
            return {
                gridObj: null,
                initFlag: false,
            };
        },
        props: {

        },
        methods: {
            initGridEvent(gridObj,vueObj){
                //表格空白处点击 停止编辑状态
                gridObj.attachEvent("onEmptyClick", function () {
                    gridObj.editStop();
                });
                //只有编辑表格才可能拖拽
                if(vueObj.config["drag"]){
                    gridObj.enableDragAndDrop(true);
                }
            },
            initGridObj(pWid, pHeight) {
                let vueObj = this;
                this.initGridParams(pWid, pHeight);
                //设置表格可以编辑的状态
                vueObj.config["editor"] = true;
                //初始化表格
                let gridObj = InitGridObj(vueObj.gridId, vueObj.config, vueObj);
                this._initCommonEvent(gridObj,vueObj);
                this.initGridEvent(gridObj,vueObj);
            }
        },
        mounted() {
            let vueObj = this;
            this.$nextTick(
                function () {
                    //如果加载标志为false 表示表格还未初始化 则进行初始化操作
                    if (!this.initFlag) {
                        if (AssertUtils.isNotEmpty(this.height)) {
                            vueObj.initGridObj(this._getGridWidth(), this.height);
                        } else {
                            //获取宽度 和高度
                            let {pWid, pHeight} = _getGridWidAndHeight(
                                this.id,
                                this.hasPagin,
                                this.topHeight
                            );
                            vueObj.initGridObj(pWid, pHeight);
                        }
                    }
                }.bind(this)
            );

        },
        watch: {
            //如果是宽度变化 则需要进行resize操作
            height(newVal) {
                $("#" + this.gridId).css({height: newVal});
                if (this.gridObj != null) {
                    this.gridObj.setSizes();
                }
            }
        },
        beforeDestroy() {
            if (this.gridObj != null) {
                this.gridObj.destructor();
                this.gridObj = null;
            }
            if (
                this.$store != null &&
                this.$store[this.id] != null &&
                this.$store[this.id].destructor != null
            ) {
                //销毁内存
                this.$store[this.id].destructor();
            }
            if (this.$store != null && this.$store[this.id] != null) {
                //置空对象
                this.$store[this.id] = null;
            }
        }
    };
</script>
