document.addEventListener('DOMContentLoaded', function () { var checks = document.querySelectorAll('.qr-check'); var checkAll = document.getElementById('checkAll'); var btnSelectAll = document.getElementById('btnSelectAll'); var btnDownloadSelected = document.getElementById('btnDownloadSelected'); var formBulk = document.getElementById('formBulkSelected'); var previewModal = document.getElementById('qrPreviewModal'); function updateBulkButton() { if (!btnDownloadSelected) return; var selected = document.querySelectorAll('.qr-check:checked'); btnDownloadSelected.disabled = selected.length === 0; btnDownloadSelected.innerHTML = 'download ' + 'Descargar seleccionados (' + selected.length + ')'; } checks.forEach(function (cb) { cb.addEventListener('change', updateBulkButton); }); if (checkAll) { checkAll.addEventListener('change', function () { checks.forEach(function (cb) { cb.checked = checkAll.checked; }); updateBulkButton(); }); } if (btnSelectAll) { btnSelectAll.addEventListener('click', function () { var allChecked = Array.from(checks).every(function (cb) { return cb.checked; }); checks.forEach(function (cb) { cb.checked = !allChecked; }); if (checkAll) checkAll.checked = !allChecked; updateBulkButton(); }); } if (btnDownloadSelected && formBulk) { btnDownloadSelected.addEventListener('click', function () { formBulk.querySelectorAll('input[name="codes[]"]').forEach(function (el) { el.remove(); }); document.querySelectorAll('.qr-check:checked').forEach(function (cb) { var input = document.createElement('input'); input.type = 'hidden'; input.name = 'codes[]'; input.value = cb.value; formBulk.appendChild(input); }); formBulk.submit(); }); } document.querySelectorAll('.btn-qr-preview').forEach(function (btn) { btn.addEventListener('click', function () { var codigo = btn.dataset.codigo; var modulo = btn.dataset.modulo; var ubicacion = btn.dataset.ubicacion; var url = btn.dataset.url; document.getElementById('qrPreviewTitle').textContent = ubicacion + ' — ' + codigo; document.getElementById('qrPreviewImg').src = url + '?t=' + Date.now(); document.getElementById('qrPreviewCodigo').textContent = codigo; document.getElementById('qrPreviewCaption').textContent = modulo + ' | ' + ubicacion; document.getElementById('qrPreviewDownload').href = 'download_qr.php?codigo=' + encodeURIComponent(codigo); if (previewModal && window.bootstrap) { bootstrap.Modal.getOrCreateInstance(previewModal).show(); } }); }); updateBulkButton(); });