<template>
  <div style="display: flex; justify-content: space-between; align-items: center">
    <div class="breadcrumb">
      <el-breadcrumb class="app-breadcrumb" separator=">">
        <transition-group name="breadcrumb">
          <el-breadcrumb-item v-for="(item, index) in breadcrumbList" :key="item.path">
            <router-link v-if="index === 0" :to="item.path">
              {{ item.meta.title }}
            </router-link>
            <span v-else-if="index !== breadcrumbList.length - 1">
              {{ item.meta.title }}
            </span>
            <span v-else class="no-redirect">{{ item.meta.title }}</span>
          </el-breadcrumb-item>
        </transition-group>
      </el-breadcrumb>
    </div>
  </div>
</template>

<script setup>
import { ref, watch } from "vue";
import { useRoute } from "vue-router";
import { ElBreadcrumb, ElBreadcrumbItem } from 'element-plus'
import "element-plus/es/components/breadcrumb/style/css";
import "element-plus/es/components/breadcrumb-item/style/css";

defineOptions({
  name: "LayoutBreadcrumb",
});
const route = useRoute();
const breadcrumbList = ref([]);
const fnGetBreadcrumb = () => {
  const matched = route.matched.filter((item) => item.meta?.title);
  if (matched[0].path === "/") matched[0].path = "/index";
  breadcrumbList.value = matched.filter(
    (item) => item.meta?.title && item.meta?.breadcrumb !== false
  );
};
fnGetBreadcrumb();
watch(
  () => route,
  () => fnGetBreadcrumb(),
  { deep: true }
);
</script>

<style lang="scss" scoped>
.app-breadcrumb.el-breadcrumb {
  font-size: 14px;
  line-height: var(--el-header-height);

  .no-redirect {
    color: #97a8be;
    cursor: text;
  }

  .el-breadcrumb__inner a {
    font-weight: normal;
  }
}
</style>
