sdelay-sayt-chtob-abiturient/script.js

50 lines
1.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Пригласительный: обратный отсчёт + форма RSVP (отправляет в /api/rsvp)
const WEDDING_DATE = new Date("2026-11-01T16:00:00");
const cd = document.getElementById("countdown");
if (cd) {
function tick() {
const diff = WEDDING_DATE - new Date();
if (diff <= 0) {
cd.innerHTML = '<span class="kt-ai-chip">Сегодня наш день!</span>';
return;
}
const d = Math.floor(diff / 86400000);
const h = Math.floor(diff / 3600000) % 24;
const m = Math.floor(diff / 60000) % 60;
const s = Math.floor(diff / 1000) % 60;
cd.innerHTML =
cell(d, "дней") + cell(h, "часов") + cell(m, "минут") + cell(s, "секунд");
}
function cell(n, lab) {
return '<div class="kt-ai-cd-cell"><div class="kt-ai-cd-num">' + n + "</div><div class=\"kt-ai-cd-lab\">" + lab + "</div></div>";
}
tick();
setInterval(tick, 1000);
}
const form = document.getElementById("rsvp-form");
const msg = document.getElementById("rsvp-msg");
if (form) {
form.addEventListener("submit", async function (e) {
e.preventDefault();
msg.textContent = "Отправляю…";
try {
const body = Object.fromEntries(new FormData(form).entries());
const res = await fetch("api/rsvp", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});
if (!res.ok) throw new Error("server " + res.status);
const attendance = form.attendance.value;
msg.textContent = attendance === "yes"
? "Спасибо! До встречи 1 ноября."
: "Спасибо, что сообщили. Мы вас поздравим на расстоянии.";
form.reset();
} catch (err) {
msg.textContent = "Не получилось отправить. Попробуйте ещё раз или напишите организатору напрямую.";
}
});
}