export function showModal(type, title, message) {
    const modal = document.getElementById("customModal");
    const modalIcon = document.getElementById("modalIcon");
    const modalTitle = document.getElementById("modalTitle");
    const modalMessage = document.getElementById("modalMessage");

    modalIcon.className = "modal-icon";

    if (type === "success") {
        modalIcon.classList.add("success");
        modalTitle.textContent = title || "Register Successful!";
    } else {
        modalIcon.classList.add("error");
        modalTitle.textContent = title || "Register Failed!";
    }

    modalMessage.textContent = message;
    modal.classList.add("show");
}

export function closeModal() {
    document.getElementById("customModal").classList.remove("show");
}

export function setupModalOk() {
    document.getElementById("modalOkBtn").addEventListener("click", () => {
        const modalIcon = document.getElementById("modalIcon");

        if (modalIcon.classList.contains("success")) {
            window.location.href = "Homepage.html"
        } else {
            closeModal();
        }
    });
}

export function setupOutsideClose() {
    document.getElementById("customModal").addEventListener("click", (e) => {
        const modalIcon = document.getElementById("modalIcon");

        if (e.target === e.currentTarget) {
            if (modalIcon.classList.contains("success")) {
                window.location.href = "Login.html";
            } else {
                closeModal();
            }
        }
    });
}