<template>
  <div v-if="!item.hidden">
    <template v-if="
      hasOneShowingChild(item.children, item) &&
      (!onlyOneChild.children || onlyOneChild.noShowingChildren) &&
      !item.alwaysShow
    ">
      <app-link v-if="onlyOneChild.meta" :to="resolvePath(onlyOneChild.path, onlyOneChild.query)">
        <el-menu-item :index="resolvePath(onlyOneChild.path)" :class="{ 'submenu-title-noDropdown': !isNest }">
          <!-- <svg-icon :icon-class="onlyOneChild.meta.icon || (item.meta && item.meta.icon)" /> -->
          <!-- {{ onlyOneChild.meta.title }} -->
          <!-- <template #title> -->
          <span class="menu-title" :title="hasTitle(onlyOneChild.meta.title)">
            {{ onlyOneChild.meta.title }}
          </span>
          <!-- </template> -->
        </el-menu-item>
      </app-link>
    </template>

    <el-sub-menu v-else ref="subMenu" :index="resolvePath(item.path)" popper-append-to-body>
      <template v-if="item.meta" #title>
        <!-- <svg-icon :icon-class="item.meta && item.meta.icon" /> -->
        <span class="menu-title" :title="hasTitle(item.meta.title)">{{ item.meta.title }}</span>
      </template>

      <sidebar-item v-for="child in item.children" :key="child.path" :is-nest="true" :item="child"
        :base-path="resolvePath(child.path)" class="nest-menu" />
    </el-sub-menu>
  </div>
</template>

<script setup>
import { isExternal } from '@/utils/validate.js'
import AppLink from './Link.vue'
import { getNormalPath } from '@/utils/ruoyi'

const props = defineProps({
  // route object
  item: {
    type: Object,
    required: true,
  },
  isNest: {
    type: Boolean,
    default: false,
  },
  basePath: {
    type: String,
    default: '',
  },
})

const onlyOneChild = ref({})

function hasOneShowingChild(children = [], parent) {
  if (!children) {
    children = []
  }
  const showingChildren = children.filter((item) => {
    if (item.hidden) {
      return false
    } else {
      // Temp set(will be used if only has one showing child)
      onlyOneChild.value = item
      return true
    }
  })

  // When there is only one child router, the child router is displayed by default
  if (showingChildren.length === 1) {
    return true
  }

  // Show parent if there are no child router to display
  if (showingChildren.length === 0) {
    onlyOneChild.value = { ...parent, path: '', noShowingChildren: true }
    return true
  }

  return false
}

function resolvePath(routePath, routeQuery) {
  if (isExternal(routePath)) {
    return routePath
  }
  if (isExternal(props.basePath)) {
    return props.basePath
  }
  if (routeQuery) {
    let query = JSON.parse(routeQuery)
    return { path: getNormalPath(props.basePath + '/' + routePath), query: query }
  }
  return getNormalPath(props.basePath + '/' + routePath)
}

function hasTitle(title) {
  if (title.length > 5) {
    return title
  } else {
    return ''
  }
}
</script>

<style scoped>
/* 优化：侧边栏菜单项样式 */
.sidebar-item-wrapper {
  width: 100%;
  transition: all 0.3s ease;
}

/* 优化：菜单项基础样式 */
:deep(.el-menu-item) {
  display: flex;
  align-items: center;
  padding: 0 20px !important;
  margin: 4px 8px;
  height: 48px;
  line-height: 48px;
  border-radius: 8px;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative;
  overflow: hidden;
}

/* 优化：悬停效果 */
:deep(.el-menu-item:hover) {
  background-color: var(--el-menu-hover-bg-color, rgba(64, 158, 255, 0.1)) !important;
  transform: translateX(4px);
}

/* 优化：激活菜单项样式 */
:deep(.el-menu-item.is-active) {
  background: var(--el-color-primary-light-9);
  /* background: linear-gradient(90deg, var(--el-color-primary-light-9) 0%, transparent 100%);   */
  color: var(--el-color-primary) !important;
  font-weight: 500;
  border-right: none;
  position: relative;
}

/* 优化：激活指示器 */
.menu-active-indicator {
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 3px;
  height: 20px;
  background: var(--el-color-primary);
  /* background: linear-gradient(180deg, var(--el-color-primary) 0%, var(--el-color-primary-light-3) 100%);  */
  border-radius: 0 4px 4px 0;
  opacity: 0;
  transition: opacity 0.2s ease;
}

:deep(.el-menu-item.is-active) .menu-active-indicator {
  opacity: 1;
}

/* 优化：图标样式 */
.menu-icon-wrapper {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  margin-right: 12px;
  transition: all 0.3s ease;
}

.menu-icon {
  width: 18px;
  height: 18px;
  color: currentColor;
  transition: transform 0.3s ease;
}

:deep(.el-menu-item:hover) .menu-icon {
  transform: scale(1.1);
}

.menu-icon-placeholder {
  width: 18px;
  height: 18px;
}

/* 优化：标题样式 */
.menu-title {
  flex: 1;
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: all 0.3s ease;
  letter-spacing: 0.3px;
}

/* 优化：子菜单样式 */
:deep(.el-sub-menu) {
  margin: 4px 0;
}

/* 优化：子菜单标题 */
:deep(.el-sub-menu__title) {
  height: 48px;
  line-height: 48px;
  padding: 0 20px !important;
  margin: 0 8px;
  border-radius: 8px;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  position: relative;
  overflow: hidden;
}

:deep(.el-sub-menu__title:hover) {
  background-color: var(--el-menu-hover-bg-color, rgba(64, 158, 255, 0.1)) !important;
  transform: translateX(4px);
}

/* 优化：子菜单标题包装器 */
.submenu-title-wrapper {
  display: flex;
  align-items: center;
  width: 100%;
  position: relative;
}

/* 优化：子菜单箭头样式 */
.submenu-arrow {
  margin-left: auto;
  transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  display: flex;
  align-items: center;
  justify-content: center;
  width: 20px;
  height: 20px;
  color: var(--el-text-color-secondary);
}

:deep(.el-sub-menu.is-opened) .submenu-arrow {
  transform: rotate(90deg);
  color: var(--el-color-primary);
}

/* 优化：子菜单容器 */
.submenu-children-wrapper {
  padding-left: 32px;
  position: relative;
}

/* 优化：子菜单项样式 */
:deep(.nest-menu .el-menu-item) {
  padding-left: 44px !important;
  font-size: 13px;
  background: transparent;
  margin: 2px 8px;
  height: 42px;
  line-height: 42px;
  position: relative;
}

:deep(.nest-menu .el-menu-item::before) {
  content: '';
  position: absolute;
  left: 28px;
  top: 50%;
  transform: translateY(-50%);
  width: 4px;
  height: 4px;
  border-radius: 50%;
  background-color: var(--el-text-color-secondary);
  opacity: 0.5;
  transition: all 0.3s ease;
}

:deep(.nest-menu .el-menu-item:hover::before) {
  background-color: var(--el-color-primary);
  opacity: 1;
  transform: translateY(-50%) scale(1.5);
}

:deep(.nest-menu .el-menu-item.is-active::before) {
  background-color: var(--el-color-primary);
  opacity: 1;
  transform: translateY(-50%) scale(2);
  box-shadow: 0 0 8px var(--el-color-primary);
}

/* 优化：popper菜单样式（针对水平折叠模式） */
:deep(.el-popper.is-pure) {
  border: none !important;
  box-shadow: 0 6px 16px -8px rgba(0, 0, 0, 0.08), 0 9px 28px 0 rgba(0, 0, 0, 0.05), 0 12px 48px 16px rgba(0, 0, 0, 0.03) !important;
  border-radius: 8px !important;
  overflow: hidden;
  padding: 4px 0 !important;
}

:deep(.el-popper .el-menu--vertical) {
  background-color: var(--el-bg-color-overlay);
  border: 1px solid var(--el-border-color-light);
  border-radius: 8px;
}

:deep(.el-popper .el-menu-item) {
  min-width: 160px;
  height: 40px;
  line-height: 40px;
  margin: 2px 4px;
  padding: 0 12px !important;
  border-radius: 6px;
}

/* 优化：折叠时的特殊样式 */
:deep(.el-menu--collapse) .menu-title {
  display: none;
}

:deep(.el-menu--collapse) .menu-icon-wrapper {
  margin-right: 0;
}

:deep(.el-menu--collapse) .el-sub-menu__title {
  padding: 0 16px !important;
  justify-content: center;
}

/* 优化：动画效果 */
@keyframes menuItemFadeIn {
  from {
    opacity: 0;
    transform: translateX(-10px);
  }

  to {
    opacity: 1;
    transform: translateX(0);
  }
}

.sidebar-item-wrapper {
  animation: menuItemFadeIn 0.3s ease;
}

/* 优化：暗色主题适配 */
:root[data-theme='dark'] {
  --el-menu-hover-bg-color: rgba(255, 255, 255, 0.05);
}

/* 优化：响应式调整 */
@media screen and (max-width: 768px) {

  :deep(.el-menu-item),
  :deep(.el-sub-menu__title) {
    height: 44px;
    line-height: 44px;
  }

  .menu-title {
    font-size: 13px;
  }
}

/* 优化：打印样式 */
@media print {
  .sidebar-item-wrapper {
    display: none;
  }
}

/* 优化：禁用状态样式 */
:deep(.el-menu-item.is-disabled) {
  opacity: 0.5;
  cursor: not-allowed;
  pointer-events: none;
}

:deep(.el-menu-item.is-disabled:hover) {
  background: none !important;
  transform: none;
}
</style>