<template>
  <div class="profile-page">
    <!-- 左侧导航栏 -->
    <aside class="sidebar">
      <!-- Logo -->
      <div class="logo">logo</div>
      
      <!-- 导航菜单 -->
      <nav class="nav-menu">
        <div class="nav-item active">
          <el-icon><User /></el-icon>
          <span>个人主页</span>
        </div>
        <div class="nav-item">
          <el-icon><Compass /></el-icon>
          <span @click="goToSubscription">会员订阅</span>
        </div>
        <div class="nav-item">
          <el-icon><Document /></el-icon>
          <span @click="goToMyWorks">我的作品</span>
        </div>
      </nav>
      
      <!-- 工具分隔线 -->
      <div class="divider">
        <span>工具</span>
      </div>
      
      <!-- 工具菜单 -->
      <nav class="tools-menu">
        <div class="nav-item">
          <el-icon><VideoPlay /></el-icon>
          <span @click="goToTextToVideo">文生视频</span>
        </div>
        <div class="nav-item">
          <el-icon><Picture /></el-icon>
          <span @click="goToImageToVideo">图生视频</span>
        </div>
        <div class="nav-item">
          <el-icon><Film /></el-icon>
          <span @click="goToStoryboardVideo">分镜视频</span>
          <el-tag size="small" type="primary" class="sora-tag">Sora2.0</el-tag>
        </div>
      </nav>
    </aside>
    
    <!-- 主内容区域 -->
    <main class="main-content">
      <!-- 顶部栏 -->
      <header class="top-header">
        <div class="header-right">
          <div class="points">
            <el-icon><Star /></el-icon>
            <span>25 | 首购优惠</span>
          </div>
          <div class="notifications">
            <el-icon><Bell /></el-icon>
            <div class="notification-dot"></div>
          </div>
          <div class="user-status" @click="showUserMenu = !showUserMenu" ref="userStatusRef">
            <div class="status-icon"></div>
          </div>
        </div>
      </header>
      
      <!-- 用户资料区域 -->
      <section class="profile-section">
        <div class="profile-info">
          <div class="avatar">
            <div class="avatar-icon"></div>
          </div>
          <div class="user-details">
            <h2 class="username">mingzi_FBx7foZYDS7inLQb</h2>
            <p class="profile-status">还没有设置个人简介,点击填写</p>
            <p class="user-id">ID 2994509784706419</p>
          </div>
        </div>
      </section>
      
      <!-- 已发布内容 -->
      <section class="published-section">
        <h3 class="section-title">已发布</h3>
        <div class="video-grid">
          <div class="video-item" v-for="(video, index) in videos" :key="index">
            <div class="video-thumbnail">
              <div class="thumbnail-image">
                <div class="figure"></div>
                <div class="text-overlay">What Does it Mean To You</div>
              </div>
              <div class="video-action">
                <el-button v-if="index === 0" type="primary" size="small">做同款</el-button>
                <span v-else class="director-text">DIRECTED BY VANNOCENT</span>
              </div>
            </div>
          </div>
        </div>
      </section>
    </main>
  </div>
  
  <!-- 用户菜单下拉 - 使用Teleport渲染到body -->
  <Teleport to="body">
    <div v-if="showUserMenu" class="user-menu-teleport" :style="menuStyle">
      <!-- 管理员功能 -->
      <template v-if="userStore.isAdmin">
        <div class="menu-item" @click="goToDashboard">
          <el-icon><User /></el-icon>
          <span>数据仪表盘</span>
        </div>
        <div class="menu-item" @click="goToOrders">
          <el-icon><Document /></el-icon>
          <span>订单管理</span>
        </div>
        <div class="menu-item" @click="goToMembers">
          <el-icon><User /></el-icon>
          <span>会员管理</span>
        </div>
        <div class="menu-item" @click="goToSettings">
          <el-icon><Setting /></el-icon>
          <span>系统设置</span>
        </div>
      </template>
      <!-- 退出登录 -->
      <div class="menu-item" @click="logout">
        <el-icon><User /></el-icon>
        <span>退出登录</span>
      </div>
    </div>
  </Teleport>
</template>

<script setup>
import { ref, onMounted, onUnmounted, computed, nextTick } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { ElMessage } from 'element-plus'
import { 
  User, 
  Document, 
  Star, 
  Bell,
  Setting,
  Compass,
  VideoPlay,
  Picture,
  Film
} from '@element-plus/icons-vue'

const router = useRouter()
const userStore = useUserStore()

// 控制用户菜单显示
const showUserMenu = ref(false)
const userStatusRef = ref(null)

// 视频数据
const videos = ref(Array(6).fill({}))

// 计算菜单位置
const menuStyle = computed(() => {
  if (!userStatusRef.value || !showUserMenu.value) return {}
  
  const rect = userStatusRef.value.getBoundingClientRect()
  return {
    position: 'fixed',
    top: `${rect.bottom + 8}px`,
    right: `${window.innerWidth - rect.right}px`,
    zIndex: 99999
  }
})

// 跳转到会员订阅页面
const goToSubscription = () => {
  router.push('/subscription')
}

const goToMyWorks = () => {
  router.push('/works')
}

const goToTextToVideo = () => {
  router.push('/text-to-video/create')
}

const goToImageToVideo = () => {
  router.push('/image-to-video/create')
}

const goToStoryboardVideo = () => {
  router.push('/storyboard-video/create')
}

// 跳转到数据仪表盘
const goToDashboard = () => {
  showUserMenu.value = false
  // 检查用户权限，只有管理员才能访问数据仪表盘
  if (userStore.isAdmin) {
    router.push('/admin/dashboard')
  } else {
    ElMessage.warning('权限不足，只有管理员才能访问数据仪表盘')
  }
}

// 跳转到订单管理
const goToOrders = () => {
  showUserMenu.value = false
  router.push('/orders')
}

// 跳转到会员管理
const goToMembers = () => {
  showUserMenu.value = false
  // 检查用户权限，只有管理员才能访问会员管理
  if (userStore.isAdmin) {
    router.push('/member-management')
  } else {
    ElMessage.warning('权限不足，只有管理员才能访问会员管理')
  }
}

// 跳转到系统设置
const goToSettings = () => {
  showUserMenu.value = false
  // 检查用户权限，只有管理员才能访问系统设置
  if (userStore.isAdmin) {
    router.push('/system-settings')
  } else {
    ElMessage.warning('权限不足，只有管理员才能访问系统设置')
  }
}

// 退出登录
const logout = async () => {
  try {
    showUserMenu.value = false
    
    // 清除用户数据
    await userStore.logoutUser()
    
    // 清除其他可能的本地存储
    localStorage.removeItem('user')
    localStorage.removeItem('token')
    
    ElMessage.success('已退出登录')
    
    // 跳转到登录页
    router.push('/login')
  } catch (error) {
    console.error('退出登录失败:', error)
    ElMessage.error('退出登录失败')
  }
}

// 点击外部关闭菜单
const handleClickOutside = (event) => {
  const userStatus = event.target.closest('.user-status')
  if (!userStatus) {
    showUserMenu.value = false
  }
}

onMounted(() => {
  document.addEventListener('click', handleClickOutside)
})

onUnmounted(() => {
  document.removeEventListener('click', handleClickOutside)
})
</script>

<style scoped>
.profile-page {
  min-height: 100vh;
  background: #0a0a0a;
  color: white;
  display: flex;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  margin: 0;
  padding: 0;
  width: 100vw;
  height: 100vh;
  overflow: visible;
  position: relative;
}

/* 页面特殊效果 */
.profile-page::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: 
    radial-gradient(circle at 10% 20%, rgba(64, 158, 255, 0.1) 0%, transparent 50%),
    radial-gradient(circle at 90% 80%, rgba(103, 194, 58, 0.1) 0%, transparent 50%),
    radial-gradient(circle at 50% 50%, rgba(230, 162, 60, 0.05) 0%, transparent 50%);
  animation: profileGlow 6s ease-in-out infinite alternate;
  pointer-events: none;
  z-index: 1;
}

@keyframes profileGlow {
  0% { opacity: 0.3; }
  100% { opacity: 0.6; }
}

/* 内容层级 */
.profile-page > * {
  position: relative;
  z-index: 1;
}

/* 左侧导航栏 */
.sidebar {
  width: 280px !important;
  background: #1a1a1a !important;
  padding: 24px 0 !important;
  border-right: 1px solid #1a1a1a !important;
  flex-shrink: 0 !important;
  z-index: 100 !important;
  display: block !important;
  position: relative !important;
}

.logo {
  padding: 0 24px 32px;
  font-size: 20px;
  font-weight: 500;
  color: white;
}

.nav-menu, .tools-menu {
  padding: 0 24px;
}

.nav-item {
  display: flex;
  align-items: center;
  padding: 14px 18px;
  margin-bottom: 4px;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
  position: relative;
}

.nav-item:hover {
  background: #2a2a2a;
}

.nav-item.active {
  background: #1e3a8a;
}

.nav-item .el-icon {
  margin-right: 14px;
  font-size: 20px;
}

.nav-item span {
  font-size: 15px;
  flex: 1;
}

.sora-tag {
  margin-left: 8px;
  font-size: 10px;
  padding: 2px 6px;
}

.divider {
  margin: 30px 20px 20px;
  padding: 0 16px;
  color: #666;
  font-size: 12px;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 1px;
}

/* 主内容区域 */
.main-content {
  flex: 1;
  padding: 0;
}

/* 顶部栏 */
.top-header {
  padding: 20px 30px;
  border-bottom: 1px solid #333;
  display: flex;
  justify-content: flex-end;
  position: relative;
  z-index: 99999;
}

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

.points {
  display: flex;
  align-items: center;
  gap: 8px;
  color: #409EFF;
  font-size: 14px;
  font-weight: 500;
}

.notifications {
  position: relative;
  cursor: pointer;
}

.notification-dot {
  position: absolute;
  top: -2px;
  right: -2px;
  width: 8px;
  height: 8px;
  background: #ff4757;
  border-radius: 50%;
}

.user-status {
  width: 32px;
  height: 32px;
  border: 2px solid #409EFF;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  position: relative;
  z-index: 100000;
  transition: all 0.3s ease;
}

.user-status:hover {
  border-color: #66b1ff;
  transform: scale(1.05);
}

.status-icon {
  width: 12px;
  height: 12px;
  background: white;
  border-radius: 2px;
}

/* 用户菜单样式 */
.user-menu {
  position: absolute;
  top: 100%;
  right: 0;
  margin-top: 8px;
  background: #1a1a1a;
  border: 1px solid #333;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
  min-width: 160px;
  z-index: 99999;
  overflow: hidden;
}

/* Teleport菜单样式 */
.user-menu-teleport {
  background: #1a1a1a;
  border: 1px solid #333;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
  min-width: 160px;
  overflow: hidden;
}

.menu-item {
  display: flex;
  align-items: center;
  padding: 12px 16px;
  cursor: pointer;
  transition: background-color 0.2s ease;
  color: white;
  font-size: 14px;
}

.menu-item:hover {
  background: #2a2a2a;
}

.menu-item .el-icon {
  margin-right: 8px;
  font-size: 16px;
}

.menu-item:not(:last-child) {
  border-bottom: 1px solid #333;
}

/* 用户资料区域 */
.profile-section {
  padding: 30px;
  background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
  margin: 20px 30px;
  border-radius: 12px;
  border: 1px solid #333;
  position: relative;
  z-index: 1;
}

.profile-section::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: radial-gradient(circle at 50% 50%, rgba(64, 158, 255, 0.1) 0%, transparent 70%);
  border-radius: 12px;
  pointer-events: none;
}

.profile-info {
  display: flex;
  align-items: center;
  gap: 20px;
  position: relative;
  z-index: 1;
}

.avatar {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  background: #409EFF;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}

.avatar-icon {
  width: 40px;
  height: 40px;
  background: #1a1a2e;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.avatar-icon::before {
  content: '';
  width: 16px;
  height: 16px;
  background: white;
  border-radius: 2px;
}

.user-details {
  flex: 1;
}

.username {
  font-size: 24px;
  font-weight: 600;
  margin: 0 0 8px 0;
  color: white;
}

.profile-status {
  font-size: 14px;
  color: #999;
  margin: 0 0 4px 0;
}

.user-id {
  font-size: 12px;
  color: #666;
  margin: 0;
}

.edit-btn {
  background: #2a2a2a;
  border: 1px solid #444;
  color: white;
  padding: 10px 20px;
  border-radius: 6px;
  font-size: 14px;
}

.edit-btn:hover {
  background: #3a3a3a;
  border-color: #555;
}

/* 已发布内容 */
.published-section {
  padding: 0 30px 30px;
}

.section-title {
  font-size: 20px;
  font-weight: 600;
  margin: 0 0 20px 0;
  color: white;
  position: relative;
  padding-bottom: 8px;
}

.section-title::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  width: 40px;
  height: 3px;
  background: #1e3a8a;
  border-radius: 2px;
}

.video-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
}

.video-item {
  background: #1a1a1a;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid #333;
  transition: all 0.3s ease;
}

.video-item:hover {
  transform: translateY(-2px);
  border-color: #444;
}

.video-thumbnail {
  position: relative;
}

.thumbnail-image {
  height: 200px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
}

.figure {
  width: 80px;
  height: 80px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 50%;
  position: relative;
}

.figure::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 40px;
  height: 40px;
  background: rgba(255, 255, 255, 0.3);
  border-radius: 50%;
}

.text-overlay {
  position: absolute;
  top: 20px;
  left: 20px;
  right: 20px;
  color: white;
  font-size: 14px;
  font-weight: 500;
  text-align: center;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.5);
}

.video-action {
  padding: 15px;
  text-align: center;
  opacity: 0;
  transition: opacity 0.2s ease;
}

.video-item:hover .video-action {
  opacity: 1;
}

.director-text {
  font-size: 12px;
  color: #999;
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .video-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (max-width: 768px) {
  .profile-page {
    flex-direction: column;
  }
  
  .sidebar {
    width: 100%;
    height: auto;
  }
  
  .nav-menu, .tools-menu {
    display: flex;
    overflow-x: auto;
    padding: 0 20px;
  }
  
  .nav-item {
    white-space: nowrap;
    margin-right: 10px;
  }
  
  .video-grid {
    grid-template-columns: 1fr;
  }
  
  .profile-info {
    flex-direction: column;
    text-align: center;
  }
}
</style>
