const LAUNCH = new Date('2026-04-30T16:00:00+03:00');


function pad2(n) {
  return String(n).padStart(2, '0');
}


function tickCountdown() {
  const root = document.getElementById('countdown');
  if (!root) {
    return false;
  }
  const doneEl = document.getElementById('countdown-done');
  const diff = LAUNCH.getTime() - Date.now();
  if (diff <= 0) {
    root.querySelectorAll('.countdown__units, .countdown__title').forEach((el) => {
      el.setAttribute('hidden', '');
    });
    if (doneEl) {
      doneEl.removeAttribute('hidden');
    }
    return false;
  }
  const totalSeconds = Math.floor(diff / 1000);
  const days = Math.floor(totalSeconds / 86400);
  const hours = Math.floor((totalSeconds % 86400) / 3600);
  const minutes = Math.floor((totalSeconds % 3600) / 60);
  const seconds = totalSeconds % 60;
  const map = {
    days: days < 100 ? pad2(days) : String(days),
    hours: pad2(hours),
    minutes: pad2(minutes),
    seconds: pad2(seconds),
  };
  Object.keys(map).forEach((key) => {
    const el = root.querySelector(`[data-unit='${key}']`);
    if (el) {
      el.textContent = map[key];
    }
  });
  return true;
}


function initCabinetSubmit() {
  const form = document.getElementById('cabinet-form');
  const status = document.getElementById('cabinet-status');
  if (!form) {
    return;
  }
  const submitBtn = form.querySelector('.cabinet__submit');
  form.addEventListener('submit', async (e) => {
    e.preventDefault();
    const cfg = window.__SITE_CONFIG__ || {};
    const url = cfg.notifyUrl || '/notify';
    const fd = new FormData(form);
    const name = String(fd.get('name') || '').trim();
    const email = String(fd.get('email') || '').trim();
    if (status) {
      status.setAttribute('hidden', '');
      status.textContent = '';
    }
    if (submitBtn) {
      submitBtn.disabled = true;
    }
    try {
      const res = await fetch(url, {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({ name, email }),
      });
      if (!res.ok) {
        throw new Error(String(res.status));
      }
      if (status) {
        status.removeAttribute('hidden');
        status.textContent = 'Заявка отправлена';
      }
      form.reset();
    } catch {
      if (status) {
        status.removeAttribute('hidden');
        status.textContent = 'Не удалось отправить заявку';
      }
    } finally {
      if (submitBtn) {
        submitBtn.disabled = false;
      }
    }
  });
}


function initNav() {
  const btn = document.querySelector('.burger');
  if (!btn) {
    return;
  }
  btn.addEventListener('click', () => {
    document.body.classList.toggle('nav-open');
  });
  document.querySelectorAll('.nav__link').forEach((link) => {
    link.addEventListener('click', () => {
      document.body.classList.remove('nav-open');
    });
  });
}


function startCountdown() {
  if (!tickCountdown()) {
    return;
  }
  const countdownTimer = setInterval(() => {
    if (!tickCountdown()) {
      clearInterval(countdownTimer);
    }
  }, 1000);
}


startCountdown();
initNav();
initCabinetSubmit();
