<template>
  <div class="app-layout">
    <header class="header">
      <h1>Shared Components Demo</h1>
    </header>
    
    <div class="content-wrapper">
      <!-- 左侧树形导航 -->
      <aside class="sidebar">
        <div class="nav-tree">
          <div class="nav-item" :class="{ active: $route.path === '/' }">
            <router-link to="/" class="nav-link">
              🏠 Home
            </router-link>
          </div>
          
          <div v-for="group in componentGroups" :key="group.name" class="nav-group">
            <div class="group-title">📁 {{ group.name }}</div>
            <div class="group-items">
              <div v-for="item in group.items" :key="item.path" 
                   class="nav-item" :class="{ active: $route.path === item.path }">
                <router-link :to="item.path" class="nav-link">
                  🧩 {{ item.displayName }}
                </router-link>
              </div>
            </div>
          </div>
        </div>
      </aside>
      
      <!-- 右侧内容区域 -->
      <main class="main-content">
        <router-view />
      </main>
    </div>
  </div>
</template>

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

const router = useRouter()

// 根据目录结构组织组件路由  
const componentGroups = computed(() => {
  // 直接基于文件路径创建路由结构
  const testComponents = import.meta.glob('../components/**/*Example.vue')
  const groups: Record<string, any[]> = {}
  
  Object.keys(testComponents).forEach(filePath => {
    // 解析文件路径
    // ../components/base/button/ButtonExample.vue -> ['base', 'button']
    // ../components/fileupload/FileUploadExample.vue -> ['fileupload']
    const pathWithoutPrefix = filePath.replace('../components/', '')
    const segments = pathWithoutPrefix.replace(/Test\.vue$/, '').split('/').filter(Boolean)
    
    if (segments.length === 0) return
    
    // 组件名是最后一级目录，但路径保持完整结构
    const componentName = segments[segments.length - 1]
    const routePath = `/${segments.join('/').toLowerCase()}`
    
    // 确定分组
    let groupName: string
    if (segments.length === 1) {
      // 顶级组件: fileupload -> Components
      groupName = 'Components'
    } else {
      // 嵌套组件: base/button -> Base
      groupName = segments[0].charAt(0).toUpperCase() + segments[0].slice(1)
    }
    
    // 格式化显示名称
    const displayName = componentName.charAt(0).toUpperCase() + componentName.slice(1)
    
    if (!groups[groupName]) {
      groups[groupName] = []
    }
    
    groups[groupName].push({
      path: routePath,
      displayName: displayName
    })
  })

  // 转换为数组并排序
  return Object.entries(groups)
    .map(([name, items]) => ({
      name,
      items: items.sort((a, b) => a.displayName.localeCompare(b.displayName))
    }))
    .sort((a, b) => a.name.localeCompare(b.name))
})
</script>

<style lang="scss">
.app-layout {
  height: 100vh;
  display: flex;
  flex-direction: column;
  max-width: 100%;
  margin: 0;
  padding: 0;
}

.header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 16px 24px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  z-index: 10;
}

.header h1 {
  margin: 0;
  font-size: 24px;
  font-weight: 600;
}

.content-wrapper {
  display: flex;
  flex: 1;
  overflow: hidden;
}

.sidebar {
  width: 280px;
  background: #f8fafc;
  border-right: 1px solid #e2e8f0;
  overflow-y: auto;
  flex-shrink: 0;
}

.nav-tree {
  padding: 16px;
}

.nav-item {
  margin-bottom: 4px;
}

.nav-item.active .nav-link {
  background: #667eea;
  color: white;
  font-weight: 600;
}

.nav-link {
  display: block;
  padding: 8px 12px;
  color: #374151;
  text-decoration: none;
  border-radius: 6px;
  transition: all 0.2s ease;
  font-size: 14px;
}

.nav-link:hover {
  background: #e2e8f0;
  color: #1f2937;
}

.nav-group {
  margin-bottom: 24px;
}

.group-title {
  font-size: 14px;
  font-weight: 600;
  color: #6b7280;
  padding: 8px 12px;
  margin-bottom: 8px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.group-items {
  margin-left: 12px;
}

.group-items .nav-link {
  padding-left: 20px;
}

.main-content {
  flex: 1;
  padding: 24px;
  overflow-y: auto;
  background: white;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .content-wrapper {
    flex-direction: column;
  }
  
  .sidebar {
    width: 100%;
    height: auto;
    max-height: 40vh;
    border-right: none;
    border-bottom: 1px solid #e2e8f0;
  }
  
  .main-content {
    padding: 16px;
  }
  
  .header {
    padding: 12px 16px;
  }
  
  .header h1 {
    font-size: 20px;
  }
}

/* 滚动条样式 */
.sidebar::-webkit-scrollbar,
.main-content::-webkit-scrollbar {
  width: 6px;
}

.sidebar::-webkit-scrollbar-track,
.main-content::-webkit-scrollbar-track {
  background: #f1f1f1;
}

.sidebar::-webkit-scrollbar-thumb,
.main-content::-webkit-scrollbar-thumb {
  background: #c1c1c1;
  border-radius: 3px;
}

.sidebar::-webkit-scrollbar-thumb:hover,
.main-content::-webkit-scrollbar-thumb:hover {
  background: #a8a8a8;
}
</style>
