<template>
  <div>
    <!-- Бургер-меню для всех устройств -->
    <div class="burger-menu-container">
      <button 
        class="burger-menu" 
        :class="{ 'active': isMobileMenuOpen }" 
        @click="toggleMobileMenu"
        :aria-label="isMobileMenuOpen ? 'Закрыть меню' : 'Открыть меню'"
        :aria-expanded="isMobileMenuOpen"
      >
        <span></span>
        <span></span>
        <span></span>
      </button>

      <!-- Выпадающее меню -->
      <div class="dropdown-menu" :class="{ 'active': isMobileMenuOpen }" ref="dropdownMenu">
        <nav class="dropdown-nav">
          <div class="mobile-menu-header">
            <div class="mobile-logo">
              <img src="../assets/logo/Logo.png" alt="Little logo begushiy bashkir" class="little-logo">
              <span>Бегущий Башкир</span>
            </div>
            <button class="close-menu" @click="closeMobileMenu" aria-label="Закрыть меню">
              ×
            </button>
          </div>
          
          <div class="dropdown-nav-content">
            <router-link to="/" class="dropdown-nav-link" @click="closeMobileMenu">
              🏠 Главная
            </router-link>
            <router-link to="/profile" class="dropdown-nav-link" @click="closeMobileMenu">
              👤 Профиль
            </router-link>
            <router-link to="/about" class="dropdown-nav-link" @click="closeMobileMenu">
              👥 О нас
            </router-link>
            <router-link to="/achievements" class="dropdown-nav-link" @click="closeMobileMenu">
              🏆 Достижения
            </router-link>
            <router-link to="/gallery" class="dropdown-nav-link" @click="closeMobileMenu">
              📸 Галерея
            </router-link>
            <router-link to="/training" class="dropdown-nav-link" @click="closeMobileMenu">
              📅 Тренировки
            </router-link>
            <router-link to="/news" class="dropdown-nav-link" @click="closeMobileMenu">
              📰 Новости
            </router-link>
            <router-link to="/members" class="dropdown-nav-link" @click="closeMobileMenu">
              👥 Участники
            </router-link>
            <router-link to="/reviews" class="dropdown-nav-link" @click="closeMobileMenu">
              ⭐ Отзывы
            </router-link>
            <div class="dropdown-divider"></div>
            <router-link to="/register" class="dropdown-nav-link accent" @click="closeMobileMenu">
              📝 Регистрация
            </router-link>
            <router-link to="/login" class="dropdown-nav-link accent" @click="closeMobileMenu">
              🔐 Войти
            </router-link>
          </div>
        </nav>
      </div>
    </div>

    <!-- Оверлей для мобильных -->
    <div class="mobile-overlay" :class="{ 'active': isMobileMenuOpen }" @click="closeMobileMenu"></div>
  </div>
</template>

<script>
export default {
  name: 'NavigationMenu',
  data() {
    return {
      isMobileMenuOpen: false,
      resizeTimeout: null
    }
  },
  methods: {
    toggleMobileMenu() {
      this.isMobileMenuOpen = !this.isMobileMenuOpen
      // Блокируем скролл body когда меню открыто на мобильных
      if (window.innerWidth <= 767) {
        document.body.style.overflow = this.isMobileMenuOpen ? 'hidden' : ''
      }
    },
    closeMobileMenu() {
      this.isMobileMenuOpen = false
      document.body.style.overflow = ''
    },
    handleClickOutside(event) {
      if (!event.target.closest('.burger-menu-container') && 
          !event.target.closest('.dropdown-menu') && 
          this.isMobileMenuOpen) {
        this.closeMobileMenu()
      }
    },
    handleEscapeKey(event) {
      if (event.key === 'Escape' && this.isMobileMenuOpen) {
        this.closeMobileMenu()
      }
    },
    handleResize() {
      // Дебаунс для оптимизации
      clearTimeout(this.resizeTimeout)
      this.resizeTimeout = setTimeout(() => {
        // Закрываем мобильное меню при переходе на десктоп
        if (window.innerWidth > 767 && this.isMobileMenuOpen) {
          this.closeMobileMenu()
        }
      }, 250)
    },
    handleTouchMove(event) {
      // Обработка свайпа для закрытия меню на мобильных
      if (this.isMobileMenuOpen && window.innerWidth <= 767) {
        const touch = event.touches[0]
        const startX = touch.clientX
        const menu = this.$refs.dropdownMenu
        
        // Более точная проверка для свайпа
        if (menu && startX < 50) { // Свайп от левого края
          event.preventDefault()
          this.closeMobileMenu()
        }
      }
    }
  },
  mounted() {
    document.addEventListener('click', this.handleClickOutside)
    document.addEventListener('keydown', this.handleEscapeKey)
    document.addEventListener('touchmove', this.handleTouchMove, { passive: false })
    window.addEventListener('resize', this.handleResize)
    
    // Закрываем меню при навигации
    this.$router.afterEach(() => {
      this.closeMobileMenu()
    })
  },
  beforeUnmount() {
    document.removeEventListener('click', this.handleClickOutside)
    document.removeEventListener('keydown', this.handleEscapeKey)
    document.removeEventListener('touchmove', this.handleTouchMove)
    window.removeEventListener('resize', this.handleResize)
    document.body.style.overflow = '' // Восстанавливаем скролл при размонтировании
  }
}
</script>

<style scoped>
/* Стили остаются такими же как в App.vue */
.burger-menu-container {
  position: relative;
  z-index: 1001;
}

.burger-menu {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  width: 30px;
  height: 21px;
  background: none;
  border: none;
  cursor: pointer;
  padding: 0;
  position: relative;
  transition: transform 0.3s ease;
}

.burger-menu:hover {
  transform: scale(1.1);
}

.burger-menu:active {
  transform: scale(0.95);
}

.burger-menu span {
  display: block;
  height: 3px;
  width: 100%;
  background-color: white;
  border-radius: 3px;
  transition: all 0.3s ease;
  transform-origin: center;
}

.burger-menu.active span:nth-child(1) {
  transform: rotate(45deg) translate(6px, 6px);
}

.burger-menu.active span:nth-child(2) {
  opacity: 0;
}

.burger-menu.active span:nth-child(3) {
  transform: rotate(-45deg) translate(6px, -6px);
}

.dropdown-menu {
  position: absolute;
  top: 100%;
  right: 0;
  background: white;
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
  min-width: 280px;
  opacity: 0;
  visibility: hidden;
  transform: translateY(-10px);
  transition: all 0.3s ease;
  border: 1px solid #e9ecef;
  margin-top: 10px;
  max-height: 80vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.dropdown-menu.active {
  opacity: 1;
  visibility: visible;
  transform: translateY(0);
}

.dropdown-nav {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.mobile-menu-header {
  display: none;
  justify-content: space-between;
  align-items: center;
  padding: 1rem 1.5rem;
  background: #2e8b57;
  color: white;
  border-bottom: 1px solid rgba(255,255,255,0.1);
}

.mobile-logo {
  display: flex;
  align-items: center;
  gap: 0.8rem;
  font-weight: bold;
}

.mobile-logo .little-logo {
  width: 2rem;
  height: 2rem;
}

.close-menu {
  display: none;
  background: none;
  border: none;
  color: white;
  font-size: 2rem;
  cursor: pointer;
  width: 40px;
  height: 40px;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: background-color 0.3s ease;
}

.close-menu:hover {
  background-color: rgba(255, 255, 255, 0.1);
}

.dropdown-nav-content {
  flex: 1;
  padding: 1rem 0;
  overflow-y: auto;
}

.dropdown-nav-link {
  display: flex;
  align-items: center;
  padding: 1rem 1.5rem;
  color: #333;
  text-decoration: none;
  transition: all 0.3s ease;
  font-size: 1rem;
  border-left: 3px solid transparent;
  white-space: nowrap;
}

.dropdown-nav-link:hover {
  background-color: #f8fff8;
  color: #2e8b57;
  border-left-color: #2e8b57;
  padding-left: 2rem;
}

.dropdown-nav-link:active {
  background-color: #e8f5e8;
  transform: translateX(5px);
}

.dropdown-nav-link.router-link-active {
  background-color: #f0f8f0;
  color: #2e8b57;
  border-left-color: #2e8b57;
  font-weight: 600;
}

.dropdown-nav-link.accent {
  background-color: #f8fff8;
  color: #2e8b57;
  font-weight: 600;
}

.dropdown-nav-link.accent:hover {
  background-color: #2e8b57;
  color: white;
}

.dropdown-divider {
  height: 1px;
  background: #e9ecef;
  margin: 0.5rem 1.5rem;
}

.mobile-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  opacity: 0;
  visibility: hidden;
  transition: all 0.3s ease;
  z-index: 998;
}

.mobile-overlay.active {
  opacity: 1;
  visibility: visible;
}

/* Адаптивность */
@media (max-width: 767px) {
  .dropdown-menu {
    position: fixed;
    top: 0;
    right: -100%;
    width: 85%;
    max-width: 320px;
    height: 100vh;
    border-radius: 0;
    margin-top: 0;
    transition: right 0.3s ease;
    box-shadow: -5px 0 25px rgba(0, 0, 0, 0.3);
    max-height: 100vh;
    overflow: hidden;
  }

  .dropdown-menu.active {
    right: 0;
  }

  .mobile-menu-header {
    display: flex;
    flex-shrink: 0;
  }

  .close-menu {
    display: flex;
  }

  .dropdown-nav-content {
    padding: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  .dropdown-nav-link {
    padding: 1.4rem 1.5rem;
    font-size: 1.1rem;
    border-left: none;
    border-bottom: 1px solid #f0f0f0;
  }

  .dropdown-nav-link:hover,
  .dropdown-nav-link.router-link-active {
    border-left: none;
    border-bottom: 1px solid #2e8b57;
    padding-left: 1.5rem;
  }
}

/* Стили для скроллбара */
.dropdown-nav-content::-webkit-scrollbar {
  width: 4px;
}

.dropdown-nav-content::-webkit-scrollbar-track {
  background: #f1f1f1;
}

.dropdown-nav-content::-webkit-scrollbar-thumb {
  background: #2e8b57;
  border-radius: 2px;
}

.dropdown-nav-content::-webkit-scrollbar-thumb:hover {
  background: #26734a;
}
</style>