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

<script>
export default {
  data() {
    return {
      levelList: null
    }
  },
  computed: {
    changeColor() {
      // console.log(this.$route);
      if (this.$route.path == '/copilot/efficiency-container' || this.$route.path == '/copilot/main' || this.$route.path == '/factoryData/hdIndex' || this.$route.path === '/factoryData/factory-data') {
        return true
      } else {
        return false
      }
    }
  },
  watch: {
    changeColor(val) {
      if (val == true) {
        let item = document.getElementsByClassName('el-breadcrumb__separator')
        // let item  =document.getElementsByClassName('')
        item[0].style.color = rgba(255, 255, 255, 0.45)
        item[1].style.color = rgba(255, 255, 255, 0.45)
      } else {
        let item = document.getElementsByClassName('el-breadcrumb__separator')
        item[0].style.color = ''
          item[1].style.color = ''
      }
    },
    $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()
  },
  methods: {
    getBreadcrumb() {
      // only show routes with meta.title
      let matched = this.$route.matched.filter(item => item.meta && item.meta.title)
      const first = matched[0]

      if (!this.isDashboard(first)) {
        matched = [{ path: '/index', meta: { title: '首页' } }].concat(matched)
      }

      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'
      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: 48px;
  margin-left: 8px;

  .no-redirect {
    color: rgba(0, 0, 0, .45);
    cursor: text;
  }
}
.app-breadcrumb .el-breadcrumb__inner a,
.el-breadcrumb__inner.is-link {
  color:rgba(0, 0, 0, 0.65)
}
</style>
<!-- <style lang="scss">
.changeColor {
  --theme-color: rgba(255, 255, 255, 0.45);
}
.el-breadcrumb__separator {
  color:var(--theme-color);
}
</style> -->
