<template>
  <div>
    <div
      class="designer-view"
      :class="[
        layoutType === 'H5' ? 'h5-layout' : layoutType === 'Pad' ? 'pad-layout' : '',
      ]"
    >
      <v-form-render
        :form-json.sync="formJson"
        :form-data="formData"
        :option-data="optionData"
        ref="vFormRef"
        :reportTemplate.sync="formTemplate"
        :conditionParam.sync="conditionParam"
        :formInsData.sync="formInsData"
        v-if="showRender"
        visible-key="showRender"
        :visiable.sync="showRender"
        :parent-target="_self"
        @reload="$baseReload()"
        :dataId.sync="dataId"
        :param="param"
        v-bind="$attrs"
        @openCopyEditTab="openCopyEditTab"
      />
    </div>
  </div>
</template>
<script>
import { loadExtension } from "@base/components/xform/extension/extension-loader";

loadExtension();
import VFormRender from "@base/components/xform/form-render/index.vue";
import scriptHttpMixin from "@base/components/xform/mixins/scriptHttp";

export default {
  name: "vform_render",
  mixins: [scriptHttpMixin],
  components: {
    VFormRender,
  },
  props: ["object_foreign_id", "dataId", "param"],
  data() {
    return {
      formJson: {},
      formData: {},
      optionData: {},
      showRender: false,
      formTemplate: null,
      formCode: null,
      layoutType: null,
      conditionParam: null,
      formInsData: null,
    };
  },
  created() {
    this.initReportCode();
    this.getReportTemplate();
  },
  methods: {
    dataTableSearch() {
      let gridRef = this.$refs.vFormRef.getGridWidgetRef()
      if(!gridRef)return
      let options = gridRef.widget.options;
      if(options.searchOnSwitchTab && options.isQueryTable){
        gridRef.searchEvent();
      }
    },
    initReportCode() {
      let code = this.$attrs.formCode;
      if (!code) {
        var hash = window.location.hash;
        let str = "#/report/xform/render/";
        if (hash.indexOf(str) == 0) {
          code = hash.slice(str.length);
        }
        if (!code && this.$route.query) {
          code = this.$route.query.formCode;
        }
      }
      this.formCode = code;
    },
    getReportTemplate() {
      let param = {
        objTypeCode: this.$attrs.objTypeCode,
        objId: this.$attrs.objId,
        taskId: this.$attrs.taskId,
      };
      this.conditionParam = param;
      let url, data;
      url = USER_PREFIX + `/formTemplate/getByFormCode`;
      data = { stringOne: this.formCode };
      this.$http({
        aes: true,
        url: url,
        method: `post`,
        data: data,
        // isLoading: true,
        // loadingTarget: document.body,
        // modalStrictly: true,
        success: async (res) => {
          let formTemplate = res.objx || {};
          this.formTemplate = formTemplate;
          this.formJson = formTemplate.formViewContent
            ? JSON.parse(formTemplate.formViewContent)
            : {};
          this.layoutType = this.formJson.formConfig.layoutType;
          this.getFormInsData2();
        },
      });
    },
    getReqParam(item, dataId, billData) {
      let param = {};
      if (item.accessParam) {
        if (typeof item.accessParam === "string") {
          let n = new Function("billData", item.accessParam);
          param = n.call(this, billData);
        } else {
          param = item.accessParam;
        }
      } else {
        param = { id: dataId };
      }
      return param;
    },
    getFormInsData2() {
      let that = this;
      let formConfig = this.formJson.formConfig;
      let dataId = this.dataId;
      if (formConfig.onBeforeCreated) {
        let n = new Function("dataId", "formCode", "done", formConfig.onBeforeCreated);
        let toDo = function (res) {
          let formData = res && res.objx ? res.objx : {};
          that.formData = formData;
          that.formInsData = formData;
          that.showRender = true;
        };
        n.call(this, dataId, this.formCode, toDo);
      } else {
        this.formData = this.$attrs.formData ?? {};
        this.showRender = true;
      }
    },
    async getFormInsData() {
      let formConfig = this.formJson.formConfig;
      let getConfig = formConfig.getConfig || {};
      let dataId = this.dataId;
      if (dataId) {
        let reqParam = { id: dataId };
        let accessParamArguments = [{ name: "id", value: dataId }];
        this.scriptHttp({
          options: getConfig,
          params: reqParam,
          targetFormTemplate: this.formTemplate,
          accessParamArguments: accessParamArguments,
          success: (res) => {
            let formData = res.objx || {};
            this.formData = formData;
            this.formInsData = res.objx;
            this.showRender = true;
          },
        });
      }
    },
    openCopyEditTab(formData) {
      this.$emit("openCopyEditTab", formData);
    },
  },
};
</script>
