console.log("JS jalan");

/* ===== ELEMENT ===== */
const overlay = document.getElementById("overlay");
const main = document.getElementById("main-content");
const footer = document.getElementById("footer");
const music = document.getElementById("bg-music");
const musicImg = document.getElementById("music-img");
const icon = document.getElementById("music-icon");
const musicPlayer = document.getElementById("music-player");

/* ===== OPEN INVITE ===== */
function openInvite() {

    main.style.display = "block";
    footer.style.display = "block";

    overlay.classList.add("drop");

    setTimeout(() => {
        overlay.style.display = "none";
        document.body.classList.remove("lock");

        if (musicPlayer) musicPlayer.style.display = "flex";

        music.play().catch(() => {});
        musicImg.classList.add("spin");

        if (icon) icon.className = "fa-solid fa-pause";

        // ðŸ”¥ TRIGGER ANIMASI DI SINI
        triggerScrollAnimation();

    }, 800);
}

/* ===== TOGGLE MUSIC ===== */
function toggleMusic() {

    if (!music || !musicImg || !icon) return;

    if (music.paused) {
        music.play();
        musicImg.classList.add("spin");

        // ðŸ”¥ sekarang lagi jalan â†’ tampilkan PAUSE
        icon.className = "fa-solid fa-pause";

    } else {
        music.pause();
        musicImg.classList.remove("spin");

        // ðŸ”¥ sekarang berhenti â†’ tampilkan PLAY
        icon.className = "fa-solid fa-play";
    }
}

/* ===== SWIPER ===== */
var swiper = new Swiper(".mySwiper", {
    slidesPerView: 3,
    spaceBetween: 30,
    centeredSlides: true,
    loop: true,

    autoplay: {
        delay: 3000,
        disableOnInteraction: false,
    },

    navigation: {
        nextEl: ".swiper-button-next",
        prevEl: ".swiper-button-prev",
    },

    breakpoints: {
        0: { slidesPerView: 1 },
        768: { slidesPerView: 2 },
        1024: { slidesPerView: 3 }
    }
});

/* ===== COUNTDOWN ===== */
document.addEventListener("DOMContentLoaded", function() {

    const targetDate = new Date("2026-05-30T09:00:00+07:00").getTime();

    const daysEl = document.getElementById("days");
    const hoursEl = document.getElementById("hours");
    const minutesEl = document.getElementById("minutes");
    const secondsEl = document.getElementById("seconds");

    if (!daysEl) return;

    setInterval(() => {
        const now = new Date().getTime();
        const distance = targetDate - now;

        if (distance < 0) return;

        const days = Math.floor(distance / (1000 * 60 * 60 * 24));
        const hours = Math.floor((distance / (1000 * 60 * 60)) % 24);
        const minutes = Math.floor((distance / (1000 * 60)) % 60);
        const seconds = Math.floor((distance / 1000) % 60);

        daysEl.innerText = String(days).padStart(2, '0');
        hoursEl.innerText = String(hours).padStart(2, '0');
        minutesEl.innerText = String(minutes).padStart(2, '0');
        secondsEl.innerText = String(seconds).padStart(2, '0');

    }, 1000);
});

/* ===== COPY REKENING ===== */
function copyRekening(btn) {

    const item = btn.closest(".gift-item");
    if (!item) return;

    const el = item.querySelector(".rekening");
    if (!el) return;

    // ðŸ”¥ ambil angka & bersihin spasi
    const text = el.innerText.replace(/\s+/g, '');

    // ðŸ”¥ modern copy + fallback
    if (navigator.clipboard) {
        navigator.clipboard.writeText(text).then(success, fallback);
    } else {
        fallback();
    }

    function success() {
        btn.innerHTML = '<i class="fa-solid fa-check"></i> SUCCESSFULLY COPIED';
        btn.disabled = true;

        setTimeout(() => {
            btn.innerHTML = '<i class="fa-regular fa-copy"></i> COPY ACCOUNT NUMBER';
            btn.disabled = false;
        }, 2000);
    }

    function fallback() {
        const textarea = document.createElement("textarea");
        textarea.value = text;
        document.body.appendChild(textarea);
        textarea.select();
        document.execCommand("copy");
        document.body.removeChild(textarea);

        success();
    }
}

/* ===== SECTION SCROLL ANIMATION ===== */
document.addEventListener("DOMContentLoaded", function() {

    const sections = document.querySelectorAll("section");

    function showOnScroll() {
        sections.forEach(sec => {
            const rect = sec.getBoundingClientRect();

            if (rect.top < window.innerHeight - 120) {
                sec.classList.add("show");
            }
        });
    }

    window.addEventListener("scroll", showOnScroll);
});

function triggerScrollAnimation() {
    const sections = document.querySelectorAll("section");

    sections.forEach(sec => {
        const rect = sec.getBoundingClientRect();

        if (rect.top < window.innerHeight - 120) {
            sec.classList.add("show");
        }
    });
}

// =========================
// ALERT KONTEN
// =========================
document.addEventListener("DOMContentLoaded", function() {

    const alertBox = document.querySelector(".nala-alert");

    let alertTimeout;

    // =========================
    // SHOW ALERT
    // =========================
    function showAlert() {

        if (!alertBox) return;

        alertBox.classList.add("show");

        clearTimeout(alertTimeout);

        alertTimeout = setTimeout(() => {

            alertBox.classList.remove("show");

        }, 2200);
    }

    // =========================
    // RIGHT CLICK
    // =========================
    document.addEventListener("contextmenu", function(e) {

        // ELEMENT YANG DIIZINKAN
        if (
            e.target.closest(".copy-btn") ||
            e.target.closest(".music-btn") ||
            e.target.closest(".swiper") ||
            e.target.closest(".swiper-button-next") ||
            e.target.closest(".swiper-button-prev") ||
            e.target.closest(".maps-btn") ||
            e.target.closest(".open-invite-btn")
        ) {
            return;
        }

        e.preventDefault();

        showAlert();

    });

    // =========================
    // BLOCK COPY
    // =========================
    document.addEventListener("copy", function(e) {

        // izinkan copy rekening
        if (
            e.target.closest(".copy-btn")
        ) {
            return;
        }

        e.preventDefault();

        showAlert();

    });

    // =========================
    // BLOCK CUT
    // =========================
    document.addEventListener("cut", function(e) {

        e.preventDefault();

        showAlert();

    });

    // =========================
    // BLOCK SELECT
    // =========================
    document.addEventListener("selectstart", function(e) {

        // izinkan swiper
        if (
            e.target.closest(".swiper")
        ) {
            return;
        }

        e.preventDefault();

    });

    // =========================
    // BLOCK DRAG
    // =========================
    document.addEventListener("dragstart", function(e) {

        // izinkan swiper
        if (
            e.target.closest(".swiper")
        ) {
            return;
        }

        e.preventDefault();

        showAlert();

    });

    // =========================
    // DOUBLE CLICK
    // =========================
    document.addEventListener("dblclick", function(e) {

        // izinkan swiper
        if (
            e.target.closest(".swiper")
        ) {
            return;
        }

        e.preventDefault();

        showAlert();

    });

    // =========================
    // HOLD RIGHT CLICK
    // =========================
    let rightClickHold;

    document.addEventListener("mousedown", function(e) {

        // klik kanan
        if (e.button === 2) {

            rightClickHold = setTimeout(() => {

                // jangan aktif di area yang diizinkan
                if (
                    e.target.closest(".copy-btn") ||
                    e.target.closest(".music-btn") ||
                    e.target.closest(".swiper") ||
                    e.target.closest(".swiper-button-next") ||
                    e.target.closest(".swiper-button-prev") ||
                    e.target.closest(".maps-btn") ||
                    e.target.closest(".open-invite-btn")
                ) {
                    return;
                }

                showAlert();

            }, 1);

        }

    });

    document.addEventListener("mouseup", function() {

        clearTimeout(rightClickHold);

    });

    // =========================
    // KEY PROTECTION
    // =========================
    document.addEventListener("keydown", function(e) {

        // F12
        if (e.key === "F12") {

            e.preventDefault();

            showAlert();
        }

        // CTRL + SHIFT + I
        if (
            e.ctrlKey &&
            e.shiftKey &&
            e.key.toLowerCase() === "i"
        ) {

            e.preventDefault();

            showAlert();
        }

        // CTRL + SHIFT + J
        if (
            e.ctrlKey &&
            e.shiftKey &&
            e.key.toLowerCase() === "j"
        ) {

            e.preventDefault();

            showAlert();
        }

        // CTRL + U
        if (
            e.ctrlKey &&
            e.key.toLowerCase() === "u"
        ) {

            e.preventDefault();

            showAlert();
        }

        // CTRL + S
        if (
            e.ctrlKey &&
            e.key.toLowerCase() === "s"
        ) {

            e.preventDefault();

            showAlert();
        }

        // CTRL + C
        if (
            e.ctrlKey &&
            e.key.toLowerCase() === "c"
        ) {

            // izinkan copy rekening
            if (
                document.activeElement.closest(".copy-btn")
            ) {
                return;
            }

            e.preventDefault();

            showAlert();
        }

    });

    // =========================
    // DEVTOOLS DETECT
    // =========================
    setInterval(function() {

        const devtoolsOpen =
            window.outerWidth - window.innerWidth > 160 ||
            window.outerHeight - window.innerHeight > 160;

        if (devtoolsOpen) {

            showAlert();
        }

    }, 1000);

});

// =========================
// RSVP FORM WA
// =========================

const rsvpForm = document.getElementById("rsvpForm");

rsvpForm.addEventListener("submit", function(e) {

    e.preventDefault();

    const nama = document.getElementById("nama").value;
    const jumlah = document.getElementById("jumlah").value;
    const kehadiran = document.getElementById("kehadiran").value;
    const ucapan = document.getElementById("ucapan").value;

    // NOMOR WHATSAPP
    const phone = "6285770324021";

    const message =
        `RSVP WEDDING
Nuryadi & Suhaeni

----------------------------

Nama Tamu :
${nama}

Jumlah Tamu :
${jumlah}

Konfirmasi Kehadiran :
${kehadiran}

Ucapan & Doa :
${ucapan}

----------------------------

Terima kasih atas perhatian dan doa terbaiknya.`;
    const url =
        `https://wa.me/${phone}?text=${encodeURIComponent(message)}`;

    location.href = url;

});

// =========================
// RSVP FORM EXCEL
// =========================

// const rsvpForm = document.getElementById("rsvpForm");

// const scriptURL =
//     "https://script.google.com/macros/s/AKfycbw-5uZNLLFvQMCgj6z6R9ghhRNRyBZ3d6co56yk9nyfAbIB3mpJExKgFh14jRLQId5_/exec";

// rsvpForm.addEventListener("submit", async function(e) {

//     e.preventDefault();

//     const data = {
//         nama: document.getElementById("nama").value,
//         jumlah: document.getElementById("jumlah").value,
//         kehadiran: document.getElementById("kehadiran").value,
//         ucapan: document.getElementById("ucapan").value
//     };

//     try {

//         await fetch(scriptURL, {

//             method: "POST",

//             mode: "no-cors",

//             headers: {
//                 "Content-Type": "application/json"
//             },

//             body: JSON.stringify(data)

//         });

//         alert("RSVP berhasil dikirim ðŸ’–");

//         rsvpForm.reset();

//     } catch (error) {

//         console.log(error);

//         alert("Terjadi kesalahan");

//     }

// });