<template>
  <nav :class="[themeClass, 'navbar']">
    <div class="nav-left">
      <button class="logo" @click="store.commit('toggleTheme')">
        CYBER
      </button>
    </div>

    <div class="nav-center">
      <ul class="nav-items" v-if="!isMobile">
        <li v-for="(item, index) in navItems" :key="index">
          <span v-if="item === 'divider'" class="divider">|</span>
          <router-link v-else :to="item.link">{{ item.name }}</router-link>
        </li>
      </ul>
      <div class="mobile-menu" v-else>
        <button @click="toggleMobileMenu" class="hamburger-btn">
          <span class="hamburger-icon"></span>
        </button>
        <transition name="fade">
          <ul v-if="showMobileMenu" class="mobile-nav-items">
            <li v-for="(item, index) in navItems.filter(item => item !== 'divider')" :key="index" @click="toggleMobileMenu">
              <router-link :to="item.link">{{ item.name }}</router-link>
            </li>
          </ul>
        </transition>
      </div>
    </div>
    <div class="nav-right">
      <router-link v-if="!store.getters.hasUserInfo" to="/login">
        <button class="login-btn">登录/注册</button>
      </router-link>
      <router-link v-else to="/account/setting"><div class="user-info">
        <img :src="store.getters.profileImage" alt="User Avatar" class="avatar" />
        <span class="username">{{ store.state.userInfo.username }}</span>
      </div></router-link>
    </div>
  </nav>
</template>

<script setup>
import {ref, computed, onMounted, onUnmounted} from 'vue'
import {useStore} from 'vuex'
import '../assets/styles/fonts.css';

const store = useStore()

const theme = computed(() => store.getters.currentTheme)
const themeClass = computed(() => theme.value === 'dark' ? 'theme-dark' : 'theme-light')

const navItems = [
  {name: '主页', link: '/'},
  'divider',
  {name: '博客', link: '/blog'},
  {name: '项目', link: '/projects'},
  {name: '小工具', link: '/tools'},
  'divider',
  {name: '关于', link: '/about'}
]

// 移动端菜单
const showMobileMenu = ref(false)
const toggleMobileMenu = () => {
  showMobileMenu.value = !showMobileMenu.value
}

// 判定
const isMobile = ref(window.innerWidth < 768)
const handleResize = () => {
  isMobile.value = window.innerWidth < 888
  if (!isMobile.value) {
    showMobileMenu.value = false  // 桌面端时关闭移动菜单
  }
}

onMounted(() => {
  window.addEventListener('resize', handleResize)
})
onUnmounted(() => {
  window.removeEventListener('resize', handleResize)
})

const toggleTheme = () => {
  store.commit('toggleTheme')
}
</script>

<style scoped>
.theme-dark {
  background-color: rgba(40, 40, 40);
  color: #0ff;
}

.theme-dark .navbar {
  background-color: rgba(40, 40, 40);
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.2);
}

.theme-dark .nav-left .logo {
  color: #0ff;
}

.theme-dark .nav-items li a {
  color: #0ff;
}

.theme-dark .nav-items li a:hover {
  color: #fff;
}

.theme-dark .nav-items li a::after {
  background-color: #0ff;
}

.theme-dark .login-btn {
  border: 1px solid #0ff;
  color: #0ff;
}

.theme-dark .login-btn:hover {
  background: #0ff;
  color: #000;
}

.theme-dark .hamburger-icon,
.theme-dark .hamburger-icon::before,
.theme-dark .hamburger-icon::after {
  background: #0ff;
}

.theme-light {
  background-color: #fff;
  color: #333;
}

.theme-light .navbar {
  background-color: #fff;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.theme-light .nav-left .logo {
  color: #2f2f2f;
}

.theme-light .nav-items li a {
  color: #2f2f2f;
}

.theme-light .nav-items li a:hover {
  color: #000000;
}

.theme-light .nav-items li a::after {
  background-color: #2f2f2f;
}

.theme-light .login-btn {
  border: 1px solid #2f2f2f;
  color: #2f2f2f;
}

.theme-light .login-btn:hover {
  background: #2f2f2f;
  color: #fff;
}

.theme-light .hamburger-icon,
.theme-light .hamburger-icon::before,
.theme-light .hamburger-icon::after {
  background: #2f2f2f;
}

.navbar {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  z-index: 99;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 2rem;
  box-sizing: border-box;
  height: 60px;
  transition: all 0.2s;
}

.nav-left .logo {
  font-family: 'Netron', sans-serif;
  font-size: 1.5rem;
  font-weight: bold;
}

.nav-center {
  flex: 1;
  display: flex;
  justify-content: center;
}

.nav-items {
  list-style: none;
  display: flex;
  gap: 3rem;
  padding: 0;
  margin: 0;
}

.nav-items li a {
  text-decoration: none;
  position: relative;
  font-size: 1rem;
  transition: color 0.3s;
}

.nav-items li a::after {
  content: "";
  position: absolute;
  left: 0;
  bottom: -4px;
  width: 100%;
  height: 2px;
  transform: scaleX(0);
  transform-origin: center;
  transition: transform 0.3s ease;
}

.nav-items li a:hover::after {
  transform: scaleX(1);
}

.nav-items .divider {
  font-size: 1rem;
  padding: 0 0.7rem;
  color: gray;
  opacity: 60%;
}

.nav-right {
  display: flex;
  align-items: center;
}

.login-btn {
  background: transparent;
  padding: 0.5rem 1rem;
  border-radius: 4px;
  cursor: pointer;
  transition: background 0.3s, color 0.3s;
}

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

.user-info .avatar {
  width: 40px;
  height: 40px;
  object-fit: cover;
  border-radius: 50%;
}

/* 主题切换按钮 */
.theme-toggle-btn {
  margin-left: 1rem;
  background: transparent;
  border: 1px solid currentColor;
  padding: 0.5rem;
  border-radius: 4px;
  cursor: pointer;
}

/* 移动端：汉堡菜单样式 */
.mobile-menu {
  position: relative;
  display: flex;
  justify-content: center;
  width: 100%;
}

.hamburger-btn {
  width: 25px;
  height: 25px;
  background: transparent;
  border: none;
  cursor: pointer;
  padding: 0;
}

.hamburger-icon {
  width: 25px;
  height: 3px;
  display: block;
  position: relative;
  transition: all 0.3s ease;
}

.hamburger-icon::before,
.hamburger-icon::after {
  content: "";
  position: absolute;
  width: 25px;
  height: 3px;
  transition: all 0.3s ease;
}

.hamburger-icon::before {
  left: 0;
  top: -8px;
}

.hamburger-icon::after {
  left: 0;
  top: 8px;
}

/* 移动端下拉菜单 */
.mobile-nav-items {
  position: absolute;
  backdrop-filter: blur(10px);
  top: 45px;
  list-style: none;
  padding: 1rem;
  margin: 0;
  border-radius: 4px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.5);
  text-align: center;
}
.mobile-nav-items li {
  margin: 0.5rem;
  padding: 5px;
  border-radius: 4px;
  font-weight: bold;
  background: rgba(0, 0, 0, 0.2);
  transition: background 0.2s ease;
}
.theme-light .mobile-nav-items li {
  background: rgba(255, 255, 255, 0.2);
}
.mobile-nav-items li:hover {
  background: black;
}
.theme-light .mobile-nav-items li:hover {
  background: #ffffff;
}
.mobile-nav-items li a {
  text-decoration: none;
  font-size: 1rem;
}

/* 下拉菜单渐隐渐现动画 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
</style>
