/* NEMETH PUBLISHERS — main.js */
document.addEventListener('DOMContentLoaded', () => {

  /* ---- Mobile menu ---- */
  const burger  = document.getElementById('hamburger');
  const menu    = document.getElementById('navMenu');
  const overlay = document.getElementById('navOverlay');

  function closeMenu() {
    menu?.classList.remove('open');
    burger?.classList.remove('active');
    overlay?.classList.remove('open');
    document.body.style.overflow = '';
  }

  burger?.addEventListener('click', () => {
    const open = menu.classList.toggle('open');
    burger.classList.toggle('active', open);
    overlay?.classList.toggle('open', open);
    document.body.style.overflow = open ? 'hidden' : '';
  });

  overlay?.addEventListener('click', closeMenu);
  document.querySelectorAll('.nav-link').forEach(l => l.addEventListener('click', closeMenu));

  /* ---- Header scroll ---- */
  const header = document.getElementById('header');
  const backTop = document.getElementById('backTop');

  window.addEventListener('scroll', () => {
    header?.classList.toggle('scrolled', window.scrollY > 80);
    backTop?.classList.toggle('show', window.scrollY > 500);
  }, { passive: true });

  backTop?.addEventListener('click', () => window.scrollTo({ top: 0, behavior: 'smooth' }));

  /* ---- Smooth anchor scroll ---- */
  document.querySelectorAll('a[href^="#"]').forEach(a => {
    a.addEventListener('click', e => {
      const t = document.querySelector(a.getAttribute('href'));
      if (!t) return;
      e.preventDefault();
      window.scrollTo({ top: t.getBoundingClientRect().top + scrollY - 85, behavior: 'smooth' });
    });
  });

  /* ---- Scroll reveal ---- */
  const observer = new IntersectionObserver(entries => {
    entries.forEach(e => {
      if (e.isIntersecting) {
        e.target.classList.add('visible');
        observer.unobserve(e.target);
      }
    });
  }, { threshold: 0.08, rootMargin: '0px 0px -40px 0px' });

  document.querySelectorAll('.reveal').forEach(el => observer.observe(el));

  /* ---- Active nav link ---- */
  const page = location.pathname.split('/').pop() || 'index.html';
  document.querySelectorAll('.nav-link').forEach(l => {
    if (l.getAttribute('href') === page) l.classList.add('active');
  });

  /* ---- Contact form — AJAX submission with PHP backend ---- */
  const cform = document.getElementById('contactForm');
  cform?.addEventListener('submit', async (e) => {
    e.preventDefault();
    
    const submitBtn = cform.querySelector('button[type="submit"]');
    const originalBtnText = submitBtn.innerHTML;
    
    // Get form values
    const formData = new FormData();
    formData.append('name', document.getElementById('cName')?.value.trim() || '');
    formData.append('email', document.getElementById('cEmail')?.value.trim() || '');
    formData.append('subject', document.getElementById('cSubject')?.value.trim() || '');
    formData.append('journal', document.getElementById('cJournal')?.value.trim() || '');
    formData.append('message', document.getElementById('cMsg')?.value.trim() || '');
    
    // Client-side validation
    const name = formData.get('name');
    const email = formData.get('email');
    const subject = formData.get('subject');
    const message = formData.get('message');
    
    if (!name || !email || !subject || !message) {
      alert('Please fill in all required fields.');
      return;
    }
    
    if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
      alert('Please enter a valid email address.');
      return;
    }
    
    // Disable submit button and show loading state
    submitBtn.disabled = true;
    submitBtn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> Sending...';
    
    try {
      // Send form data to PHP backend
      const response = await fetch('contact-handler.php', {
        method: 'POST',
        body: formData
      });
      
      const result = await response.json();
      
      if (result.success) {
        // Success
        alert(result.message);
        cform.reset();
      } else {
        // Error from server
        alert(result.message);
      }
      
    } catch (error) {
      // Network or parsing error
      console.error('Form submission error:', error);
      alert('Sorry, there was an error sending your message. Please try again or contact us directly at nemethpublishers@gmail.com');
    } finally {
      // Re-enable submit button
      submitBtn.disabled = false;
      submitBtn.innerHTML = originalBtnText;
    }
  });

  /* ---- Policies accordion ---- */
  document.querySelectorAll('.policy-head').forEach(btn => {
    btn.addEventListener('click', () => {
      const item = btn.closest('.policy-item');
      item.classList.toggle('open');
    });
  });
});
