<template>
    <div ref="formPanel" class="go-form-panel" style="height: 100%;box-sizing: border-box">
        <div class="go-form-panel-body">
            <div style="position: relative">
                <form v-html="getFormContent()"></form>
            </div>
        </div>
    </div>
</template>

<script>
    //重写授权人员窗口的事件注册
    import GoingForm from "../../jsCom/comCtl/goingForm";
    import EditorTypeUtils from "../../common/editorTypeUtils";

    export default {
        name: "go-from-ctl",
        data: function () {
            return {
                goingFrom: null
            };
        },
        props: {
            domainData: {
                type: Object,
                default: () => {
                    return {};
                }
            },
            labelWidth: {
                type: [String, Number],
                default: 80
            },
            inputWidth: {
                type: [Number, String],
                default: "auto"
            },
            colWidth: {
                type: Array,
                default: () => []
            },
            colNum: {
                type: Number,
                default: 0
            },
            domain: {
                type: Array,
                required: true,
                default: () => []
            },
            formType: {
                type: String,
                default: "editor"
            }
        },
        methods: {
            getFormContent() {
                let queryAry = [];
                let vueObj = this;
                this.domain.map(function (item) {
                    //如果是查询类型 判断是否参与查询  如果是编辑类型判断是否参与 编辑
                    if ((item["editor"] !== false && vueObj.formType !== "query") || item["query"] !== false && vueObj.formType === "query") {
                        //如果配置了单元格占用数量 而且占用宽度大于1
                        if (item["cellCount"] != null && item["cellCount"] > 1) {
                            queryAry.push(item);
                            for (let num = 0; num < item["cellCount"] - 1; num++) {
                                queryAry.push({type: "slot", fieldId: item["id"]});
                            }
                        } else {
                            queryAry.push(item);
                        }
                    }
                });
                //需要初始化表单才行
                EditorTypeUtils.initGoForm();
                //初始化表单
                let goingForm = new GoingForm(this.formType, null, this.queryBtnFlag);

                //初始化form的内容区域
                goingForm.initForm({
                    fieldAry: queryAry,
                    colWidth: this.colWidth,
                    colNum: this.colNum,
                    labelWidth: this.labelWidth,
                    inputWidth: this.inputWidth
                });
                this.goingFrom = goingForm;

                return goingForm.html();
            }
        },
        updated() {
            //如果组件发生了变更  则需要重新加载form的值
            if (this.domainData != null) {
                this.goingFrom.loadDataByJson(this.domainData);
            }
            //重新初始化form对象。
            EditorTypeUtils.initGoCtlByClass(
                $(this.$refs.formPanel),
                this.inputWidth,
                this.goingFrom,
                this
            );
        },
        computed: {
            //判断用户是否定义了查询按钮事件 如果定义了则显示查询按钮
            queryBtnFlag() {
                if (this._events["queryBtn"] != null) {
                    return true;
                }
                return false;
            }
        },
        mounted() {
            this.$nextTick(function () {
                //重新初始化form对象。
                EditorTypeUtils.initGoCtlByClass(
                    $(this.$refs.formPanel),
                    this.inputWidth,
                    this.goingFrom,
                    this
                );
                //如果传入了初始值 则根据初始值来初始化form对象
                if (this.domainData != null) {
                    this.goingFrom.loadDataByJson(this.domainData);
                }

                var vm = this;
                $(this.$refs.formPanel)
                    .find(".common_button_emphasize")
                    .on("click", function () {
                        vm.$emit("queryBtn", vm.goingFrom.getSubmitData());
                    });
                $(this.$refs.formPanel)
                    .find(".common_button_gray")
                    .on("click", function () {
                        vm.goingFrom.clearData();
                    });
            });
        },
        beforeDestroy() {
            //查询按钮事件解绑
            $(this.$refs.formPanel)
                .find(".common_button_emphasize").unbind();
            //取消按钮事件解绑
            $(this.$refs.formPanel)
                .find(".common_button_gray").unbind();
            //销毁对象
            if (this.goingFrom != null) {
                this.goingFrom.destructor();
            }
            this.goingFrom = null;
        }
    };
</script>
