
const revealItems = document.querySelectorAll('[data-reveal]');
const revealObserver = new IntersectionObserver((entries) => {
  entries.forEach((entry) => {
    if (entry.isIntersecting) {
      entry.target.classList.add('is-visible');
      revealObserver.unobserve(entry.target);
    }
  });
}, { threshold: 0.14 });

revealItems.forEach((item) => revealObserver.observe(item));

document.querySelectorAll('.faq-question').forEach((button) => {
  button.addEventListener('click', () => {
    const item = button.closest('.faq-item');
    item.classList.toggle('active');
  });
});

document.querySelectorAll('a[href^="#"]').forEach((link) => {
  link.addEventListener('click', (event) => {
    const target = document.querySelector(link.getAttribute('href'));
    if (target) {
      event.preventDefault();
      target.scrollIntoView({ behavior: 'smooth', block: 'start' });
    }
  });
});

const form = document.querySelector('.lead-form');
const formMessage = document.querySelector('.form-message');

if (form) {
  form.addEventListener('submit', async (event) => {
    event.preventDefault();

    const button = form.querySelector('button[type="submit"]');
    const defaultText = button.textContent;
    button.textContent = 'Отправка...';
    button.disabled = true;
    formMessage.textContent = '';

    try {
      const response = await fetch('send.php', {
        method: 'POST',
        body: new FormData(form)
      });

      const result = await response.json();

      if (!response.ok || !result.success) {
        throw new Error(result.message || 'Не удалось отправить заявку');
      }

      form.reset();
      formMessage.textContent = result.message || 'Заявка отправлена. Мы свяжемся с вами в ближайшее время.';
      formMessage.classList.remove('error');
      formMessage.classList.add('success');
    } catch (error) {
      formMessage.textContent = error.message || 'Ошибка отправки. Попробуйте позже.';
      formMessage.classList.remove('success');
      formMessage.classList.add('error');
    } finally {
      button.textContent = defaultText;
      button.disabled = false;
    }
  });
}
