<script setup lang="ts">
import { RouterView } from 'vue-router'
import { computed, ref, onMounted, onUnmounted } from 'vue'
import { useUserStore } from './stores/user'

const userStore = useUserStore()
const isAuthenticated = computed(() => userStore.isAuthenticated)
const userInfo = computed(() => userStore.userInfo)
const showUserMenu = ref(false)

const handleLogout = () => {
  userStore.logout()
  if (window.location.pathname.includes('/user')) {
    window.location.href = '/'
  }
  showUserMenu.value = false
}

const toggleUserMenu = () => {
  showUserMenu.value = !showUserMenu.value
}

// 点击其他地方关闭菜单
const closeUserMenu = (event: MouseEvent) => {
  const target = event.target as HTMLElement
  if (!target.closest('.user-section')) {
    showUserMenu.value = false
  }
}

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

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

<template>
  <div class="app-container">
    <!-- 左侧栏 -->
    <aside class="sidebar">
      <div class="sidebar-content">
        <!-- 网站标题 -->
        <div class="site-title">
          <span class="title-text">AI Trade</span>
          <div class="subtitle-text">AI for Trading</div>
        </div>
        <!-- Agent 列表 -->
        <div class="agent-list">
          <RouterLink to="/" class="agent-item">
            <span class="agent-icon">🏠</span>
            <span class="agent-name">首页</span>
          </RouterLink>
          <RouterLink to="/ai-agent" class="agent-item">
            <span class="agent-icon">🤖</span>
            <span class="agent-name">AI 助手</span>
          </RouterLink>
        </div>
      </div>
    </aside>

    <!-- 右侧对话区域 -->
    <main class="chat-container">
      <div class="chat-content">
        <RouterView />
      </div>

      <!-- 用户信息区域 -->
      <div class="user-section" v-if="isAuthenticated">
        <div class="user-info-box" @click="toggleUserMenu">
          <div class="user-avatar">
            <span>{{ userInfo?.nickname?.charAt(0) || 'U' }}</span>
          </div>
          <span class="user-nickname">{{ userInfo?.nickname }}</span>
        </div>

        <!-- 用户菜单 -->
        <div v-if="showUserMenu" class="user-menu">
          <button class="menu-item" @click="handleLogout">退出登录</button>
        </div>
      </div>
      <div v-else class="user-section">
        <div class="auth-buttons">
          <RouterLink to="/login" class="auth-link">登录</RouterLink>
          <RouterLink to="/register" class="auth-link register-link">注册</RouterLink>
        </div>
      </div>
    </main>
  </div>
</template>

<style>
:root {
  --color-bg-primary: #ffffff;
  --color-bg-secondary: #f8f9fa;
  --color-bg-elevated: #ffffff;
  --color-bg-card: #f8f9fa;

  --color-text-primary: #2c3e50;
  --color-text-secondary: #6c757d;
  --color-text-tertiary: #8c98a4;
  --color-text-disabled: #adb5bd;

  --color-divider: rgba(0, 0, 0, 0.1);
  --color-border: rgba(0, 0, 0, 0.1);
  --color-border-hover: rgba(0, 0, 0, 0.2);

  --color-accent: #3355ff;
  --color-accent-hover: #2244ee;
  --color-accent-light: rgba(51, 85, 255, 0.1);

  --font-weight-light: 300;
  --font-weight-regular: 400;
  --font-weight-medium: 500;
  --font-weight-bold: 600;

  --border-radius: 0.5rem;
  --sidebar-width: 260px;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html,
body,
#app {
  width: 100%;
  height: 100vh;
  margin: 0;
  padding: 0;
  overflow: hidden;
}

body {
  font-family: 'Inter', 'Helvetica Neue', Arial, sans-serif;
  background-color: var(--color-bg-primary);
  color: var(--color-text-primary);
  line-height: 1.6;
  font-weight: var(--font-weight-regular);
  height: 100vh;
}

.app-container {
  width: 100%;
  height: 100vh;
  display: flex;
  overflow: hidden;
}

/* 左侧栏样式 */
.sidebar {
  width: var(--sidebar-width);
  height: 100vh;
  background-color: var(--color-bg-elevated);
  border-right: 1px solid var(--color-border);
  display: flex;
  flex-direction: column;
}

.sidebar-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  height: 100%;
}

/* 网站标题样式 */
.site-title {
  padding: 2rem 1.5rem;
  text-align: center;
  border-bottom: 1px solid var(--color-divider);
}

.title-text {
  font-size: 1.8rem;
  font-weight: 800;
  background: linear-gradient(135deg, var(--color-accent) 0%, #2244ee 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  text-fill-color: transparent;
  font-family:
    'SF Pro Display',
    -apple-system,
    BlinkMacSystemFont,
    'Segoe UI',
    Roboto,
    sans-serif;
  letter-spacing: -0.5px;
  margin-bottom: 0.3rem;
  display: block;
}

.subtitle-text {
  font-size: 0.9rem;
  color: var(--color-text-secondary);
  font-weight: 400;
  font-family:
    'SF Pro Display',
    -apple-system,
    BlinkMacSystemFont,
    'Segoe UI',
    Roboto,
    sans-serif;
  letter-spacing: 0.5px;
  text-transform: uppercase;
}

/* Agent 列表样式 */
.agent-list {
  flex: 1;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  overflow-y: auto;
}

.agent-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.75rem 1rem;
  color: var(--color-text-primary);
  text-decoration: none;
  border-radius: var(--border-radius);
  transition: background-color 0.2s ease;
}

.agent-item:hover {
  background-color: var(--color-bg-secondary);
}

.agent-item.router-link-active {
  background-color: var(--color-accent-light);
  color: var(--color-accent);
}

.agent-icon {
  font-size: 1.25rem;
}

.agent-name {
  font-size: 0.95rem;
  font-weight: var(--font-weight-medium);
}

/* 修改用户信息区域样式 */
.user-section {
  position: fixed;
  left: 0;
  bottom: 0;
  width: var(--sidebar-width);
  background-color: var(--color-bg-elevated);
  border-radius: 0;
  padding: 1rem;
  z-index: 100;
  cursor: pointer;
  transition: all 0.2s ease;
  border-top: 1px solid var(--color-divider);
  border: 1px solid var(--color-border);
}

.user-section:hover {
  background-color: var(--color-bg-secondary);
}

.user-info-box {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.user-avatar {
  width: 2.5rem;
  height: 2.5rem;
  border-radius: 50%;
  background: var(--color-accent-light);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
  color: var(--color-accent);
  font-weight: bold;
}

.user-nickname {
  font-size: 0.95rem;
  font-weight: var(--font-weight-medium);
  color: var(--color-text-primary);
}

/* 用户菜单样式 */
.user-menu {
  position: absolute;
  bottom: 100%;
  right: 0;
  margin-bottom: 0.5rem;
  background-color: var(--color-bg-elevated);
  border: 1px solid var(--color-border);
  border-radius: var(--border-radius);
  min-width: 120px;
  overflow: hidden;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}

.menu-item {
  width: 100%;
  padding: 0.75rem 1rem;
  border: none;
  background: none;
  color: var(--color-text-primary);
  font-size: 0.9rem;
  text-align: left;
  cursor: pointer;
  transition: background-color 0.2s ease;
}

.menu-item:hover {
  background-color: var(--color-bg-secondary);
}

/* 移动端适配 */
@media (max-width: 768px) {
  .user-section {
    width: 100%;
    left: 0;
    bottom: 0;
    border-radius: 0;
  }

  .site-title {
    padding: 1rem;
  }

  .title-text {
    font-size: 1.5rem;
    margin-bottom: 0.2rem;
  }

  .subtitle-text {
    font-size: 0.8rem;
  }
}

/* 右侧对话区域样式 */
.chat-container {
  flex: 1;
  height: 100vh;
  display: flex;
  background-color: var(--color-bg-primary);
  overflow: hidden;
}

.chat-content {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  position: relative;
}

/* 全局按钮样式 */
.btn {
  padding: 0.8rem 1.6rem;
  border-radius: var(--border-radius);
  font-weight: var(--font-weight-medium);
  font-size: 1rem;
  cursor: pointer;
  transition: all 0.2s ease;
  border: none;
}

.btn-primary {
  background-color: var(--color-bg-elevated);
  color: var(--color-text-primary);
  border: 1px solid var(--color-border);
}

.btn-primary:hover {
  background-color: var(--color-bg-secondary);
  border-color: var(--color-border-hover);
}

.btn-secondary {
  background-color: transparent;
  color: var(--color-text-secondary);
  border: 1px solid var(--color-border);
}

.btn-secondary:hover {
  background-color: var(--color-bg-elevated);
  color: var(--color-text-primary);
}

/* 全局卡片样式 */
.card {
  background-color: var(--color-bg-card);
  border-radius: var(--border-radius);
  padding: 1.5rem;
  border: 1px solid var(--color-border);
  transition: all 0.3s ease;
}

.card:hover {
  border-color: var(--color-border-hover);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
}

/* 响应式设计 */
@media (max-width: 768px) {
  :root {
    --sidebar-width: 100%;
  }

  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    z-index: 100;
    transform: translateX(-100%);
    transition: transform 0.3s ease;
  }

  .sidebar-open {
    transform: translateX(0);
  }

  .chat-container {
    margin-left: 0;
  }
}
</style>
