<script setup>
import { ref, onMounted, onBeforeUnmount } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores'
import DefaultImg from '@/assets/default.png'
const userStore = useUserStore()
const userInfo = ref({ ...userStore.user })
const router = useRouter()
const showDropdown = ref(false)
// 点击页面其他区域关闭下拉菜单
const handleClickOutside = (event) => {
  const profileElement = document.querySelector('.user-profile')
  const dropdownElement = document.querySelector('.user-dropdown')

  if (profileElement && dropdownElement) {
    if (
      !profileElement.contains(event.target) &&
      !dropdownElement.contains(event.target)
    ) {
      showDropdown.value = false
    }
  }
}

// 添加点击事件监听
onMounted(() => {
  document.addEventListener('click', handleClickOutside)
})

// 组件卸载时移除事件监听
onBeforeUnmount(() => {
  document.removeEventListener('click', handleClickOutside)
})

// 退出登录
const logout = () => {
  showDropdown.value = false
  ElMessageBox.confirm('确定要退出登录吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(() => {
      userStore.setToken('')
      userStore.setUser({})
      // 跳转到登录页
      router.push('/login')
      ElMessage.success('已退出登录')
    })
    .catch(() => {})
}
</script>
<template>
  <div class="app-container">
    <!-- 公共头部 -->
    <header class="header">
      <div class="header-content">
        <div class="logo">
          <span class="logo-icon">🏫</span>
          <span class="logo-text">校园 e 站通</span>
        </div>

        <div class="main-nav">
          <ul class="nav-list">
            <li class="nav-item">
              <router-link to="/mobile/index" class="nav-link"
                >首页</router-link
              >
            </li>
            <li class="nav-item">
              <router-link to="/mobile/record" class="nav-link"
                >我的订单</router-link
              >
            </li>
            <li class="nav-item">
              <router-link to="/mobile/userinfo" class="nav-link"
                >个人信息</router-link
              >
            </li>
            <li class="nav-item">
              <router-link to="/mobile/message" class="nav-link"
                >聊天窗</router-link
              >
            </li>
            <li class="nav-item">
              <router-link to="/mobile/question" class="nav-link"
                >我的提问</router-link
              >
            </li>
          </ul>
        </div>

        <div class="user-profile" @click="showDropdown = !showDropdown">
          <div class="user-avatar-container">
            <img
              class="user-avatar"
              :src="userInfo.isUpload === 1 ? userInfo.avatar : DefaultImg"
              alt="用户头像"
            />
          </div>
        </div>
      </div>

      <!-- 用户下拉菜单 -->
      <div v-show="showDropdown" class="user-dropdown">
        <div class="dropdown-menu">
          <el-button type="text" class="logout-btn" @click.stop="logout">
            <i class="el-icon-switch-button" style="margin-right: 8px"></i
            >退出登录
          </el-button>
        </div>
      </div>
    </header>

    <!-- 主体内容插槽 -->
    <main class="main-content">
      <slot></slot>
    </main>

    <!-- 公共底部 -->
    <footer class="footer">
      <div class="footer-content">
        <div class="footer-info">
          <div class="footer-logo">
            <span class="logo-icon">🏫</span>
            <span class="logo-text">华交场馆管控一体化平台</span>
          </div>
        </div>
        <div class="footer-contact">
          <p>© 2025 华东交通大学 All Rights Reserved.</p>
        </div>
      </div>
    </footer>
  </div>
</template>

<style scoped>
/* 全局样式 */
:root {
  --primary-color: #1e40af;
  --secondary-color: #3b82f6;
  --text-color: #334155;
  --bg-gradient: linear-gradient(135deg, #1e40af 0%, #3b82f6 100%);
  --card-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
  --card-hover-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
}

/* 应用容器 */
.app-container {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  background: linear-gradient(135deg, #f8fafc 0%, #6c83a1 100%);
}

/* 头部样式 */
.header {
  background: var(--bg-gradient);
  color: white;
  padding: 16px 0;
  position: relative;
  z-index: 1000;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  max-width: 1440px;
  margin: 0 auto;
  padding: 0 32px;
}

.logo {
  display: flex;
  align-items: center;
  font-size: 20px;
  font-weight: 600;
}

.logo-icon {
  margin-right: 8px;
  font-size: 24px;
}
.logo-text {
  color: black;
}
/* 新增的顶部导航 */
.main-nav {
  flex: 1;
  max-width: 500px;
  margin: 0 20px;
}

.nav-list {
  display: flex;
  list-style: none;
  margin: 0;
  padding: 0;
}

.nav-item {
  margin: 0 15px;
}

.nav-link {
  text-decoration: none;
  font-weight: 500;
  padding: 8px 0;
  position: relative;
  transition: all 0.3s;
  color: black;
}

.nav-link:hover {
  opacity: 0.8;
}

.nav-link::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  width: 0;
  height: 2px;
  background-color: white;
  transition: width 0.3s;
}

.nav-link:hover::after {
  width: 100%;
}

/* 用户头像区域 */
.user-avatar-container {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  overflow: hidden;
  cursor: pointer;
  transition: transform 0.3s;
  border: 2px solid white;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.user-avatar-container:hover {
  transform: rotate(10deg);
}

.user-avatar {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

/* 用户下拉菜单 */
.user-dropdown {
  position: absolute;
  top: 100%;
  right: 32px;
  width: 180px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
  z-index: 1000;
  animation: fadeIn 0.2s ease-in-out;
  margin-top: 8px;
}

.logout-btn {
  width: 100%;
  text-align: left;
  padding: 12px 16px;
  font-weight: 500;
}

.logout-btn:hover {
  background-color: #fef2f2;
  color: #ef4444;
}

/* 主要内容区域 */
.main-content {
  flex: 1;
  max-width: 1550px;
  width: 90%;
  margin: 0 auto;
  padding: 32px;
}

/* 页脚样式 */
.footer {
  background: var(--bg-gradient);
  color: white;
  padding: 32px 0 24px;
}

.footer-content {
  max-width: 1440px;
  margin: 0 auto;
  padding: 0 32px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.footer-logo {
  display: flex;
  align-items: center;
  font-size: 20px;
  font-weight: 600;
  margin-bottom: 0;
}

.footer-desc {
  font-size: 14px;
  line-height: 1.6;
  color: rgba(255, 255, 255, 0.8);
  margin-top: 8px;
  max-width: 300px;
}

.footer-contact {
  text-align: right;
}

.footer-contact p {
  margin: 4px 0;
  font-size: 14px;
  color: rgba(255, 255, 255, 0.8);
}

.footer-copyright {
  text-align: center;
  padding-top: 16px;
  border-top: 1px solid rgba(255, 255, 255, 0.1);
  font-size: 14px;
  color: rgba(255, 255, 255, 0.6);
  margin-top: 16px;
}

/* 动画效果 */
@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(-10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 响应式布局 */
@media (max-width: 1024px) {
  .header-content {
    padding: 0 24px;
  }

  .main-nav {
    max-width: 300px;
  }

  .footer-content {
    flex-direction: column;
    text-align: center;
  }

  .footer-contact {
    text-align: center;
    margin-top: 16px;
  }
}

@media (max-width: 768px) {
  .header-content {
    flex-wrap: wrap;
  }

  .main-nav {
    order: 3;
    margin: 16px 0;
    width: 100%;
    justify-content: center;
  }

  .nav-list {
    justify-content: center;
  }

  .nav-item {
    margin: 0 10px;
  }
}
</style>
