<template>
  <div class="page-content corporate-customers">
    <transition-group
      :name="transitionName"
      appear
      class="transition-group"
      tag="div"
    >
      <!--   表格页面   -->
      <table-page
        v-show="componentName === 'TablePage'"
        key="TablePage"
        :componentName="componentName"
        @changePage="changePage"
      />
      <!--   新增修改页面   -->
      <edit-add-page
        v-show="componentName === 'EditAddPage'"
        key="EditAddPage"
        :componentName="componentName"
        @changePage="changePage"
      />
      <!--   详情页面   -->
      <detail-page
        v-show="componentName === 'DetailPage'"
        key="DetailPage"
        :componentName="componentName"
        @changePage="changePage"
      />
      <abnormalListPage
        v-show="componentName === 'AbnormalListPage'"
        key="AbnormalListPage"
        :componentName="componentName"
        @changePage="changePage"
      />
      <add-page
        v-show="componentName === 'AddPage'"
        key="AddPage"
        :componentName="componentName"
        @changePage="changePage"
      />
    </transition-group>
  </div>
</template>

<script>
import AddPage from './components/AddPage/index.vue'
import AbnormalListPage from './components/AbnormalListPage/index.vue'
import TablePage from './components/TablePage/index.vue'
import EditAddPage from './components/EditAddPage/index.vue'
import DetailPage from './components/DetailPage/index.vue'
import PubSub from 'pubsub-js'

export default {
  name: 'EventManagement',
  components: {TablePage, EditAddPage, DetailPage, AbnormalListPage, AddPage},
  data() {
    return {
      componentName: 'TablePage',
      pubSub: null,
      transitionName: '',
    }
  },
  mounted() {
    // 添加自定义面包屑pub-sub
    this.pubSub = PubSub.subscribe('breadcrumb-click', (_, item) => {
      if (item.compName) {
        this.componentName = item.compName
      } else if (item.isCurrentRoute) {
        this.componentName = 'TablePage'
      }
    })
  },
  methods: {
    // pageName显示页面标识符 done回调函数，保证页面挂载后执行的逻辑
    /**
     * @param {String} pageName 显示页面标识符(必传)
     * @param {Function} done 回调函数(非必传)
     * @param {String} type 面包屑标题(非必传)
     * */
    changePage(pageName = '', done, type = '') {
      this.componentName = pageName
      this.transitionName =
        this.transitionName === 'fold-right' ? 'fold-left' : 'fold-right'
      done && done()
      switch (pageName) {
        case 'TablePage':
          PubSub.publish('go-back')
          break
        case 'EditAddPage':
          PubSub.publish('add-custom-breadcrumb', type || '编辑')
          break
        case 'DetailPage':
          PubSub.publish('add-custom-breadcrumb', type || '详情')
          break
        case 'AbnormalListPage':
          // 可跳转面包屑组件
          PubSub.publish('add-custom-breadcrumb', {
            name: '进展上报',
            compName: pageName,
          })
          break
        case 'AbnormalListPageBack':
          PubSub.publish('go-back')
          this.componentName = 'AbnormalListPage'
          break
        case 'AddPage':
          PubSub.publish('add-custom-breadcrumb', type || '详情')
      }
    },
  },
  beforeDestroy() {
    // 在组件销毁之前 清除订阅消息
    PubSub.unsubscribe(this.pubSub)
  },
}
</script>

<style lang="scss" scoped>
</style>
