<template>
  <section class="app-main">
    <!--    <transition name="fade-transform" mode="out-in">-->
    <!--      <keep-alive :include="cachedViews">-->
    <!--        <router-view :key="key" />-->
    <!--      </keep-alive>-->
    <!--    </transition>-->
    <transition
      :name="setTransitionName"
      mode="out-in"
    >
      <keep-alive :include="keepAliveNameList">
        <router-view :key="refreshRouterViewKey" />
      </keep-alive>
    </transition>

    <route-iframe
      v-show="isIframe"
      key="iframe"
    />
  </section>
</template>

<script>
import RouteIframe from '../RouteIframe'
export default {
  name: 'AppMain',
  components: { RouteIframe },
  data() {
    return {
      refreshRouterViewKey: null,
      keepAliveNameList: []
    }
  },
  computed: {
    cachedViews() {
      return this.$store.state.tagsView.cachedViews
    },
    key() {
      return this.$route.path
    },
    isIframe() {
      return this.$route.name === 'iframeRoute'
    },
    setTransitionName() {
      return 'fade'
    }
  },
  watch: {
    key(val) {
      this.refreshRouterViewKey = val
    }
  },
  created() {
    this.keepAliveNameList = this.getKeepAliveNames()
    this.$events.$on('onTagsViewRefreshRouterView', (fullPath) => {
      if (this.$route.fullPath !== fullPath) return false
      this.keepAliveNameList = this.getKeepAliveNames().filter(
        (name) => this.$route.name !== name
      )
      this.refreshRouterViewKey = this.$route.fullPath
      this.$nextTick(() => {
        this.refreshRouterViewKey = null
        this.keepAliveNameList = this.getKeepAliveNames()
      })
    })
  },
  // 保证每次页面更新都会保存【需要缓存的页面】
  updated() {
    this.$nextTick(() => {
      // this.refreshRouterViewKey = null
      this.keepAliveNameList = this.getKeepAliveNames()
    })
  },
  methods: {
    getKeepAliveNames() {
      return this.$store.state.tagsView.cachedViews
    }
  }
}
</script>

<style lang="scss" scoped>
.app-main {
  /* 50= navbar  50  */
  min-height: calc(100vh - 50px);
  width: 100%;
  position: relative;
  overflow: hidden;
}

.fixed-header + .app-main {
  padding-top: 50px;
}

.hasTagsView {
  .app-main {
    /* 84 = navbar + tags-view = 50 + 34 */
    min-height: calc(100vh - 84px);
  }

  .fixed-header + .app-main {
    padding-top: 84px;
  }
}
</style>

<style lang="scss">
// fix css style bug in open el-dialog
.el-popup-parent--hidden {
  .fixed-header {
    padding-right: 15px;
  }
}
</style>
