<template>
  <el-dropdown trigger="click" @command="handleCommand">
    <div class="user-info-trigger">
      <UserAvatar :avatar="userInfo?.avatar" :nickname="userInfo?.nickname || '用户'" size="medium" />
      <div class="user-text">
        <div class="user-nickname">{{ userInfo?.nickname || '用户' }}</div>
        <div class="user-level">{{ userInfo?.memberLevel || 'Lv.1' }}</div>
      </div>
      <el-icon class="dropdown-icon">
        <ArrowDown />
      </el-icon>
    </div>
    
    <template #dropdown>
      <el-dropdown-menu>
        <el-dropdown-item command="profile">
          <el-icon><User /></el-icon>
          个人中心
        </el-dropdown-item>
        <el-dropdown-item command="settings">
          <el-icon><Setting /></el-icon>
          账号设置
        </el-dropdown-item>
        <el-dropdown-item command="dashboard">
          <el-icon><DataBoard /></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>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessageBox } from 'element-plus'
import { 
  ArrowDown, 
  User, 
  Setting, 
  DataBoard, 
  SwitchButton 
} from '@element-plus/icons-vue'
import { useAuthStore } from '@/stores/auth'
import UserAvatar from './UserAvatar.vue'

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

// 用户信息
const userInfo = computed(() => authStore.userInfo)

// 处理下拉菜单命令
const handleCommand = async (command: string) => {
  switch (command) {
    case 'profile':
      router.push('/profile')
      break
    case 'settings':
      router.push('/settings')
      break
    case 'dashboard':
      router.push('/personal-dashboard')
      break
    case 'logout':
      await handleLogout()
      break
  }
}

// 处理退出登录
const handleLogout = async () => {
  try {
    await ElMessageBox.confirm(
      '确定要退出登录吗？',
      '提示',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }
    )
    
    await authStore.logout()
    router.push('/')
  } catch (error) {
    // 用户取消操作
  }
}
</script>

<style scoped>
.user-info-trigger {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 12px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  cursor: pointer;
  transition: all 0.3s ease;
  color: white;
}

.user-info-trigger:hover {
  background: rgba(255, 255, 255, 0.15);
  border-color: rgba(255, 255, 255, 0.3);
  transform: translateY(-1px);
}

.user-text {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  min-width: 0;
}

.user-nickname {
  font-size: 14px;
  font-weight: 600;
  color: white;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100px;
}

.user-level {
  font-size: 12px;
  color: rgba(255, 255, 255, 0.7);
  line-height: 1;
}

.dropdown-icon {
  font-size: 14px;
  color: rgba(255, 255, 255, 0.7);
  transition: transform 0.3s ease;
}

.user-info-trigger:hover .dropdown-icon {
  transform: translateY(1px);
}

:deep(.el-dropdown-menu) {
  border-radius: 12px;
  border: 1px solid rgba(0, 0, 0, 0.1);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
  backdrop-filter: blur(10px);
  background: rgba(255, 255, 255, 0.95);
}

:deep(.el-dropdown-menu__item) {
  padding: 12px 16px;
  font-size: 14px;
  display: flex;
  align-items: center;
  gap: 8px;
  transition: all 0.2s ease;
}

:deep(.el-dropdown-menu__item:hover) {
  background: rgba(102, 126, 234, 0.1);
  color: #667eea;
}

:deep(.el-dropdown-menu__item.is-divided) {
  border-top: 1px solid rgba(0, 0, 0, 0.1);
  margin-top: 4px;
}

/* 响应式设计 */
@media (max-width: 640px) {
  .user-text {
    display: none;
  }
  
  .user-info-trigger {
    padding: 6px;
    gap: 0;
  }
}
</style>
