<!--
  * 双层混合菜单-侧栏包装器
  * 
  * @Copyright  1024创新实验室 （ https://1024lab.net ），Since 2012 
-->
<template>
  <div class="top-side-menu-container">
    <!-- 1、菜单区域： 使用递归菜单解决 -->
    <div class="menu-list-wrapper">
      <RecursionMenu :collapsed="collapsed" :menuTree="menuTree" ref="menuRef" />
    </div>

    <!-- 2、底部用户卡片（仅未收缩时展示，类似于原型中的 user-card） -->
    <div class="side-menu-footer" v-if="!collapsed">
      <div class="user-card">
        <div class="user-avatar">{{ avatarName }}</div>
        <div class="user-info">
          <div class="user-name">{{ actualName }}</div>
          <div class="user-role">{{ administratorFlag ? '系统管理员' : '普通用户' }}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
  import { computed, nextTick, ref, watch } from 'vue';
  import RecursionMenu from './recursion-menu.vue';
  import { useAppConfigStore } from '/@/store/modules/system/app-config';
  import { useUserStore } from '/@/store/modules/system/user';

  const props = defineProps({
    collapsed: {
      type: Boolean,
      default: false,
    },
    activeCategoryId: {
      type: [Number, String],
      default: '',
    },
  });

  const userStore = useUserStore();
  const actualName = computed(() => userStore.actualName || '用户');
  const avatarName = computed(() => actualName.value.charAt(0));
  const administratorFlag = computed(() => userStore.administratorFlag);

  // 计算当前一级分类下的子菜单树
  const menuTree = computed(() => {
    if (!props.activeCategoryId) {
      return [];
    }
    const allMenuTree = userStore.getMenuTree || [];
    const currentRoot = allMenuTree.find((item) => String(item.menuId) === String(props.activeCategoryId));
    return currentRoot ? currentRoot.children || [] : [];
  });

  const menuRef = ref();

  watch(
    () => props.collapsed,
    (newValue) => {
      if (!newValue) {
        nextTick(() => menuRef.value && menuRef.value.updateOpenKeysAndSelectKeys());
      }
    }
  );

  watch(
    () => props.activeCategoryId,
    () => {
      nextTick(() => menuRef.value && menuRef.value.updateOpenKeysAndSelectKeys());
    }
  );

  defineExpose({
    updateOpenKeysAndSelectKeys: () => {
      if (menuRef.value) {
        menuRef.value.updateOpenKeysAndSelectKeys();
      }
    },
  });
</script>

<style lang="less" scoped>
  .top-side-menu-container {
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    background: transparent;
  }

  .menu-list-wrapper {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 10px 4px;
    
    &::-webkit-scrollbar {
      width: 4px;
    }
    &::-webkit-scrollbar-thumb {
      border-radius: 10px;
      background: rgba(0, 0, 0, 0.1);
    }
  }

  .side-menu-footer {
    padding: 16px;
    border-top: 1px solid rgba(226, 232, 240, 0.5);
    background: rgba(255, 255, 255, 0.4);
    backdrop-filter: blur(10px);
    border-radius: 0 0 0 12px;
  }

  .user-card {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px;
    background: rgba(255, 255, 255, 0.6);
    border-radius: 8px;
    border: 1px solid rgba(226, 232, 240, 0.6);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04);
    transition: all 0.3s ease;

    &:hover {
      box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
      transform: translateY(-1px);
    }
  }

  .user-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: linear-gradient(135deg, #2563eb 0%, #8b5cf6 100%);
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    font-weight: 600;
    font-size: 13px;
    flex-shrink: 0;
    box-shadow: 0 2px 6px rgba(37, 99, 235, 0.2);
  }

  .user-info {
    flex: 1;
    overflow: hidden;
  }

  .user-name {
    font-size: 13px;
    font-weight: 600;
    color: #1e293b;
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
  }

  .user-role {
    font-size: 11px;
    color: #64748b;
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
  }
</style>
