<template>
    <el-breadcrumb class="tjm-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 == levelList.length - 1" 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>
</template>

<script setup>
const route = useRoute()
const router = useRouter()
const levelList = ref([])

function getBreadcrumb() {
    let matched = route.matched.filter(item => item.meta && item.meta.title);
    
    if (!isDashboard(matched[0]) && route) matched.unshift({ path: "/home/index", meta: { title: "首页" }, redirect: "/" });
    if (route && route.path.trim() === "/publicDomain") {
        matched[matched.length - 1].meta.title = route.query.zcType && route.query.zcType.trim() || route.name;
        matched[matched.length - 2].meta.title = route.query.inWhType && route.query.inWhType.includes("解读") && "领域解读" || route.name;
    }

    levelList.value = matched.filter(item => item.meta && item.meta.title && item.meta.breadcrumb !== false)
}
function isDashboard(route) {
    return route && route.name && route.name.trim() === "Index"
}
function handleLink({ redirect }) {
    if (redirect && redirect != route.path) redirect && router.push({ path: redirect });
}

watchEffect(() => {
    if (route.path.startsWith("/redirect/")) return
    getBreadcrumb()
})
getBreadcrumb()
</script>

<style lang="scss" scoped>
.tjm-breadcrumb.el-breadcrumb {
  display: inline-block;
  font-size: 14px;
  line-height: 50px;
  margin-left: 8px;

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