<template>
  <div
    style="display: flex; justify-content: space-between; align-items: center"
  >
    <div class="breadcrumb">
      <icon-local theme="filled" size="22" fill="#10a7e5" :strokeWidth="3" />
      <el-breadcrumb class="app-breadcrumb" separator=">">
        <transition-group name="breadcrumb">
          <el-breadcrumb-item
            v-for="(item, index) in data.breadcrumbList"
            :key="item.path"
          >
            <router-link v-if="index === 0" :to="item.path">
              {{ item.meta.title }}
            </router-link>
            <span v-else-if="index !== data.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 class="return">
      <el-button
        type="primary"
        text
        link
        @click="$router.back()"
        v-if="
          route.meta?.activeMenu &&
          route.path &&
          route.meta?.activeMenu !== route.path
        "
      >
        返回上一页
      </el-button>
    </div>
  </div>
</template>

<script setup>
import { reactive, watch } from "vue";
import { useRoute } from "vue-router";

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

<style lang="scss" scoped>
.breadcrumb {
  margin-top: 10px;
  margin-left: 20px;
  display: flex;
  align-items: center;

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

.return {
  margin-right: 20px;
}
</style>
