<template>
  <div v-if="!menu.meta || !menu.meta.hidden" class="sidebar-item">
    <el-sub-menu
      v-if="menu?.children && menu?.children.length > 0"
      :index="menu.path"
      popper-class="sidebar-container-popper"
    >
      <!-- 父级级菜单 -->
      <template #title>
        <lz-svg-icon v-if="menu.meta && menu.meta.icon" :icon-class="menuIcon" class="sidebar-icon" />
        <span>{{ menu.meta?.title }}</span>
      </template>
      <!-- 递归 -->
      <sidebar-item
        v-for="(child, index) in menu?.children"
        :key="index"
        :menu="child"
        :activeMenu="activeMenu"
      >
      </sidebar-item>
    </el-sub-menu>
    <!-- 当前菜单-->
    <app-link
      v-else-if="menu.meta && !menu.meta.hidden"
      :to="{ path: menu.path }"
    >
      <el-menu-item
        ref="subMenu"
        :index="menu.path"
        popper-class="sidebar-popper"
      >
        <template #title>
          <lz-svg-icon v-if="menu.meta && menu.meta.icon" :icon-class="menuIcon" class="sidebar-icon" />
          <span v-if="menu.meta && menu.meta.title">{{ menu.meta.title }}</span>
        </template>
      </el-menu-item>
  </app-link>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { type RouteRecordRaw } from 'vue-router'

defineOptions({ name: 'SidebarItem', inheritAttrs: false })

const props = defineProps<{
  menu: RouteRecordRaw
  activeMenu: any
}>()

// const router = useRouter()

// function clickHandle(item: any) {
//   if (item.path === '/docs/chat') {
//     router.push({ path: `/docs/chat?agentId=${localStorage.getItem('agentId')}` })
//   }
// }
const menuIcon = computed(() => {
  if (props.activeMenu === props.menu.path) {
    return props.menu.meta?.iconActive || props.menu?.meta?.icon
  } else {
    return props.menu?.meta?.icon
  }
})
</script>

<style scoped lang="scss">
.sidebar-item {
  .sidebar-icon {
    font-size: 20px;
    margin-top: -2px;
  }
  .el-menu-item {
    padding: 13px 12px 13px 16px !important;
    font-weight: 500;
    border-radius: 4px;
    &:hover {
      background: none;
      color: var(--el-color-primary);
    }
  }
  :deep(.el-sub-menu__title) {
    padding: 13px 12px 13px 16px !important;
    &:hover {
      background: none;
      color: var(--el-color-primary);
    }
  }
  .el-sub-menu {
    .el-menu-item {
      padding-left: 43px !important;
    }
  }
  .el-menu-item.is-active {
    color: var(--el-color-primary);
    background: var(--el-color-primary-light-9);
  }
}
</style>
