<script setup>
import { onMounted, computed } from 'vue'
import { useRoute } from 'vue-router'
import { userStore } from './store/user'
import HelloWorld from './components/HelloWorld.vue'
import TheWelcome from './components/TheWelcome.vue'

// 获取当前路由
const route = useRoute()

// 判断是否是后台管理路由
const isAdminRoute = computed(() => {
  return route.path.startsWith('/admin')
})

// 应用启动时获取用户信息
onMounted(async () => {
  // 如果用户已登录但没有完整的用户信息，则从后端获取
  if (userStore.isLoggedIn && (!userStore.user?.id || typeof userStore.user.id === 'number')) {
    try {
      console.log('应用启动，尝试获取用户信息...')
      await userStore.fetchLoginUser()
      console.log('用户信息获取成功:', userStore.user)
    } catch (error) {
      console.error('获取用户信息失败:', error)
      // 如果获取失败，可能是token过期，清除登录状态
      if (error.response?.status === 401) {
        userStore.logout()
      }
    }
  }

  // 如果是后台路由，给body添加admin-body类
  if (isAdminRoute.value) {
    document.body.classList.add('admin-body')
  } else {
    document.body.classList.remove('admin-body')
  }
})
</script>

<template>
  <!-- 后台管理路由直接显示内容，不显示底部导航 -->
  <template v-if="isAdminRoute">
    <router-view />
  </template>
  
  <!-- 前台用户路由显示应用容器和底部导航 -->
  <div v-else class="app-container">
    <main class="main-content">
      <router-view />
    </main>
    
    <!-- 底部导航栏 -->
    <nav class="bottom-nav">
      <router-link to="/" class="nav-item" :class="{ active: $route.path === '/' }">
        <div class="nav-icon">
          <img src="@/assets/banner/home.png" alt="首页" class="nav-icon-img" />
        </div>
        <div class="nav-text">首页</div>
      </router-link>
      
      <router-link to="/lottery-info" class="nav-item" :class="{ active: $route.path === '/lottery-info' }">
        <div class="nav-icon">
          <img src="@/assets/banner/kaijiang.png" alt="开奖" class="nav-icon-img" />
    </div>
        <div class="nav-text">开奖</div>
      </router-link>

      <router-link to="/profile" class="nav-item" :class="{ active: $route.path === '/profile' }">
        <div class="nav-icon">
          <img src="@/assets/banner/wode.png" alt="我的" class="nav-icon-img" />
        </div>
        <div class="nav-text">我的</div>
      </router-link>
    </nav>
  </div>
</template>

<style>
/* 全局重置 */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html {
  background: #f0f2f5 !important;
  min-height: 100vh;
  width: 100%;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
    'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
    sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  background: #f0f2f5 !important;
  line-height: 1.5;
  min-height: 100vh;
  margin: 0 !important;
  padding: 30px !important;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
}

/* 应用容器 - 包含主内容和底部导航 */
.app-container {
  min-height: calc(100vh - 60px);
  display: flex;
  flex-direction: column;
  position: relative;
  max-width: 900px;
  width: 100%;
  margin: 0 auto;
  background: white;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.08);
  border-radius: 12px;
  overflow: hidden;
}

/* 主要内容区域 */
.main-content {
  flex: 1;
  background: #f0f2f5;
  position: relative;
}

/* 底部导航栏 */
.bottom-nav {
  height: 75px;
  background: white;
  display: flex;
  align-items: center;
  justify-content: space-around;
  border-top: 1px solid #f0f0f0;
  z-index: 1000;
  padding: 5px 0;
  box-shadow: 0 -2px 12px rgba(0, 0, 0, 0.1);
}

.nav-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  color: #9ca3af;
  transition: all 0.3s ease;
  padding: 0;
  min-width: 65px;
  position: relative;
}

.nav-item:hover {
  color: #e53e3e;
}

.nav-item:hover .nav-icon-img {
  transform: scale(1.05);
}

.nav-item.active {
  color: #e53e3e;
}

.nav-item.active .nav-icon {
  background: linear-gradient(135deg, #e53e3e, #ff6b6b);
  border-radius: 50%;
  box-shadow: 0 4px 12px rgba(229, 62, 62, 0.3);
}

.nav-item.active .nav-icon-img {
  filter: brightness(0) invert(1);
  transform: scale(1.1);
}

.nav-item.active .nav-text {
  color: #e53e3e;
  font-weight: 700;
}

.nav-icon {
  margin-bottom: 5px;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: 50%;
}

.nav-icon-img {
  width: 26px;
  height: 26px;
  object-fit: contain;
  transition: all 0.3s ease;
}

.nav-icon svg {
  width: 24px;
  height: 24px;
}

.nav-text {
  font-size: 12px;
  font-weight: 500;
  text-align: center;
  transition: all 0.3s ease;
}

/* 移动端适配 */
@media (max-width: 768px) {
  .bottom-nav {
    height: 70px;
    padding: 3px 0;
  }
  
  .nav-item {
    padding: 0;
    min-width: 60px;
  }
  
  .nav-icon-img {
    width: 28px;
    height: 28px;
  }
  
  .nav-text {
    font-size: 11px;
  }
  
  .nav-item.active::before {
    width: 30px;
    height: 3px;
  }
}

/* 通用容器样式 */
.container {
  width: 100%;
  background: #f0f2f5;
  position: relative;
}

/* 页面头部样式 */
.page-header {
  background: url('@/assets/banner/banner.png') center/cover no-repeat, linear-gradient(135deg, #e53e3e 0%, #ff6b6b 100%);
  color: white;
  padding: 60px 20px 30px;
  text-align: center;
  position: relative;
  overflow: hidden;
}

.page-header::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><circle cx="20" cy="20" r="2" fill="rgba(255,255,255,0.1)"/><circle cx="80" cy="40" r="1.5" fill="rgba(255,255,255,0.1)"/><circle cx="40" cy="70" r="1" fill="rgba(255,255,255,0.1)"/><circle cx="70" cy="80" r="2.5" fill="rgba(255,255,255,0.1)"/></svg>');
  pointer-events: none;
  }

.page-title {
  font-size: 28px;
  font-weight: 700;
  margin-bottom: 8px;
  position: relative;
  z-index: 1;
}

.page-subtitle {
  font-size: 16px;
  opacity: 0.9;
  position: relative;
  z-index: 1;
}

/* 通用按钮样式 */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: none;
  border-radius: 12px;
  padding: 14px 24px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  text-decoration: none;
  position: relative;
  overflow: hidden;
}

.btn::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255,255,255,0.2), transparent);
  transition: left 0.5s;
}

.btn:hover::before {
  left: 100%;
}

.btn-primary {
  background: linear-gradient(135deg, #e53e3e, #ff6b6b);
  color: white;
  box-shadow: 0 4px 15px rgba(229, 62, 62, 0.3);
}

.btn-primary:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(229, 62, 62, 0.4);
  }

.btn-secondary {
  background: #f8f9fa;
  color: #6c757d;
  border: 1px solid #e9ecef;
}

.btn-secondary:hover {
  background: #e9ecef;
  border-color: #d1d5db;
  transform: translateY(-1px);
}

/* 球号样式 */
.ball-red {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  background: linear-gradient(135deg, #e53e3e, #ff6b6b);
  color: white;
  border-radius: 50%;
  font-size: 14px;
  font-weight: bold;
  margin: 3px;
  box-shadow: 0 2px 8px rgba(229, 62, 62, 0.3);
  transition: transform 0.2s ease;
}

.ball-red:hover {
  transform: scale(1.1);
}

.ball-blue {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  background: linear-gradient(135deg, #3b82f6, #1d4ed8);
  color: white;
  border-radius: 50%;
  font-size: 14px;
  font-weight: bold;
  margin: 3px;
  box-shadow: 0 2px 8px rgba(59, 130, 246, 0.3);
  transition: transform 0.2s ease;
}

.ball-blue:hover {
  transform: scale(1.1);
}

/* 输入框样式 */
.form-input {
  width: 100%;
  padding: 16px;
  border: 2px solid #e9ecef;
  border-radius: 12px;
  font-size: 16px;
  transition: all 0.3s ease;
  background: #f8f9fa;
}

.form-input:focus {
  outline: none;
  border-color: #e53e3e;
  background: white;
  box-shadow: 0 0 0 3px rgba(229, 62, 62, 0.1);
}

.form-input::placeholder {
  color: #9ca3af;
}

/* 卡片样式 */
.card {
  background: white;
  border-radius: 16px;
  padding: 20px;
  margin: 16px;
  box-shadow: 0 4px 25px rgba(0, 0, 0, 0.1);
  border: 1px solid #f0f0f0;
  transition: all 0.3s ease;
}

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

/* 加载动画 */
.loading {
  display: inline-block;
  width: 20px;
  height: 20px;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-radius: 50%;
  border-top-color: white;
  animation: spin 1s ease-in-out infinite;
  }

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .app-container {
    max-width: 800px;
  }
}

@media (max-width: 768px) {
  body {
    padding: 15px !important;
  }
  
  .app-container {
    max-width: 100%;
    min-height: calc(100vh - 30px);
    border-radius: 8px;
  }
}

@media (max-width: 480px) {
  body {
    padding: 10px !important;
  }
  
  .app-container {
    border-radius: 0;
    min-height: 100vh;
  }
  
  .page-header {
    background: url('@/assets/banner/banner.png') center/cover no-repeat, linear-gradient(135deg, #e53e3e 0%, #ff6b6b 100%);
    padding: 50px 16px 25px;
  }
  
  .page-title {
    font-size: 24px;
  }
  
  .page-subtitle {
    font-size: 14px;
  }
  
  .card {
    margin: 12px;
    padding: 16px;
  }
  
  .btn {
    padding: 12px 20px;
    font-size: 14px;
  }
}

/* 平滑滚动 */
html {
  scroll-behavior: smooth;
}

/* 选择高亮颜色 */
::selection {
  background: rgba(229, 62, 62, 0.2);
  color: #e53e3e;
}
</style>
