Files
admin-edu-space/frontend/views/partials/user_qr_modal.html
T

305 lines
14 KiB
HTML

<style>
/* Asegurar aislamiento modal total e impedir scroll o clics detrás */
#userQrCredentialModal {
z-index: 1080 !important;
overscroll-behavior: contain;
}
.modal-backdrop.show {
z-index: 1075 !important;
opacity: 0.65 !important;
}
html.modal-open,
body.modal-open {
overflow: hidden !important;
height: 100% !important;
touch-action: none;
overscroll-behavior: none;
}
</style>
<!-- Modal Global de Credencial Digital QR del Usuario (UniCABA) -->
<div class="modal fade" id="userQrCredentialModal" tabindex="-1" aria-labelledby="userQrCredentialModalLabel" aria-hidden="true" data-bs-backdrop="true" data-bs-keyboard="true">
<div class="modal-dialog modal-dialog-centered" style="max-width: 440px;">
<div class="modal-content border-0 shadow-lg overflow-hidden" style="border-radius: 18px;">
<!-- Encabezado con estética de Carnet Universitario -->
<div class="p-4 text-white position-relative" style="background: linear-gradient(135deg, #1E1B4B 0%, #4338CA 50%, #B43E8E 100%);">
<button type="button" class="btn-close btn-close-white position-absolute top-0 end-0 m-3" data-bs-dismiss="modal" aria-label="Cerrar"></button>
<div class="d-flex align-items-center gap-2 mb-2">
<img src="/img/logo_unicaba_mini.png" alt="UniCABA" height="26" class="bg-white rounded-circle p-1 shadow-sm">
<span class="fw-bold tracking-wider fs-6 text-uppercase">UniCABA · Identidad Digital</span>
</div>
<div class="d-flex align-items-center justify-content-between mt-2">
<div>
<h5 class="modal-title fw-bold text-white mb-0" id="userQrCredentialModalLabel">
Credencial Universitaria
</h5>
<p class="text-white-50 small mb-0 mt-1">Acceso a sedes y convenios de beneficios</p>
</div>
<span class="badge bg-success-subtle text-success border border-success-subtle rounded-pill px-2 py-1 small fw-bold">
● ACTIVO
</span>
</div>
</div>
<!-- Cuerpo del Modal -->
<div class="modal-body p-4 text-center">
<!-- Datos del Usuario -->
<div class="mb-3">
<h5 class="fw-bold mb-1 text-body" id="modalQrUserName">
{{ auth_user.name if (auth_user and auth_user.name) else (user.name if user else 'Usuario') }}
</h5>
<div class="d-flex align-items-center justify-content-center gap-2 flex-wrap mb-2">
<span class="badge bg-primary-subtle text-primary border border-primary-subtle font-monospace text-uppercase" style="font-size: 0.74rem;">
{{ auth_user.role_badge_display.label if (auth_user and auth_user.role_badge_display) else (auth_user.role|upper if auth_user else 'ALUMNO') }}
</span>
{% if auth_user and auth_user.document_number %}
<span class="badge bg-body-tertiary text-body-secondary border font-monospace" style="font-size: 0.74rem;">
DNI: {{ auth_user.document_number }}
</span>
{% endif %}
</div>
<div class="text-muted small font-monospace" style="font-size: 0.78rem;">
{{ auth_user.email if auth_user else '' }}
</div>
</div>
<!-- Contenedor del Código QR Dinámico -->
<div class="p-3 bg-body-tertiary border rounded-4 d-inline-block shadow-sm my-2 position-relative" style="background-color: var(--bs-body-bg) !important;">
<div id="globalQrModalContainer" class="d-flex align-items-center justify-content-center" style="min-width: 190px; min-height: 190px;">
<div class="spinner-border text-primary" role="status" id="globalQrSpinner">
<span class="visually-hidden">Generando credencial QR...</span>
</div>
</div>
</div>
<!-- Barra de Progreso y Cuenta Regresiva de Renovación Dinámica -->
<div class="mt-2 mb-2 px-3" style="max-width: 260px; margin: 0 auto;">
<div class="progress" style="height: 4px; background-color: rgba(180, 62, 142, 0.15); border-radius: 4px;">
<div id="modalQrProgressBar" class="progress-bar" role="progressbar" style="width: 100%; background: linear-gradient(90deg, #4338CA, #B43E8E); transition: width 0.1s linear;"></div>
</div>
<div class="d-flex justify-content-between align-items-center mt-1 text-muted small" style="font-size: 0.72rem;">
<span class="d-flex align-items-center gap-1">
<span class="spinner-grow spinner-grow-sm text-success" style="width: 6px; height: 6px;" role="status"></span>
<span class="text-success fw-semibold">Anti-captura activo</span>
</span>
<span>Renueva en <strong id="modalQrCountdown" class="font-monospace text-primary">3s</strong></span>
</div>
</div>
<!-- Información de Validez y Seguridad -->
<div class="mt-2 mb-3">
<div class="d-inline-flex align-items-center gap-1 text-success small fw-semibold">
<i class="bi bi-shield-check"></i>
<span>Firma Criptográfica Verificada</span>
</div>
<div class="text-muted small mt-1" style="font-size: 0.73rem;" id="modalQrValidityText">
<i class="bi bi-arrow-repeat me-1"></i>Código dinámico con rotación cada 3 seg
</div>
</div>
<!-- Botón de Acción -->
<div class="d-grid">
<a href="/credential/my-card" class="btn btn-primary py-2 rounded-3 shadow-xs d-flex align-items-center justify-content-center">
<i class="bi bi-person-badge me-2"></i>Ver Carnet Completo
</a>
</div>
</div>
<!-- Footer Informativo -->
<div class="modal-footer bg-body-tertiary py-2 px-3 border-top d-flex justify-content-between align-items-center">
<small class="text-muted" style="font-size: 0.72rem;">
<i class="bi bi-info-circle me-1"></i>Escanea en recepción para ingresar
</small>
<button type="button" class="btn btn-sm btn-outline-secondary" data-bs-dismiss="modal">
Cerrar
</button>
</div>
</div>
</div>
</div>
<script>
(function() {
let qrModalInstance = null;
let timerId = null;
let renewalInterval = 3; // Segundos (default 3s configurable)
let elapsedMs = 0;
const tickIntervalMs = 100;
let isFetching = false;
function ensureQrLibrary(callback) {
if (typeof QRCode !== 'undefined') {
callback();
return;
}
var script = document.createElement('script');
script.src = 'https://cdn.jsdelivr.net/npm/qrcodejs@1.0.0/qrcode.min.js';
script.onload = callback;
script.onerror = function() {
var container = document.getElementById('globalQrModalContainer');
if (container) {
container.innerHTML = '<div class="alert alert-warning p-2 small">Error al cargar librería QR</div>';
}
};
document.head.appendChild(script);
}
function renderModalQR(token) {
var container = document.getElementById('globalQrModalContainer');
if (!container) return;
container.innerHTML = '';
try {
qrModalInstance = new QRCode(container, {
text: token,
width: 180,
height: 180,
colorDark: "#0F172A",
colorLight: "#FFFFFF",
correctLevel: QRCode.CorrectLevel.M
});
} catch (err) {
console.error('Error generando QR en modal:', err);
container.innerHTML = '<div class="alert alert-danger p-2 small">Error generando código QR</div>';
}
}
function updateProgressUi() {
var progressBar = document.getElementById('modalQrProgressBar');
var countdownEl = document.getElementById('modalQrCountdown');
var totalMs = renewalInterval * 1000;
var remainingMs = Math.max(0, totalMs - elapsedMs);
var pct = Math.max(0, Math.min(100, (remainingMs / totalMs) * 100));
if (progressBar) {
progressBar.style.width = pct + '%';
}
if (countdownEl) {
countdownEl.textContent = (remainingMs / 1000).toFixed(1) + 's';
}
}
function fetchAndRenderToken(showSpinner) {
if (isFetching) return;
isFetching = true;
var container = document.getElementById('globalQrModalContainer');
if (showSpinner && container && !qrModalInstance) {
container.innerHTML = '<div class="spinner-border text-primary spinner-border-sm" role="status"></div>';
}
fetch('/credential/token')
.then(function(res) { return res.json(); })
.then(function(data) {
if (data && data.qr_token) {
if (data.renewal_interval_seconds && data.renewal_interval_seconds > 0) {
renewalInterval = data.renewal_interval_seconds;
}
ensureQrLibrary(function() {
renderModalQR(data.qr_token);
});
var valText = document.getElementById('modalQrValidityText');
if (valText) {
valText.innerHTML = '<i class="bi bi-arrow-repeat me-1"></i>Código dinámico con rotación cada ' + renewalInterval + ' seg';
}
} else {
throw new Error('Token no disponible');
}
})
.catch(function(err) {
console.warn('Fallback a token local para QR modal:', err);
var fallback = "UNICABA:USR:{{ auth_user.id if auth_user else 0 }}:DNI:{{ auth_user.document_number if auth_user else 'N/A' }}";
ensureQrLibrary(function() {
renderModalQR(fallback);
});
})
.finally(function() {
isFetching = false;
elapsedMs = 0;
updateProgressUi();
});
}
function startAutoRefreshLoop() {
stopAutoRefreshLoop();
elapsedMs = 0;
updateProgressUi();
fetchAndRenderToken(false);
timerId = setInterval(function() {
elapsedMs += tickIntervalMs;
updateProgressUi();
if (elapsedMs >= renewalInterval * 1000) {
elapsedMs = 0;
fetchAndRenderToken(false);
}
}, tickIntervalMs);
}
function stopAutoRefreshLoop() {
if (timerId) {
clearInterval(timerId);
timerId = null;
}
}
window.refreshGlobalQrModal = function() {
var icon = document.getElementById('globalRefreshQrIcon');
if (icon) icon.classList.add('spin-animation');
elapsedMs = 0;
fetchAndRenderToken(false);
setTimeout(function() {
if (icon) icon.classList.remove('spin-animation');
}, 600);
};
document.addEventListener('DOMContentLoaded', function() {
var modalEl = document.getElementById('userQrCredentialModal');
if (modalEl) {
// Impedir que eventos de scroll de rueda o táctiles se propaguen a la página de fondo
modalEl.addEventListener('wheel', function(e) {
if (e.target === modalEl) {
e.preventDefault();
}
}, { passive: false });
modalEl.addEventListener('touchmove', function(e) {
if (e.target === modalEl) {
e.preventDefault();
}
}, { passive: false });
modalEl.addEventListener('show.bs.modal', function() {
// Bloqueo total de scroll en el elemento raíz (html) y cuerpo (body)
document.documentElement.classList.add('modal-open');
document.body.classList.add('modal-open');
document.documentElement.style.overflow = 'hidden';
document.body.style.overflow = 'hidden';
// Si el sidebar móvil estuviera desplegado, cerrarlo para evitar solapamientos
if (typeof window.closeMobileSidebar === 'function') {
window.closeMobileSidebar();
} else {
var sb = document.querySelector('.sidebar.mobile-open');
if (sb) sb.classList.remove('mobile-open');
var bd = document.getElementById('sidebarBackdrop');
if (bd) bd.classList.remove('show');
}
startAutoRefreshLoop();
});
modalEl.addEventListener('hidden.bs.modal', function() {
// Restaurar scroll de página
document.documentElement.classList.remove('modal-open');
document.body.classList.remove('modal-open');
document.documentElement.style.overflow = '';
document.body.style.overflow = '';
stopAutoRefreshLoop();
});
}
});
})();
</script>