<template>
  <div style="height: 100%;">
    <generate-layout-form v-if="viewDragLayout"
                          :data="viewDragLayout"
                          :relationList="relationList"
                          :refreshMap="refreshMap"
                          :isShowInTab="isShowInTab"
                          :formOption="formOption"
                          :bpmOption="bpmOption"
                          :subViews="subViews"
                          :stores="stores"
                          :parameterValue="parameterValue"
                          @setPk="setPk"
                          @setRefresh="setRefresh"
                          style="height: 100%;"
                          @addFormInCurrentTab="addFormInCurrentTab"
                          @editFormInCurrentTab="editFormInCurrentTab"
                          @detailFormInCurrentTab="detailFormInCurrentTab"
                          v-on="$listeners">
    </generate-layout-form>
    <view-redirect-row v-else-if="layout && layout.length > 0"
                       :rows="layout"
                       :relationList="relationList"
                       :refreshMap="refreshMap"
                       :isShowInTab="isShowInTab"
                       :formOption="formOption"
                       :bpmOption="bpmOption"
                       :subViews="subViews"
                       :stores="stores"
                       :parameterValue="parameterValue"
                       @setPk="setPk"
                       @setRefresh="setRefresh"
                       style="height: 100%;"
                       @addFormInCurrentTab="addFormInCurrentTab"
                       @editFormInCurrentTab="editFormInCurrentTab"
                       @detailFormInCurrentTab="detailFormInCurrentTab"
                       v-on="$listeners">
    </view-redirect-row>

    <form-redirect-div ref="formInCurrent" :stores="stores"></form-redirect-div>
  </div>
</template>

<script>

  import { getAction } from '../../utils/manage'
  import ViewRedirectRow from './ViewRedirectRow'
  import YchConstant from "../../utils/YchConstant";
  import GenerateLayoutForm from '../Eform/GenerateLayoutForm'
  import RelationUtil from "./RelationUtil";
  import RefreshUtil from "./RefreshUtil";
  import FormRedirectDiv from "./FormRedirectDiv";
  import {TabInCurrentMixin} from './TabInCurrentMixin'

  export default {
    name: 'ViewLayoutRedirect',
    mixins: [TabInCurrentMixin],
    components: {
      ViewRedirectRow,
      GenerateLayoutForm,
      FormRedirectDiv,
    },
    props: {
      viewCode: String,
      stores: {
        type: Object,
        default: () => {
          return {}
        }
      },
      parameterValue: {
        type: Array,
        default: () => []
      },
      isShowInTab: Boolean,
      formOption: Object,
      bpmOption: Object,
    },
    data () {
      return {
        relationList: [], // 联动关系
        refreshMap: {}, // 刷新关系
        layout: [],
        viewDragLayout: null,
        url: {
          getViewModel: YchConstant.PREFIX_EFORM + '/engine/viewEngine/getMainViewModel'
        },
        subViews: {},
        layoutList: []
      }
    },
    created() {
      console.log(this,'ViewLayoutRedirect')
    },
    methods: {
      reLoadForPreview (viewDragLayout, model) {
        this.relationList = []
        this.refreshMap = {}
        this.subViews = {}
        this.layout = []
        this.viewDragLayout = null

        this.subViews = model.subViews
        this.relationList = model.relationList
        if (model.viewLayout && model.viewLayout.rows) {
          this.layout = model.viewLayout.rows
          this.layoutList = model.viewLayout.rows
        } else if (model.viewDragLayout) {
          this.viewDragLayout = model.viewDragLayout
          this.layoutList = model.viewDragLayout
        }
      },
      reLoad() {
        this.relationList = []
        this.refreshMap = {}
        this.subViews = {}
        this.layout = []
        this.viewDragLayout = null

        if (!this.viewCode) {
          return
        }
        getAction(this.url.getViewModel, {viewCode: this.viewCode}).then((res) => {
          if (res.success) {
            const model = res.result
            this.subViews = model.subViews
            this.relationList = model.relationList
            if (model.viewLayout && model.viewLayout.rows) {
              this.layout = model.viewLayout.rows
              this.layoutList = model.viewLayout.rows
            } else if (model.viewDragLayout) {
              this.viewDragLayout = model.viewDragLayout
              this.layoutList = model.viewDragLayout
            }
          } else {
            this.$message.warning(res.message)
          }
        }).finally(() => {
        })
      },
      setPk(option) {
        this.relationList = RelationUtil.setChangeValue(this.relationList, option)
      },
      setRefresh(option) {
        this.refreshMap = RefreshUtil.setRefreshValue(this.refreshMap, option)
      },
      doResize() {
        // 解决缓存路由的情况下，切换路由后，自由组合页面宽度变成0的问题
        if (this.viewDragLayout && this.$route.meta.keepAlive) {
          this.$nextTick(() => {
            // 手动触发窗口resize事件
            if (document.createEvent) {
              const event = document.createEvent("HTMLEvents");
              event.initEvent("resize", true, true);
              window.dispatchEvent(event);
            } else if (document.createEventObject) {
              window.fireEvent("onresize");
            }
          })
        }
      }
    },
    watch: {
      viewCode: {
        handler () {
          this.reLoad()
        },
        immediate: true
      },
      layoutList: {
        handler (value) {
          sessionStorage.setItem('layoutList',JSON.stringify(value))
        },
        immediate: true
      }
    },
    activated() {
      this.doResize();
    }
  }
</script>

