<template>
  <div v-if="isShowBreadcrumb" class="breadcrumb">
    <SvgIcon :icon-class="breadcrumb[0].meta.icon" />
    <ul>
      <li v-for="({ meta: { title }, path, redirect }, i) in breadcrumb" :key="title">
        <span class="separator">/</span>
        <span :key="i" class="breadcrumb-name" @click="$router.push(path.length > 0 ? path : redirect)">
          {{ title }}
        </span>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  name: 'LayoutBreadcrumb',
  data() {
    return {
      breadcrumb: [],
    };
  },
  computed: {
    isShowBreadcrumb() {
      return this.breadcrumb.length > 0;
    },
  },
  watch: {
    $route() {
      this.getBreadcrumb();
    },
  },
  created() {
    this.getBreadcrumb();
  },
  methods: {
    getBreadcrumb() {
      this.breadcrumb = this.$route.matched.filter((item) => item?.meta && item.meta.title);
      this.$emit('update:isShowBreadcrumb', this.isShowBreadcrumb);
    },
  },
};
</script>

<style lang="scss" scoped>
.breadcrumb {
  display: flex;
  align-items: center;
  height: 56px;
  padding: 16px 24px;

  > ul {
    display: flex;

    li {
      font-weight: 400;
      color: $font-light-color;

      .separator {
        margin: 0 8px;
        color: #c9cdd4;
      }

      .breadcrumb-name {
        font-size: 14px;
      }

      &:not(:last-child) {
        cursor: pointer;
      }

      &:nth-last-child(1) {
        font-weight: bold;
        color: $font-color;
      }
    }
  }
}
</style>
