<script setup lang="ts">
import { computed } from 'vue'
import { icons } from '@/utils/common'
import { ArrowRight } from '@element-plus/icons-vue'
import { useRoute, useRouter } from 'vue-router'
import { GlobalStore } from '@/stores/index'

const route = useRoute()
const router = useRouter()
const globalStore = GlobalStore()

const breadcrumbList: any = computed(() => {
  const { path, name } = route
  const BreadcrumbList = getExtraBreadcrumb(name as string)
  if (BreadcrumbList) return BreadcrumbList
  return globalStore.GET_AllBreadcrumbList[path]
})
// console.log(globalStore.GET_AllBreadcrumbList)
/**
 * 获取额外的面包屑
 * @param name 路由名称
 */
const getExtraBreadcrumb = (name: string) => {
  switch (name) {
    case 'ContractDetail':
      return getBreadcrumb('/contractDetail', 'ue628', '/contract')
    case 'InStorageDetail':
      return getBreadcrumb('/inStorageDetail', 'ue60f', '/inStorage')
    case 'ReceiveOutStock':
      return getBreadcrumb('/receiveOutStock', 'ue7ec', '/outStock')
    case 'SaleOutStock':
      return getBreadcrumb('/saleOutStock', 'ue7e8', '/outStock')
    case 'OutStockDetail':
      return getBreadcrumb('/outStockDetail', 'ue72c', '/outStock')
    case 'VerifyContractDetail':
      return getBreadcrumb('/verifyContractDetail', 'ue679', '/verifyContract')
  }
}
/**
 * 获取面包屑
 */
const getBreadcrumb = (path: string, icon: string, view: string) => {
  const routerList = [...globalStore.GET_AllBreadcrumbList[view]]
  routerList.push({ meta: { ...route.meta, icon }, path })
  return routerList
}

const isUnicode = (val: string | undefined) => {
  if (!val) return false
  return !/ue/g.test(val)
}

const onBreadcrumbClick = (item: any, index: number) => {
  if (item.meta.routerView) return
  if (index !== breadcrumbList.value.length - 1) router.push(item.path)
}
</script>

<template>
  <div class="breadcrumb-box">
    <el-breadcrumb :separator-icon="ArrowRight">
      <!-- animate__slideInUp -->
      <transition-group
        appear
        leave-active-class="animate__animated animate__slideInUp"
        enter-active-class="animate__animated animate__slideInDown"
      >
        <el-breadcrumb-item
          v-for="(item, index) in breadcrumbList"
          :key="item.path"
          :class="{ cursor: item.meta.routerView }"
        >
          <div class="el-breadcrumb__inner is-link" @click="onBreadcrumbClick(item, index)">
            <el-icon class="breadcrumb-icon" v-if="isUnicode(item.meta.icon as string)">
              <component :is="item.meta.icon"></component>
            </el-icon>
            <i v-else class="iconfont el-icon breadcrumb-icon">{{ icons[item.meta.icon] }}</i>
            <span class="breadcrumb-title">{{ item.meta.title }}</span>
          </div>
        </el-breadcrumb-item>
      </transition-group>
    </el-breadcrumb>
  </div>
</template>

<style scoped lang="scss">
.breadcrumb-box {
  display: flex;
  align-items: center;
  padding-right: 50px;
  overflow: hidden;
  mask-image: linear-gradient(90deg, #000000 0%, #000000 calc(100% - 50px), transparent);
  color: #fff;
  .iconfont {
    width: 16px;
    color: #fff;
  }
  .cursor.el-breadcrumb__item {
    cursor: auto;
    .el-breadcrumb__inner {
      cursor: auto;
      .breadcrumb-title {
        cursor: auto;
      }
    }
  }
  .el-breadcrumb {
    white-space: nowrap;
    .el-breadcrumb__item {
      position: relative;
      display: inline-block;
      float: none;
      .el-breadcrumb__inner {
        display: inline-flex;
        color: #fff;
        .breadcrumb-icon {
          margin-right: 6px;
          font-size: 16px;
        }
      }
      :deep(.el-breadcrumb__separator) {
        position: relative;
      }
    }
  }
}
.no-icon {
  .el-breadcrumb {
    .el-breadcrumb__item {
      top: -2px;
      :deep(.el-breadcrumb__separator) {
        top: 2px;
      }
    }
  }
}
</style>
