<template>
  <div class="user-center-page">
    <div class="user-card-wrapper">
      <UserCard/>
    </div>
    <div class="content-wrapper">
      <div class="sidebar-wrapper">
        <FloatingSidebar :menus="menus" />
      </div>
      <div class="main-content">
        <router-view/>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue';
import { useRoute } from 'vue-router';
import { FloatingSidebar } from '@/components/base';
import { UserCard } from '@/views/user-center/components';
import { getParentChildrenRoutes } from '@/utils/routeUtils';
import type { SysMenu } from '@/types/menu';

const route = useRoute();

// 从当前路由配置中获取子路由，转换为菜单格式
const menus = computed(() => {
  // 使用工具函数获取父路由的子路由
  const childRoutes = getParentChildrenRoutes(route);
  
  if (childRoutes.length === 0) {
    return [];
  }
  
  // 获取父路由路径（用于拼接相对路径）
  const parentRoute = route.matched[route.matched.length - 2];
  
  // 将子路由转换为菜单格式
  return childRoutes
    .map((child: any) => ({
      menuID: child.name as string || child.path,
      name: child.meta?.title as string,
      url: child.path.startsWith('/') ? child.path : `${parentRoute.path}/${child.path}`,
      icon: child.meta?.icon as string,
      orderNum: child.meta?.orderNum as number || 0,
    } as SysMenu))
    .sort((a: any, b: any) => (a.orderNum || 0) - (b.orderNum || 0));
});
</script>

<style lang="scss" scoped>
.user-center-page {
  min-height: 100vh;
  background: #f5f5f5;
  padding: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
}

.user-card-wrapper {
  width: 100%;
  display: flex;
  justify-content: center;
}

.content-wrapper {
  width: 100%;
  max-width: 1200px;
  display: flex;
  gap: 20px;
}

.sidebar-wrapper {
  flex-shrink: 0;
}

.main-content {
  flex: 1;
  background: white;
  border-radius: 8px;
  padding: 20px;
}
</style>

