<template>
  <div class="dashboard">
    <el-container>
      <!-- 头部 -->
      <el-header class="header">
        <div class="header-left">
          <h1>子系统示例</h1>
        </div>
        <div class="header-right">
          <el-dropdown @command="handleCommand">
            <span class="user-info">
              <el-avatar :src="authStore.user?.avatar_url" :size="32">
                {{ authStore.user?.username?.charAt(0).toUpperCase() }}
              </el-avatar>
              <span class="username">{{ authStore.user?.username }}</span>
              <el-icon><ArrowDown /></el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="profile">
                  <el-icon><User /></el-icon>
                  个人资料
                </el-dropdown-item>
                <el-dropdown-item divided command="logout">
                  <el-icon><SwitchButton /></el-icon>
                  退出登录
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>

      <el-container>
        <!-- 侧边栏 -->
        <el-aside width="200px" class="sidebar">
          <el-menu
            :default-active="activeMenu"
            class="sidebar-menu"
            router
          >
            <el-menu-item index="/dashboard">
              <el-icon><House /></el-icon>
              <span>仪表盘</span>
            </el-menu-item>
            <el-menu-item index="/products">
              <el-icon><Grid /></el-icon>
              <span>产品管理</span>
            </el-menu-item>
            <el-menu-item index="/orders">
              <el-icon><Document /></el-icon>
              <span>订单管理</span>
            </el-menu-item>
          </el-menu>
        </el-aside>

        <!-- 主内容区 -->
        <el-main class="main-content">
          <div class="welcome-section">
            <h2>欢迎使用子系统示例</h2>
            <p>这是一个集成了SSO认证的子系统示例</p>
            <p>当前用户：{{ authStore.user?.username }} ({{ authStore.user?.email }})</p>
          </div>

          <!-- 功能卡片 -->
          <div class="feature-grid">
            <el-card class="feature-card" @click="$router.push('/products')">
              <div class="feature-content">
                <el-icon size="48" color="#409EFF"><Grid /></el-icon>
                <h3>产品管理</h3>
                <p>管理系统中的产品信息</p>
              </div>
            </el-card>

            <el-card class="feature-card" @click="$router.push('/orders')">
              <div class="feature-content">
                <el-icon size="48" color="#67C23A"><Document /></el-icon>
                <h3>订单管理</h3>
                <p>查看和管理用户订单</p>
              </div>
            </el-card>

            <el-card class="feature-card">
              <div class="feature-content">
                <el-icon size="48" color="#E6A23C"><User /></el-icon>
                <h3>用户中心</h3>
                <p>管理个人信息和设置</p>
              </div>
            </el-card>
          </div>

          <!-- SSO集成说明 -->
          <el-card class="info-card">
            <template #header>
              <span>SSO集成说明</span>
            </template>
            <div class="info-content">
              <p>✅ 已成功集成SSO单点登录</p>
              <p>✅ 用户信息自动同步</p>
              <p>✅ 统一认证和授权</p>
              <p>✅ 安全的令牌验证</p>
              
              <el-divider />
              
              <h4>技术特性：</h4>
              <ul>
                <li>OAuth 2.0 授权码模式</li>
                <li>JWT令牌验证</li>
                <li>自动令牌刷新</li>
                <li>统一登出</li>
              </ul>
            </div>
          </el-card>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useAuthStore } from '@/stores/auth'

const router = useRouter()
const route = useRoute()
const authStore = useAuthStore()

const activeMenu = computed(() => route.path)

// 处理下拉菜单命令
const handleCommand = async (command: string) => {
  switch (command) {
    case 'profile':
      ElMessage.info('个人资料功能开发中...')
      break
    case 'logout':
      try {
        await ElMessageBox.confirm('确定要退出登录吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        
        await authStore.logout()
        ElMessage.success('已退出登录')
        router.push('/login')
      } catch (error) {
        // 用户取消
      }
      break
  }
}
</script>

<style scoped>
.dashboard {
  height: 100vh;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: #fff;
  border-bottom: 1px solid #e6e6e6;
  padding: 0 20px;
}

.header-left h1 {
  margin: 0;
  color: #333;
  font-size: 20px;
  font-weight: 600;
}

.header-right {
  display: flex;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  padding: 8px;
  border-radius: 4px;
  transition: background-color 0.3s;
}

.user-info:hover {
  background-color: #f5f5f5;
}

.username {
  font-size: 14px;
  color: #333;
}

.sidebar {
  background: #fff;
  border-right: 1px solid #e6e6e6;
}

.sidebar-menu {
  border-right: none;
}

.main-content {
  background: #f5f5f5;
  padding: 20px;
}

.welcome-section {
  margin-bottom: 24px;
  padding: 20px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.welcome-section h2 {
  margin: 0 0 8px 0;
  color: #333;
  font-size: 24px;
  font-weight: 600;
}

.welcome-section p {
  margin: 4px 0;
  color: #666;
  font-size: 14px;
}

.feature-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}

.feature-card {
  cursor: pointer;
  transition: transform 0.2s, box-shadow 0.2s;
}

.feature-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.feature-content {
  text-align: center;
  padding: 20px;
}

.feature-content h3 {
  margin: 16px 0 8px 0;
  color: #333;
  font-size: 18px;
  font-weight: 600;
}

.feature-content p {
  margin: 0;
  color: #666;
  font-size: 14px;
}

.info-card {
  margin-bottom: 24px;
}

.info-content p {
  margin: 8px 0;
  color: #333;
}

.info-content h4 {
  margin: 16px 0 8px 0;
  color: #333;
}

.info-content ul {
  margin: 8px 0;
  padding-left: 20px;
}

.info-content li {
  margin: 4px 0;
  color: #666;
}
</style>