<template>
  <el-breadcrumb class="app-breadcrumb" separator="/">
    <transition-group name="breadcrumb">
      <el-breadcrumb-item v-for="(item, index) in levelList" :key="item.path">
        <span v-if="item.redirect === 'noRedirect' || index == levelList.length - 1" class="no-redirect">{{
          item.meta.title }}</span>
        <a v-else @click.prevent="handleLink(item)">{{ item.meta.title }}</a>
      </el-breadcrumb-item>
    </transition-group>
  </el-breadcrumb>
</template>

<script>
import { mapGetters } from "vuex";

export default {
  data() {
    return {
      levelList: null
    }
  },
  watch: {
    $route(route) {
      // if you go to the redirect page, do not update the breadcrumbs
      if (route.path.startsWith('/redirect/')) {
        return
      }
      this.getBreadcrumb()
    }
  },
  created() {
    this.getBreadcrumb()
  },
  computed: {
    ...mapGetters(["sidebarRouters"]),
  },
  methods: {
    getBreadcrumb() {
      // only show routes with meta.title
      // 当前全部侧边栏
      let sidebarRouters = this.sidebarRouters
      let matched = this.$route.matched.filter(item => item.meta && item.meta.title)
      const first = matched[0]
      // sidebarRouters.forEach(element => {
      //   if (element.children && element.children.length != 0) {
      //     // 提取带childen的侧边栏
      //     if (element.children.find(item => item.name == this.$route.name)) {
      //       // 插入
      //       matched.splice(1, 0, element); // 在索引为 1 的位置插入 newObj
      //     }
      //   }
      // });
      if (!this.isDashboard(first)) {
        // matched = [{ path: '/index', meta: { title: '首页' }}].concat(matched)
      }
      // console.log(sidebarRouters);
      if(matched[0].path == "/assetsAuth" ) {
        // 管理端--资产管理--新增资产
        const activeRoute = sidebarRouters.filter(item => item.path == '/' && item.children[0].path == 'assets')
        this.levelList = [activeRoute[0].children[0],matched[0]]
      } else if(matched[0].path == "/taskInfo" || matched[0].path == "/taskAudit") {
        // 管理端--任务管理--任务审核/任务详情
        const activeRoute = sidebarRouters.filter(item => item.path == '/' && item.children[0].path == 'task')
        this.levelList = [activeRoute[0].children[0],matched[0]]
      } else if(matched[0].path == "/mytaskInfo" || matched[0].path == "/mytaskAudit") {
        // 单位端--我的任务--资产核查/任务详情
        const activeRoute = sidebarRouters.filter(item => item.path == '/' && item.children[0].path == 'myTask')
        this.levelList = [activeRoute[0].children[0],matched[0]]
      } else if(matched[0].path == "/myAssetsAuth") {
        // 单位端--我的资产--资产详情
        const activeRoute = sidebarRouters.filter(item => item.path == '/' && item.children[0].path == 'myAssets')
        this.levelList = [activeRoute[0].children[0],matched[0]]
      } else if(matched[0].path == "/taskAuditInfo") {
        // 管理端--任务管理--任务审核--资产审核
        const activeRoute = sidebarRouters.filter(item => item.path == '/' && item.children[0].path == 'task')
        const route = sidebarRouters.filter(item => item.path == '')
        const twoActiveRoute = route[0].children.filter(item => item.path == "taskAudit")
        this.levelList = [activeRoute[0].children[0],twoActiveRoute[0],matched[0]]
      } else if(matched[0].path == "/mytaskAuditInfo") {
        // 单位端--我的任务--资产核查--资产审核详情
        const activeRoute = sidebarRouters.filter(item => item.path == '/' && item.children[0].path == 'myTask')
        const route = sidebarRouters.filter(item => item.path == '')
        const twoActiveRoute = route[0].children.filter(item => item.path == "mytaskAudit")
        this.levelList = [activeRoute[0].children[0],twoActiveRoute[0],matched[0]]
      } else {
        this.levelList = matched.filter(item => item.meta && item.meta.title && item.meta.breadcrumb !== false)
      }
    },
    isDashboard(route) {
      const name = route && route.name
      if (!name) {
        return false
      }
      return name.trim() === 'Index'
    },
    handleLink(item) {
      const { redirect, path } = item
      if (redirect) {
        this.$router.push(redirect)
        return
      }
      this.$router.push(path)
    }
  }
}
</script>

<style lang="scss" scoped>
.app-breadcrumb.el-breadcrumb {
  display: inline-block;
  font-size: 14px;
  line-height: 50px;
  margin-left: 8px;

  .no-redirect {
    color: #97a8be;
    cursor: text;
  }
}
</style>
