/* =============================== */
/* DemoBank Global JavaScript       */
/* =============================== */

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

    // ===============================
    // SIDEBAR TOGGLE (MOBILE)
    // ===============================
    const sidebarToggle = document.querySelector(".sidebar-toggle");
    const sidebar = document.querySelector(".sidebar");

    if (sidebarToggle && sidebar) {
        sidebarToggle.addEventListener("click", function () {
            sidebar.classList.toggle("active");
        });
    }

    // ===============================
    // TRANSFER TYPE HANDLING
    // ===============================
    const transferType = document.querySelector("select[name='transfer_type']");
    const intlFields = document.getElementById("intlFields");

    function handleTransferType() {
        if (!transferType || !intlFields) return;

        if (transferType.value === "international") {
            intlFields.style.display = "block";
        } else {
            intlFields.style.display = "none";
        }
    }

    if (transferType) {
        transferType.addEventListener("change", handleTransferType);
        handleTransferType(); // run on load
    }

    // ===============================
    // FORM VALIDATION HELPERS
    // ===============================
    function showError(message) {
        alert("Error: " + message);
    }

    function showSuccess(message) {
        alert("Success: " + message);
    }

    // ===============================
    // CONFIRM ACTION (SAFE DELETE / CANCEL)
    // ===============================
    window.confirmAction = function (message) {
        return confirm(message || "Are you sure?");
    };

    // ===============================
    // TRANSFER AMOUNT VALIDATION (CLIENT SIDE CHECK)
    // NOTE: Backend MUST also validate
    // ===============================
    const transferForm = document.querySelector("#transferForm");

    if (transferForm) {
        transferForm.addEventListener("submit", function (e) {

            const amountInput = transferForm.querySelector("input[name='amount']");
            const fee = parseFloat(transferForm.dataset.fee || 0);
            const balance = parseFloat(transferForm.dataset.balance || 0);

            if (amountInput) {
                const amount = parseFloat(amountInput.value || 0);
                const total = amount + fee;

                if (total > balance) {
                    e.preventDefault();
                    alert("Insufficient balance. Total (amount + fee) exceeds available balance.");
                    return false;
                }
            }
        });
    }

    // ===============================
    // OTP / TOKEN STEP UI (HOOKS)
    // ===============================
    window.showOtpStep = function () {
        const otpBox = document.querySelector("#otpBox");
        if (otpBox) {
            otpBox.style.display = "block";
        }
    };

    window.hideOtpStep = function () {
        const otpBox = document.querySelector("#otpBox");
        if (otpBox) {
            otpBox.style.display = "none";
        }
    };

    // ===============================
    // AUTO DISABLE BUTTON AFTER CLICK (PREVENT DOUBLE SUBMIT)
    // ===============================
    document.querySelectorAll("form").forEach(form => {
        form.addEventListener("submit", function () {
            const btn = form.querySelector("button[type='submit']");
            if (btn) {
                btn.disabled = true;
                btn.innerText = "Processing...";
            }
        });
    });

    // ===============================
    // NOTIFICATION AUTO HIDE
    // ===============================
    const alerts = document.querySelectorAll(".alert-auto-hide");

    alerts.forEach(alert => {
        setTimeout(() => {
            alert.style.display = "none";
        }, 4000);
    });

});