<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)">
        <el-menu-item :index="resolvePath(onlyOneChild.path)" :class="{'submenu-title-noDropdown': !isNest}">
          <!-- 动态显示图标 -->
          <img v-if="!isNest && onlyOneChild.meta" :src="getIcon(onlyOneChild)" class="sidebar-icon" />
          <span>{{ generateTitle(onlyOneChild.meta.title) }}</span>
        </el-menu-item>
      </app-link>
    </template>

    <!-- 子菜单 -->
    <el-submenu v-else ref="subMenu" :index="resolvePath(item.path)" popper-append-to-body>
      <template slot="title">
        <!-- 动态显示图标 -->
        <img v-if="!isNest && item.meta" :src="getIcon(item)" class="sidebar-icon" />
        <span>{{ generateTitle(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-submenu>
  </div>
</template>

<script>
import path from 'path'
import { isExternal } from '@/utils/validate'
import AppLink from './Link'
import FixiOSBug from './FixiOSBug'

export default {
  name: 'SidebarItem',
  components: { AppLink },
  mixins: [FixiOSBug],
  props: {
    item: {
      type: Object,
      required: true
    },
    isNest: {
      type: Boolean,
      default: false
    },
    basePath: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      onlyOneChild: null
    }
  },
  methods: {
    hasOneShowingChild(children = [], parent) {
      const showingChildren = children.filter(item => !item.hidden)
      if (showingChildren.length === 1) {
        this.onlyOneChild = showingChildren[0]
        return true
      }
      if (showingChildren.length === 0) {
        this.onlyOneChild = { ...parent, path: '', noShowingChildren: true }
        return true
      }
      return false
    },
    resolvePath(routePath) {
      if (isExternal(routePath)) {
        return routePath
      }
      if (isExternal(this.basePath)) {
        return this.basePath
      }
      return path.resolve(this.basePath, routePath)
    },
    generateTitle(metaTitle) {
      if (this.$te(metaTitle)) {
        return this.$t(metaTitle)
      }
      return metaTitle || 'No Title'
    },
    // 动态获取图标
    getIcon(item) {
      const currentPath = this.$route.path;

      // 直接检查当前路径是否与一级菜单的路径匹配
      if (this.resolvePath(item.path) === currentPath) {
        return item.meta.selected_icon; // 如果一级菜单本身被选中，返回 selected_icon
      }

      // 递归检查是否存在子路径匹配当前路径
      const isPathInChildren = (children) => {
        return children.some(child => {
          if (this.resolvePath(child.path) === currentPath) {
            return true;
          }
          return child.children ? isPathInChildren(child.children) : false;
        });
      };

      // 如果当前路径匹配第一级菜单的子路径，返回 selected_icon
      if (isPathInChildren(item.children || [])) {
        return item.meta.selected_icon; // 返回选中状态的图标
      }

      return item.meta.icon; // 返回默认图标
    },
    // 新增方法：获取只有一个子项的图标
    getOnlyOneChildIcon() {
      if (this.onlyOneChild) {
        return this.getIcon(this.onlyOneChild);
      }
      return ''; // 或者返回一个默认图标
    }
  }
}
</script>

<style scoped>
.sidebar-icon {
  width: 20px;
  height: 20px;
  margin-right: 12px;
  vertical-align: middle;
}
</style>
