function filtrarRev(){
    const q = (document.getElementById("searchRev").value || "").toLowerCase().trim();
    const rows = document.querySelectorAll("#tblRev tbody tr");
    rows.forEach(r=>{
        const t = (r.innerText || "").toLowerCase();
        r.style.display = t.includes(q) ? "" : "none";
    });
}

(function(){
    var modalEditar = document.getElementById('modalEditarRev');
    if(modalEditar){
        modalEditar.addEventListener('show.bs.modal', function(e){
            var btn = e.relatedTarget;
            if(!btn) return;

            var id = btn.getAttribute('data-id') || "";
            var nome = btn.getAttribute('data-nome') || "";
            var email = btn.getAttribute('data-email') || "";
            var username = btn.getAttribute('data-username') || "";
            var whatsapp = btn.getAttribute('data-whatsapp') || "";
            var mac_limit = btn.getAttribute('data-mac_limit') || "0";
            var vencimento = btn.getAttribute('data-vencimento') || "";
            var status = btn.getAttribute('data-status') || "ativo";
            var creditos = btn.getAttribute('data-creditos') || "0";

            var inputId = document.getElementById('editRevId');
            var inputNome = document.getElementById('editRevNome');
            var inputEmail = document.getElementById('editRevEmail');
            var inputUsername = document.getElementById('editRevUsername');
            var inputWhatsapp = document.getElementById('editRevWhatsapp');
            var inputMacLimit = document.getElementById('editRevMacLimit');
            var inputVenc = document.getElementById('editRevVencimento');
            var inputStatus = document.getElementById('editRevStatus');
            var inputPass = document.getElementById('editRevPassword');

            if(inputId) inputId.value = id;
            if(inputNome) inputNome.value = nome;
            if(inputEmail) inputEmail.value = email;
            if(inputUsername) inputUsername.value = username;
            if(inputWhatsapp) inputWhatsapp.value = whatsapp;
            if(inputMacLimit) inputMacLimit.value = mac_limit;
            if(inputVenc) inputVenc.value = vencimento;
            if(inputStatus) inputStatus.value = status;
            if(inputPass) inputPass.value = "";

            var inputCred = document.getElementById('editRevCreditos');
            if(inputCred) inputCred.value = creditos;
        });
    }

    var modalExcluir = document.getElementById('modalExcluirRev');
    if(modalExcluir){
        modalExcluir.addEventListener('show.bs.modal', function(e){
            var btn = e.relatedTarget;
            if(!btn) return;

            var id = btn.getAttribute('data-id') || "";
            var nome = btn.getAttribute('data-nome') || "";

            var inputId = document.getElementById('deleteRevId');
            var labelNome = document.getElementById('deleteRevNome');

            if(inputId) inputId.value = id;
            if(labelNome) labelNome.textContent = nome;
        });
    }
})();

(function(){
    var btnNova = document.getElementById('btnNovaRevenda');
    var modalCriar = document.getElementById('modalCriarRev');
    if(btnNova && modalCriar){
        btnNova.addEventListener('click', function(ev){
            try {
                if (typeof bootstrap !== 'undefined' && bootstrap.Modal) {
                    ev.preventDefault();
                    var instance = bootstrap.Modal.getOrCreateInstance(modalCriar);
                    instance.show();
                    return;
                }
            } catch(_e) {}

            modalCriar.classList.add('show');
            modalCriar.style.display = 'block';
            modalCriar.removeAttribute('aria-hidden');
            modalCriar.setAttribute('aria-modal', 'true');
            document.body.classList.add('modal-open');

            var backdrop = document.createElement('div');
            backdrop.className = 'modal-backdrop fade show revenda-backdrop-fallback';
            document.body.appendChild(backdrop);
        });
    }

    document.querySelectorAll('#modalCriarRev [data-bs-dismiss="modal"]').forEach(function(btn){
        btn.addEventListener('click', function(){
            var backdrop = document.querySelector('.revenda-backdrop-fallback');
            if(backdrop) backdrop.remove();
            if(modalCriar){
                modalCriar.classList.remove('show');
                modalCriar.style.display = '';
                modalCriar.setAttribute('aria-hidden', 'true');
                modalCriar.removeAttribute('aria-modal');
            }
            document.body.classList.remove('modal-open');
        });
    });
})();
