<template>
  <div class="Breadcrumb">
    <i
      class="el-icon-d-arrow-left fl"
      :class="{ cor: isShow }"
      @click="back_arrow"
    />
    <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 > 0"
            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>
  </div>
</template>

<script>
import pathToRegexp from 'path-to-regexp'

export default {
  name: 'Breadcrumb',
  data() {
    return {
      levelList: null,
      isShow: false,
      backOtions: '',
      backUrl: ''
    }
  },
  watch: {
    $route(route) {
      // if you go to the redirect page, do not update the breadcrumbs
      if (route.path.startsWith('/redirect/')) {
        this.isShow = false
        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: '/welcome/dashboard', meta: { title: '首页' }}
        ].concat(matched)
      }

      this.levelList = matched.filter(
        (item) => item.meta && item.meta.title && item.meta.breadcrumb !== false
      )
      const { preModel } = this.$route.query
      if (preModel) {
        const model = JSON.parse(preModel)
        this.isShow = true
        const { options, router } = model
        this.backOtions = JSON.stringify(options)
        this.backUrl = router
      } else {
        this.isShow = false
        // this.backOtions = "";
      }
      console.log('active' + this.backOtions)
    },
    isDashboard(route) {
      const name = route && route.name
      if (!name) {
        return false
      }
      return (
        name.trim().toLocaleLowerCase() === 'Dashboard'.toLocaleLowerCase()
      )
    },
    pathCompile(path) {
      const { params } = this.$route
      var toPath = pathToRegexp.compile(path)
      return toPath(params)
    },
    back_arrow() {
      if (!this.isShow) {
        return
      }
      console.log('wantto' + this.backOtions)
      window.vm.$router.push({
        path: this.backUrl,
        query: {
          back: this.backOtions
        }
      })
    },
    handleLink(item) {
      const { redirect, path } = item
      if (redirect) {
        window.vm.$router.push(redirect)
        return
      }
      window.vm.$router.push(this.pathCompile(path))
    }
  }
}
</script>

<style lang="scss" scoped>
.Breadcrumb {
  .el-icon-d-arrow-left {
    display: inline-block;
    font-size: 14px;
    line-height: 50px;
    margin: 0 8px 0 1px;
    color: transparent;
    &.cor {
      color: #000;
      &:hover {
        cursor: pointer;
      }
    }
  }
  .app-breadcrumb.el-breadcrumb {
    display: inline-block;
    font-size: 14px;
    line-height: 50px;

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