/* Comportamentos exclusivos da página onlines.php */

window.ONLINES_MODAL_OPEN = false;

document.addEventListener("DOMContentLoaded", function () {
    function setText(id, value) {
        var el = document.getElementById(id);
        if (el) el.textContent = value && String(value).trim() !== "" ? value : "—";
    }

    function openOnlineDetails(button) {
        setText("odCliente", button.getAttribute("data-cliente"));
        setText("odMac", button.getAttribute("data-mac"));
        setText("odConteudo", button.getAttribute("data-conteudo"));
        setText("odTipo", button.getAttribute("data-tipo"));
        setText("odLocal", button.getAttribute("data-local"));
        setText("odLast", button.getAttribute("data-last"));
        setText("odIp", button.getAttribute("data-ip"));

        var modal = document.getElementById("onlineDetailsModal");
        if (!modal) return;

        window.ONLINES_MODAL_OPEN = true;

        if (window.bootstrap && window.bootstrap.Modal) {
            window.bootstrap.Modal.getOrCreateInstance(modal).show();
            return;
        }

        modal.classList.add("show", "manual-show");
        modal.removeAttribute("aria-hidden");
        modal.setAttribute("aria-modal", "true");
        document.body.classList.add("modal-open-manual");
    }

    function closeOnlineDetails() {
        var modal = document.getElementById("onlineDetailsModal");
        if (!modal) return;

        window.ONLINES_MODAL_OPEN = false;

        if (window.bootstrap && window.bootstrap.Modal) {
            var instance = window.bootstrap.Modal.getInstance(modal);
            if (instance) {
                instance.hide();
                return;
            }
        }

        modal.classList.remove("show", "manual-show");
        modal.setAttribute("aria-hidden", "true");
        modal.removeAttribute("aria-modal");
        document.body.classList.remove("modal-open-manual");
    }

    document.addEventListener("click", function (event) {
        var detailsButton = event.target.closest(".btn-online-details");
        if (detailsButton) {
            event.preventDefault();
            openOnlineDetails(detailsButton);
            return;
        }

        var closeButton = event.target.closest("[data-bs-dismiss='modal']");
        if (closeButton && closeButton.closest("#onlineDetailsModal")) {
            event.preventDefault();
            closeOnlineDetails();
            return;
        }

        if (event.target && event.target.id === "onlineDetailsModal") {
            closeOnlineDetails();
        }
    });

    var modal = document.getElementById("onlineDetailsModal");
    if (modal) {
        modal.addEventListener("hidden.bs.modal", function () {
            window.ONLINES_MODAL_OPEN = false;
        });
        modal.addEventListener("shown.bs.modal", function () {
            window.ONLINES_MODAL_OPEN = true;
        });
    }

    document.addEventListener("keydown", function (event) {
        if (event.key === "Escape" && window.ONLINES_MODAL_OPEN) {
            closeOnlineDetails();
        }
    });
});

setTimeout(function () {
  if (!window.ONLINES_MODAL_OPEN) window.location.reload();
}, 10000);
