<template>
  <el-breadcrumb class="app-breadcrumb" separator="/">
    <transition-group name="breadcrumb">
      <el-breadcrumb-item
        v-for="(item, index) in filteredLevelList"
        :key="item.path"
      >
        <span
          v-if="item.redirect === 'noredirect' || index === filteredLevelList.length - 1"
          class="no-redirect"
        >
          {{ translateMenuTitle(item.meta.title) }}
        </span>
        <router-link v-else :to="item.redirect || item.path">
          {{ translateMenuTitle(item.meta.title) }}
        </router-link>
      </el-breadcrumb-item>
    </transition-group>
  </el-breadcrumb>
</template>

<script>
import { translateMenuTitle } from '@/utils/i18n'

export default {
  data() {
    return {
      levelList: null
    }
  },
  computed: {
    // 过滤掉没有 title 的项
    filteredLevelList() {
      return this.levelList.filter(item => item.meta?.title)
    }
  },
  watch: {
    $route() {
      this.getBreadcrumb()
    }
  },
  created() {
    this.getBreadcrumb()
  },
  methods: {
    translateMenuTitle,
    getBreadcrumb() {
      let matched = this.$route.matched.filter(item => item.name)
      const first = matched[0]
      if (first && first.name !== 'dashboard') {
        matched = [{ path: '/dashboard', meta: { title: '主页' }}].concat(matched)
      }
      this.levelList = matched
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
  .app-breadcrumb.el-breadcrumb {
    display: inline-block;
    font-size: 14px;
    line-height: 50px;
    margin-left: 10px;
    color: #fff;
    .no-redirect {
      color: #fff;
      cursor: text;
    }
  }
  .el-breadcrumb__inner a, .el-breadcrumb__inner.is-link {
    color: #fff!important;
  }
</style>
