<template>
  <a-spin :spinning="spinning !== 0" :style="bodyStyle" style="height: 100%">
    <a-card :bordered="false" v-if="formType === 'form'">
      <form-redirect ref="formRedirect"
                     :func="func"
                     :formId="formId"
                     :formCode="formCode"
                     :fks="fks"
                     :parentValue="parentValue"
                     :saveInPage="saveInPage"
                     :disQueryDB="disQueryDB"
                     :record="record"
                     :stores="stores"
                     :isTab="true"
                     @close="close"
                     @hideDialogButtons="hideDialogButtons"
                     @addParentSpinning="addSpinning"
                     @delParentSpinning="delSpinning"
                     @refreshParent="refreshParent"
                     @handleOk="handleOk"
                     @setFormTitle="setFormTitle">
      </form-redirect>
    </a-card>
    <view-layout-redirect v-else-if="formType === 'view'"
                          ref="viewRedirect"
                          :formOption="formOption"
                          :view-code="formCode"
                          :stores="stores"
                          :isShowInTab="true"
                          @closeFromView="close"
                          @hideDialogButtonsFromView="hideDialogButtons"
                          @addParentSpinningFromView="addSpinning"
                          @delParentSpinningFromView="delSpinning"
                          @refreshParent="refreshParent"
                          @handleOkFromView="handleOk"
                          @setFormTitleFromView="setFormTitle">
    </view-layout-redirect>
  </a-spin>
</template>

<script>

import FormRedirect from './FormRedirect'
import locleUtil from "../../utils/LocleUtil";

export default {
  name: 'FormRedirectTab',
  components: {
    FormRedirect,
    ViewLayoutRedirect: () => import('./ViewLayoutRedirect')
  },
  props: {
    func: String,
    formCode: String,
    formId: String,
    record: Object,
    formType: {
      type: String,
      default: 'form'
    },
    fks: {
      type: Array,
      default: () => []
    },
    parentValue: {
      type: Object,
      default: () => {
      }
    },
    disQueryDB: {
      type: Boolean,
      default: false // 不查询数据库
    },
    stores: {
      type: Object,
      default: () => {
        return {}
      }
    },
  },
  computed: {
    formOption() {
      return {
        func: this.func,
        formId: this.formId,
        fks: this.fks,
        parentValue: this.parentValue,
        saveInPage: this.saveInPage,
        disQueryDB: this.disQueryDB,
        record: this.record,
        isTab: true,
        needClose: false,
      }
    },
    bodyStyle() {
      if (this.formType === 'view') {
        return {
          background: "#F0F2F5"
        }
      }
      return {}
    },
  },
  data() {
    return {
      spinning: 0,
      saveInPage: false, // 仅在页面暂时存储，用于子表添加
      needClose: false,
    }
  },
  inject: ['closeCurrent', 'removeCache'],
  methods: {
    // 设置标题
    // TODO 暂时没有处理纯列表页面的标题
    setFormTitle(title) {
      let titleName = title || locleUtil.getLocale('m.eform.formDesign.label.biao-dan')
      let funcName = locleUtil.getLocale('m.common.button.add')
      if (this.func === 'edit') {
        funcName = locleUtil.getLocale('m.common.button.update')
      } else if (this.func === 'detail') {
        funcName = locleUtil.getLocale('m.common.title.detail')
      }
      this.$route.meta.title = funcName + " " + titleName
    },
    handleCancel() {
      if (this.spinning > 0) {
        this.needClose = true
        return;
      }
      this.closeCurrent()
    },
    handleOk($vue,isDraft) {
      const that = this;
      this.addSpinning();
      $vue.save(this.afterSave,null,isDraft)
      setTimeout(function () {
        that.delSpinning();
      }, 500)
    },
    afterSave(id,saveSucessMsg) {
      if(saveSucessMsg){
        this.$message.success(saveSucessMsg)
      }else{
        this.$message.success(locleUtil.getLocale('m.eform.formDesign.label.bao-cun-cheng-gong'))
      }
      // 不关闭当前页面，后续改为可配置参数
      // this.closeCurrent()
    },
    hideDialogButtons() {
    },
    //关闭页面
    close() {
      this.handleCancel()
    },
    //刷新父页面
    refreshParent(id, tableName, formData) {
      this.removeCache(this.$route.meta.parentFullPath)
    },
    addSpinning() {
      this.spinning++
    },
    delSpinning() {
      this.spinning--
      if (this.spinning === 0 && this.needClose) {
        this.needClose = false
        this.handleCancel()
      }
    },
  },
  activated() {
    if (this.formType === 'view') {
      if (this.$refs.viewRedirect) {
        this.$refs.viewRedirect.doResize();
      }
    }
  }
}
</script>
