<template>
  <div class="layout-default">
    <!-- 顶部导航栏 -->
    <header class="layout-header">
      <div class="header-left">
        <h1 class="logo">Kx UI Framework</h1>
      </div>
      <div class="header-right">
        <span class="user-info" v-if="userStore.isLogin">
          欢迎, {{ userStore.userNickname }}
        </span>
        <NuxtLink to="/home" class="header-link" v-if="!userStore.isLogin">登录</NuxtLink>
        <a href="javascript:;" class="header-link" v-else @click="handleLogout">退出</a>
      </div>
    </header>

    <!-- 主体内容区域 -->
    <div class="layout-body">
      <!-- 侧边栏 -->
      <aside class="layout-sidebar" :class="{ collapsed: appStore.sidebarCollapsed }">
        <nav class="sidebar-nav">
          <NuxtLink to="/home" class="nav-item" :class="{ active: route.path === '/home' }">
            <span class="nav-icon">🏠</span>
            <span class="nav-text">首页</span>
          </NuxtLink>
          <NuxtLink to="/auth/login" class="nav-item" :class="{ active: route.path === '/auth/login' }">
            <span class="nav-icon">🔐</span>
            <span class="nav-text">登录页</span>
          </NuxtLink>
        </nav>
      </aside>

      <!-- 主内容区域 -->
      <main class="layout-main">
        <slot />
      </main>
    </div>

    <!-- 底部 -->
    <footer class="layout-footer">
      <p>© 2026 Kx UI Framework. All rights reserved.</p>
    </footer>

    <!-- 全局加载遮罩 -->
    <div class="loading-mask" v-if="appStore.isLoading">
      <div class="loading-spinner">
        <div class="spinner"></div>
        <p>{{ appStore.loadingText || '加载中...' }}</p>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { useUserStore } from '~/stores/user'
import { useAppStore } from '~/stores/app'

const userStore = useUserStore()
const appStore = useAppStore()
const route = useRoute()
const router = useRouter()

// 退出登录
const handleLogout = () => {
  userStore.clearUserInfo()
  router.push('/auth/login')
}
</script>

<style scoped>
.layout-default {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.layout-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 60px;
  padding: 0 20px;
  background-color: var(--bg-color);
  border-bottom: 1px solid var(--border-color);
  box-shadow: var(--box-shadow-light);
}

.header-left .logo {
  font-size: 18px;
  font-weight: 600;
  color: var(--primary-color);
}

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

.user-info {
  color: var(--text-color-secondary);
}

.header-link {
  color: var(--primary-color);
  text-decoration: none;
  padding: 6px 12px;
  border-radius: var(--border-radius);
  transition: background-color 0.3s;
}

.header-link:hover {
  background-color: var(--bg-color-page);
}

.layout-body {
  display: flex;
  flex: 1;
}

.layout-sidebar {
  width: 200px;
  background-color: var(--bg-color);
  border-right: 1px solid var(--border-color);
  transition: width 0.3s;
}

.layout-sidebar.collapsed {
  width: 60px;
}

.sidebar-nav {
  padding: 20px 0;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 20px;
  color: var(--text-color);
  text-decoration: none;
  transition: background-color 0.3s;
}

.nav-item:hover {
  background-color: var(--bg-color-page);
}

.nav-item.active {
  background-color: var(--primary-color);
  color: #fff;
}

.nav-icon {
  font-size: 16px;
}

.nav-text {
  font-size: 14px;
}

.layout-main {
  flex: 1;
  padding: 20px;
  background-color: var(--bg-color-page);
  overflow-y: auto;
}

.layout-footer {
  padding: 20px;
  text-align: center;
  color: var(--text-color-secondary);
  border-top: 1px solid var(--border-color);
  background-color: var(--bg-color);
}

.loading-mask {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 9999;
}

.loading-spinner {
  text-align: center;
  color: #fff;
}

.spinner {
  width: 40px;
  height: 40px;
  margin: 0 auto 10px;
  border: 3px solid rgba(255, 255, 255, 0.3);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}
</style>