Files
whatsapp/modules/turnero/views/display_global.php
T
2026-04-21 20:26:01 -05:00

717 lines
37 KiB
PHP

<?php
// Leer configuración visual del laboratorio
$_dispCfg = [];
try {
$__pdo = Database::getInstance()->getConnection();
$__rows = $__pdo->query(
"SELECT clave, valor FROM lab_config WHERE clave IN ('empresa_nombre','doc_logo_base64','doc_color','turnero_tv_video')"
)->fetchAll(PDO::FETCH_KEY_PAIR);
$_dispCfg = $__rows ?: [];
} catch (\Throwable $_) {}
$_labNombre = htmlspecialchars($_dispCfg['empresa_nombre'] ?? 'Sistema de Turnos');
$_labLogo = $_dispCfg['doc_logo_base64'] ?? '';
$_labColor = preg_match('/^#[0-9a-fA-F]{3,8}$/', $_dispCfg['doc_color'] ?? '') ? $_dispCfg['doc_color'] : '#1565c0';
$_tvVideo = $_dispCfg['turnero_tv_video'] ?? '';
// Validar que sea una URL del mismo origen (solo rutas relativas o mismo dominio)
if ($_tvVideo && !preg_match('#^https?://#', $_tvVideo)) { $_tvVideo = ''; }
?>
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pantalla Global de Turnos</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
<style>
*, *::before, *::after { box-sizing: border-box; }
html, body {
margin: 0; padding: 0;
height: 100%; width: 100%;
overflow: hidden;
background: #f0f4f8;
font-family: 'Segoe UI', system-ui, sans-serif;
color: #1e293b;
}
:root {
--brand: <?= $_labColor ?>;
--brand-dark: color-mix(in srgb, <?= $_labColor ?> 70%, #000);
--brand-light: color-mix(in srgb, <?= $_labColor ?> 15%, #fff);
}
/* ── Layout principal ──────────────────────────────── */
.pg-grid {
display: grid;
grid-template-rows: auto 1fr auto auto;
height: 100vh;
}
/* ── Header ─────────────────────────────────────────── */
.pg-header {
display: flex; align-items: center; justify-content: space-between;
padding: .6rem 2rem;
background: var(--brand);
gap: 1rem; flex-shrink: 0;
}
.header-brand {
display: flex; align-items: center; gap: .8rem;
flex: 1; min-width: 0;
}
.header-brand img.logo {
height: 44px; max-width: 120px; object-fit: contain;
background: rgba(255,255,255,.12); border-radius: 8px; padding: 4px 6px;
}
.header-brand .lab-nombre {
font-size: clamp(.9rem, 2vw, 1.3rem);
font-weight: 800; color: #fff;
white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.header-brand .lab-sub {
font-size: .7rem; color: rgba(255,255,255,.65);
font-weight: 500; text-transform: uppercase; letter-spacing: 1.5px;
display: block; margin-top: 1px;
}
.header-right {
display: flex; align-items: center; gap: .75rem; flex-shrink: 0;
}
.reloj {
font-size: clamp(1rem, 2.2vw, 1.45rem);
font-weight: 800; color: #fff;
font-variant-numeric: tabular-nums;
letter-spacing: 1px;
}
.btn-sonido {
background: rgba(255,255,255,.15); border: 1px solid rgba(255,255,255,.3);
color: #fff; border-radius: 8px; padding: 5px 12px;
font-size: .78rem; cursor: pointer;
}
/* ── Cuerpo: paneles de área ─────────────────────── */
.pg-body {
display: grid;
grid-template-columns: 1fr 1fr; /* recepción | lugares */
gap: 0;
overflow: hidden;
}
/* Panel genérico de área */
.area-panel {
display: flex; flex-direction: column;
overflow: hidden;
}
/* Encabezado del área */
.area-header {
padding: .55rem 1.2rem;
font-size: clamp(.7rem, 1.4vw, .9rem);
font-weight: 800; text-transform: uppercase; letter-spacing: 2px;
color: #fff; display: flex; align-items: center; gap: .5rem;
flex-shrink: 0;
}
.area-header.recepcion { background: #1e40af; }
.area-header.muestras { background: #15803d; }
/* Turno activo dentro del área */
.area-turno {
flex: 1; display: flex; flex-direction: column;
align-items: center; justify-content: center;
padding: 1.5rem 1rem;
position: relative; overflow: hidden;
background: #fff;
transition: background .5s;
}
.area-turno::before {
content: ''; position: absolute; top: 0; left: 0; right: 0; height: 6px;
background: var(--prio-color, var(--brand));
transition: background .5s;
}
.area-turno .lbl-destino {
font-size: clamp(.65rem, 1.3vw, .85rem);
color: #94a3b8; font-weight: 700;
text-transform: uppercase; letter-spacing: 3px;
margin-bottom: .15rem;
}
.area-turno .codigo-num {
font-size: clamp(4rem, 16vw, 12rem);
font-weight: 900; line-height: 1; letter-spacing: -3px;
color: var(--prio-color, var(--brand));
transition: color .4s;
}
.area-turno .pac-nombre {
font-size: clamp(.9rem, 2.2vw, 1.5rem);
font-weight: 700; color: #334155;
margin-top: .3rem; text-align: center;
}
.area-turno .prio-chip {
display: inline-flex; align-items: center; gap: .4rem;
padding: .35rem 1.1rem; border-radius: 30px;
font-size: clamp(.7rem, 1.3vw, .95rem); font-weight: 700;
margin-top: .75rem;
transition: background .4s, color .4s;
}
.area-turno .sin-turno {
font-size: clamp(1rem, 2.5vw, 1.8rem);
color: #cbd5e1; text-align: center;
}
/* Anillo */
@keyframes ping-ring {
0% { transform: scale(.8); opacity: .7; }
100% { transform: scale(2.6); opacity: 0; }
}
.ring {
position: absolute; inset: 0;
display: flex; align-items: center; justify-content: center;
pointer-events: none; z-index: 0;
}
.ring::before {
content: ''; width: 30vmin; height: 30vmin; border-radius: 50%;
background: transparent;
border: 5px solid var(--prio-color, var(--brand)); opacity: 0;
}
.ring.animar::before { animation: ping-ring .75s ease-out 1; }
.area-turno > *:not(.ring) { position: relative; z-index: 1; }
/* Divisor entre áreas */
.area-divider {
width: 3px; background: #e2e8f0; flex-shrink: 0;
}
/* Sub-paneles múltiples cuando hay varios lugares */
.lugares-grid {
display: grid;
grid-template-rows: repeat(auto-fill, 1fr);
height: 100%;
overflow: hidden;
}
.lugar-sub {
display: flex; flex-direction: column;
border-bottom: 2px solid #e2e8f0;
}
.lugar-sub:last-child { border-bottom: none; }
.lugar-sub .area-header { background: #166534; }
.lugar-sub .area-turno .codigo-num {
font-size: clamp(2.5rem, 10vw, 7rem);
}
/* ── Cola en espera ──────────────────────────────── */
.pg-cola {
background: #f8fafc;
border-top: 2px solid #e2e8f0;
padding: .4rem .8rem;
display: flex; align-items: center; gap: .6rem;
overflow-x: auto; flex-shrink: 0;
scrollbar-width: none;
}
.pg-cola::-webkit-scrollbar { display: none; }
.cola-lbl {
font-size: .7rem; font-weight: 800; text-transform: uppercase;
letter-spacing: 1.5px; color: var(--brand); white-space: nowrap;
flex-shrink: 0;
}
.cola-chip {
display: inline-flex; align-items: center; gap: .3rem;
padding: .25rem .7rem; border-radius: 99px;
font-size: .78rem; font-weight: 700;
background: #fff; border: 1.5px solid;
white-space: nowrap; flex-shrink: 0;
}
.cola-vacia-msg {
font-size: .78rem; color: #94a3b8;
}
/* ── Footer stats ─────────────────────────────────── */
.pg-footer {
display: flex; justify-content: center; align-items: center;
gap: 2rem; flex-wrap: wrap;
padding: .4rem 2rem;
background: var(--brand);
flex-shrink: 0;
}
.stat-item { text-align: center; }
.stat-item .val { font-size: 1.15rem; font-weight: 800; color: #fff; }
.stat-item .lbl { font-size: .6rem; color: rgba(255,255,255,.65); text-transform: uppercase; letter-spacing: 1px; }
.stat-sep { width: 1px; height: 26px; background: rgba(255,255,255,.2); }
</style>
<style>
/* ══ DARK THEME OVERRIDE ═════════════════════════════════════ */
:root {
--bg: #070e1d;
--glass: rgba(255,255,255,.04);
--border:rgba(255,255,255,.08);
--muted: #4a5568;
}
html, body {
background: var(--bg);
font-family: 'Inter', 'Segoe UI', system-ui, sans-serif;
color: #e2e8f0;
}
/* Layout: sin scroll */
.pg-wrap { display: grid; grid-template-rows: auto 1fr auto auto; height: 100vh; overflow: hidden; }
/* Header/footer: brand más oscuro */
.pg-header { background: color-mix(in srgb, var(--brand) 72%, #000) !important; }
.pg-footer { background: color-mix(in srgb, var(--brand) 42%, #000) !important; border-top: 1px solid color-mix(in srgb, var(--brand) 35%, transparent) !important; }
/* Botón pantalla completa */
.btn-fs { background: rgba(255,255,255,.10); border: 1px solid rgba(255,255,255,.2); color: rgba(255,255,255,.8); border-radius: 8px; padding: 4px 10px; font-size: .68rem; cursor: pointer; font-family: inherit; transition: background .2s; }
.btn-fs:hover { background: rgba(255,255,255,.22); }
/* Header modernize */
.pg-header::before {
content: ''; position: absolute; inset: 0; pointer-events: none;
background: linear-gradient(135deg,rgba(0,0,0,.32) 0%,rgba(0,0,0,0) 55%,rgba(255,255,255,.07) 100%);
}
.pg-header { position: relative; }
.pg-header::after {
content: ''; position: absolute; bottom: 0; left: 0; right: 0; height: 1px;
background: linear-gradient(90deg,transparent,rgba(255,255,255,.28),transparent);
}
.btn-son {
background: rgba(255,255,255,.12); border: 1px solid rgba(255,255,255,.2);
color: rgba(255,255,255,.82); border-radius: 8px; padding: 4px 11px;
font-size: .68rem; font-weight: 600; cursor: pointer; font-family: inherit;
transition: background .2s;
}
.btn-son.on { background: rgba(34,197,94,.2); border-color: rgba(34,197,94,.35); color: #86efac; }
.live-dot { width: 7px; height: 7px; border-radius: 50%; background: #22c55e; box-shadow: 0 0 8px #22c55e; animation: pdot 2s ease-in-out infinite; }
@keyframes pdot { 0%,100%{opacity:1} 50%{opacity:.35} }
/* Override body/pg-* for dark */
.pg-body { background: var(--bg); }
.v-split { background: linear-gradient(180deg,transparent,var(--border) 12%,var(--border) 88%,transparent); width: 1px; }
.area-col { background: var(--bg); display: flex; flex-direction: column; overflow: hidden; }
.col-hdr { display: flex; align-items: center; gap: .55rem; padding: .38rem 1.1rem; font-size: .62rem; font-weight: 800; text-transform: uppercase; letter-spacing: 2.5px; color: rgba(255,255,255,.88); flex-shrink: 0; }
.col-hdr.rec { background: linear-gradient(90deg, color-mix(in srgb,var(--brand) 38%,#000), color-mix(in srgb,var(--brand) 58%,#000)); border-bottom: 1px solid color-mix(in srgb,var(--brand) 45%,transparent); }
.col-hdr.mue { background: linear-gradient(90deg, color-mix(in srgb,var(--brand) 28%,#000), color-mix(in srgb,var(--brand) 44%,#000)); border-bottom: 1px solid color-mix(in srgb,var(--brand) 35%,transparent); opacity: .88; }
.slot-grid { display: grid; grid-auto-rows: 1fr; height: 100%; overflow: hidden; }
.slot { display: flex; flex-direction: column; border-bottom: 1px solid var(--border); position: relative; overflow: hidden; }
.slot:last-child { border-bottom: none; }
.slot-hdr { display: flex; align-items: center; gap: .4rem; padding: .27rem .9rem; font-size: .59rem; font-weight: 700; text-transform: uppercase; letter-spacing: 1.8px; flex-shrink: 0; z-index: 1; position: relative; }
.slot-hdr.rec { color: color-mix(in srgb,var(--brand) 88%,#fff); border-bottom: 1px solid color-mix(in srgb,var(--brand) 28%,transparent); background: color-mix(in srgb,var(--brand) 18%,transparent); }
.slot-hdr.mue { color: color-mix(in srgb,var(--brand) 78%,#fff); border-bottom: 1px solid color-mix(in srgb,var(--brand) 22%,transparent); background: color-mix(in srgb,var(--brand) 12%,transparent); }
.slot-hdr .hdot { width: 5px; height: 5px; border-radius: 50%; flex-shrink: 0; }
.slot-hdr.rec .hdot { background: var(--brand); box-shadow: 0 0 5px color-mix(in srgb,var(--brand) 80%,transparent); }
.slot-hdr.mue .hdot { background: color-mix(in srgb,var(--brand) 75%,#fff); box-shadow: 0 0 5px color-mix(in srgb,var(--brand) 65%,transparent); }
.slot-body { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; padding: .3rem .7rem; position: relative; overflow: hidden; }
.slot-body::after { content: ''; position: absolute; inset: 0; pointer-events: none; opacity: 0; transition: opacity .6s; background: radial-gradient(ellipse 80% 60% at 50% 60%, var(--pc,transparent) 0%, transparent 70%); }
.slot-body.lit::after { opacity: .07; }
.slot-body::before { content: ''; position: absolute; left: 0; top: 18%; bottom: 18%; width: 3px; border-radius: 0 3px 3px 0; background: var(--pc,transparent); opacity: 0; transition: opacity .5s,background .5s; }
.slot-body.lit::before { opacity: 1; }
.lbl-dest { font-size: clamp(.5rem,.95vw,.66rem); font-weight: 700; text-transform: uppercase; letter-spacing: 3px; color: var(--muted); margin-bottom: .1rem; z-index: 1; position: relative; transition: color .4s; }
.slot-body.lit .lbl-dest { color: var(--pc); opacity: .7; }
.big-code { font-size: clamp(1.8rem,5.5vw,5.5rem); font-weight: 900; line-height: 1; color: var(--muted); letter-spacing: -2px; font-variant-numeric: tabular-nums; z-index: 1; position: relative; transition: color .45s; }
.slot-body.lit .big-code { color: var(--pc); }
.pac-name { font-size: clamp(.55rem,1vw,.8rem); font-weight: 700; color: rgba(255,255,255,.6); margin-top: .15rem; text-align: center; min-height: 1em; z-index: 1; position: relative; }
.prio-badge { display: inline-flex; align-items: center; gap: .3rem; padding: .16rem .6rem; border-radius: 99px; font-size: clamp(.46rem,.72vw,.6rem); font-weight: 700; margin-top: .2rem; border: 1px solid rgba(255,255,255,.08); background: rgba(255,255,255,.04); color: var(--muted); transition: background .4s,color .4s,border-color .4s; z-index: 1; position: relative; }
.ring-pop-el { position: absolute; top: 50%; left: 50%; width: 10px; height: 10px; border-radius: 50%; transform: translate(-50%,-50%); pointer-events: none; z-index: 0; }
.ring-pop-el.do-pop { animation: r-pop .85s ease-out forwards; }
@keyframes r-pop { 0%{transform:translate(-50%,-50%) scale(1);background:var(--pc,var(--brand));opacity:.38} 100%{transform:translate(-50%,-50%) scale(50);background:var(--pc,var(--brand));opacity:0} }
/* Cola dark */
.pg-cola { background: rgba(255,255,255,.022); border-top: 1px solid var(--border); }
/* Video de fondo TV */
.tv-bg-video {
position: absolute; inset: 0; width: 100%; height: 100%;
object-fit: cover; opacity: .14; z-index: 0; pointer-events: none;
}
.pg-body { position: relative; }
.pg-body > *:not(.tv-bg-video) { position: relative; z-index: 1; }
.cola-lbl { color: var(--brand); }
.cola-sep { width: 1px; height: 12px; background: var(--border); flex-shrink: 0; }
.cola-chip { background: rgba(255,255,255,.04); font-size: .68rem; }
.cola-chip .cdot { width: 5px; height: 5px; border-radius: 50%; }
.cola-empty { font-size: .68rem; color: var(--muted); }
/* Footer dark */
.pg-footer { background: rgba(0,0,0,.45); border-top: 1px solid var(--border); }
.stat-item .val { font-size: clamp(.78rem,1.6vw,1rem); font-variant-numeric: tabular-nums; }
.stat-sep { background: var(--border); height: 19px; }
/* ══ ANNOUNCEMENT OVERLAY ════════════════════════════════════ */
#ann-overlay {
position: fixed; inset: 0; z-index: 9999;
display: flex; align-items: center; justify-content: center;
opacity: 0; pointer-events: none; transition: opacity .3s ease;
}
#ann-overlay.visible { opacity: 1; pointer-events: all; }
.ann-bg { position: absolute; inset: 0; background: rgba(4,8,20,.92); backdrop-filter: blur(18px); transition: background .4s; }
.ann-card {
position: relative; z-index: 1; width: min(600px,88vw);
background: rgba(255,255,255,.055); border: 1px solid rgba(255,255,255,.11);
border-radius: 28px; padding: 2.8rem 3.5rem 2.4rem; text-align: center;
box-shadow: 0 30px 90px rgba(0,0,0,.7), 0 0 70px var(--ac,#1565c0)44;
transform: scale(.88) translateY(14px);
transition: transform .4s cubic-bezier(.34,1.5,.64,1);
}
#ann-overlay.visible .ann-card { transform: scale(1) translateY(0); }
.ann-card::before { content: ''; position: absolute; left: 0; top: 12%; bottom: 12%; width: 4px; border-radius: 0 4px 4px 0; background: var(--ac,var(--brand)); box-shadow: 0 0 28px var(--ac,var(--brand)); }
.ann-ring { position: absolute; top: 50%; left: 50%; width: 10px; height: 10px; border-radius: 50%; transform: translate(-50%,-50%); pointer-events: none; }
.ann-ring.pop { animation: ann-pop 1.1s ease-out forwards; }
@keyframes ann-pop { 0%{transform:translate(-50%,-50%) scale(1);background:var(--ac,var(--brand));opacity:.4} 100%{transform:translate(-50%,-50%) scale(55);background:var(--ac,var(--brand));opacity:0} }
.ann-label { font-size: clamp(.68rem,1.5vw,.88rem); font-weight: 700; text-transform: uppercase; letter-spacing: 3.5px; color: rgba(255,255,255,.42); margin-bottom: .5rem; }
.ann-code { font-size: clamp(5rem,17vw,11rem); font-weight: 900; line-height: 1; letter-spacing: -4px; font-variant-numeric: tabular-nums; color: var(--ac,var(--brand)); text-shadow: 0 0 90px var(--ac,var(--brand))55; position: relative; z-index: 1; }
.ann-pac { font-size: clamp(.85rem,2.1vw,1.25rem); font-weight: 700; color: rgba(255,255,255,.72); margin-top: .55rem; min-height: 1.5em; }
.ann-chip { display: inline-flex; align-items: center; gap: .35rem; padding: .34rem 1.1rem; border-radius: 99px; font-size: clamp(.6rem,1.05vw,.8rem); font-weight: 700; margin-top: .7rem; border: 1px solid; }
.ann-progress { width: 100%; height: 3px; background: rgba(255,255,255,.07); border-radius: 99px; margin-top: 1.7rem; overflow: hidden; }
.ann-bar { height: 100%; width: 100%; border-radius: 99px; background: var(--ac,var(--brand)); box-shadow: 0 0 12px var(--ac,var(--brand)); }
</style>
</head>
<body>
<!-- ══ Announcement overlay ══════════════════════════════════════ -->
<div id="ann-overlay">
<div class="ann-bg" id="ann-bg"></div>
<div class="ann-card">
<div class="ann-ring" id="ann-ring"></div>
<div class="ann-label" id="ann-label">Pase a Recepción</div>
<div class="ann-code" id="ann-code">A01</div>
<div class="ann-pac" id="ann-pac"></div>
<div class="ann-chip" id="ann-chip"></div>
<div class="ann-progress"><div class="ann-bar" id="ann-bar"></div></div>
</div>
</div>
<!-- ══ Main layout ════════════════════════════════════════════════ -->
<div class="pg-wrap">
<header class="pg-header">
<div class="header-brand">
<?php if ($_labLogo): ?>
<img class="logo" src="<?= htmlspecialchars($_labLogo) ?>" alt="Logo">
<?php endif; ?>
<div>
<div class="lab-nombre"><?= $_labNombre ?></div>
<span class="lab-sub">Sistema de Turnos — Vista General</span>
</div>
</div>
<div class="header-right">
<div class="live-dot" title="En línea"></div>
<button class="btn-son" id="btn-sonido" onclick="event.stopPropagation();activarSonido()">
<i class="fas fa-volume-mute"></i> Sonido
</button>
<button class="btn-fs" id="btn-fs" onclick="toggleFullscreen()" title="Pantalla completa">
<i class="fas fa-expand" id="fs-icon"></i>
</button>
<div class="reloj" id="reloj">--:--:--</div>
</div>
</header>
<div class="pg-body">
<?php if ($_tvVideo): ?>
<video class="tv-bg-video" autoplay muted loop playsinline>
<source src="<?= htmlspecialchars($_tvVideo) ?>">
</video>
<?php endif; ?>
<div class="area-col">
<div class="col-hdr rec"><i class="fas fa-door-open"></i> Recepción</div>
<div class="slot-grid" id="rec-grid"></div>
</div>
<div class="v-split"></div>
<div class="area-col">
<div class="col-hdr mue"><i class="fas fa-vials"></i> Toma de Muestras</div>
<div class="slot-grid" id="lugares-grid"></div>
</div>
</div>
<div class="pg-cola" id="pg-cola">
<span class="cola-lbl"><i class="fas fa-list-ol" style="margin-right:.3rem"></i>EN ESPERA</span>
<div class="cola-sep"></div>
<span class="cola-empty" id="cola-vacia">Cola vacía</span>
</div>
<footer class="pg-footer">
<div class="stat-item"><div class="val" id="st-espera">—</div><div class="lbl">En espera</div></div>
<div class="stat-sep"></div>
<div class="stat-item"><div class="val" id="st-atencion">—</div><div class="lbl">En atención</div></div>
<div class="stat-sep"></div>
<div class="stat-item"><div class="val" id="st-finalizados">—</div><div class="lbl">Atendidos</div></div>
<div class="stat-sep"></div>
<div class="stat-item"><div class="val" id="st-total">—</div><div class="lbl">Total día</div></div>
<div class="stat-sep"></div>
<div class="stat-item"><div class="val" id="st-tiempo">—</div><div class="lbl">T. promedio</div></div>
</footer>
</div>
<script>
const BASE_API = '<?= BASE_URL ?>modules/turnero/api/';
// ── Reloj ──────────────────────────────────────────────────────
function tick() {
const d = new Date();
document.getElementById('reloj').textContent =
String(d.getHours()).padStart(2,'0') + ':' +
String(d.getMinutes()).padStart(2,'0') + ':' +
String(d.getSeconds()).padStart(2,'0');
}
tick(); setInterval(tick, 1000);
// ── Pantalla completa ─────────────────────────────────────────
function toggleFullscreen() {
if (!document.fullscreenElement) {
document.documentElement.requestFullscreen().catch(() => {});
} else {
document.exitFullscreen().catch(() => {});
}
}
document.addEventListener('fullscreenchange', () => {
const ico = document.getElementById('fs-icon');
if (ico) ico.className = document.fullscreenElement ? 'fas fa-compress' : 'fas fa-expand';
});
// ── Audio ──────────────────────────────────────────────────────
let audioCtx = null, sonidoActivo = false;
function activarSonido() {
try {
audioCtx = new (window.AudioContext || window.webkitAudioContext)();
const g = audioCtx.createGain(); g.gain.setValueAtTime(0.001, audioCtx.currentTime);
const o = audioCtx.createOscillator(); o.connect(g); g.connect(audioCtx.destination);
o.start(); o.stop(audioCtx.currentTime + 0.05);
} catch (_) {}
sonidoActivo = true;
const btn = document.getElementById('btn-sonido');
if (btn) {
btn.innerHTML = '<i class="fas fa-volume-up"></i> Sonido ON';
btn.classList.add('on');
setTimeout(playBeep, 100);
}
}
document.addEventListener('click', () => { if (!sonidoActivo) activarSonido(); }, { once: true });
document.addEventListener('touchstart', () => { if (!sonidoActivo) activarSonido(); }, { once: true });
function playBeep() {
if (!sonidoActivo || !audioCtx) return;
try {
const osc = audioCtx.createOscillator();
const g = audioCtx.createGain();
osc.type = 'sine';
osc.connect(g); g.connect(audioCtx.destination);
osc.frequency.setValueAtTime(880, audioCtx.currentTime);
osc.frequency.setValueAtTime(1100, audioCtx.currentTime + 0.1);
osc.frequency.setValueAtTime(880, audioCtx.currentTime + 0.2);
g.gain.setValueAtTime(0.35, audioCtx.currentTime);
g.gain.exponentialRampToValueAtTime(0.0001, audioCtx.currentTime + 0.65);
osc.start(audioCtx.currentTime);
osc.stop(audioCtx.currentTime + 0.65);
} catch (_) {}
}
function anunciarTurno(codigo, destino) {
playBeep();
if ('speechSynthesis' in window) {
setTimeout(() => {
window.speechSynthesis.cancel();
const letras = codigo.split('').join(' ');
const utt = new SpeechSynthesisUtterance(`Turno ${letras}, pase a ${destino}`);
utt.lang = 'es-CO'; utt.rate = 0.85; utt.pitch = 1.05; utt.volume = 1;
window.speechSynthesis.speak(utt);
}, 700);
}
}
// ── Estado previo ──────────────────────────────────────────────
// ── Announcement queue ────────────────────────────────────────
const annQueue = [];
let isAnnouncing = false;
function queueAnnouncement(item) {
if (annQueue.some(q => q._key === item._key)) return;
annQueue.push(item);
if (!isAnnouncing) processQueue();
}
function processQueue() {
if (annQueue.length === 0) { isAnnouncing = false; return; }
isAnnouncing = true;
showAnnouncement(annQueue.shift());
}
function showAnnouncement({ codigo, destino, paciente, prio_codigo, prio_nombre, color }) {
const ov = document.getElementById('ann-overlay');
const bg = document.getElementById('ann-bg');
const ring = document.getElementById('ann-ring');
const code = document.getElementById('ann-code');
const lbl = document.getElementById('ann-label');
const pac = document.getElementById('ann-pac');
const chip = document.getElementById('ann-chip');
const bar = document.getElementById('ann-bar');
const c = color || '#1565c0';
code.textContent = codigo;
lbl.textContent = 'Pase a ' + destino;
pac.textContent = paciente || '';
chip.innerHTML = '<i class="fas fa-ticket-alt"></i> ' + esc(prio_codigo||'') + ' — ' + esc(prio_nombre||'');
ov.style.setProperty('--ac', c);
bg.style.background = 'radial-gradient(ellipse at 50% 45%, ' + c + '2a 0%, rgba(4,8,20,.94) 65%)';
chip.style.color = c;
chip.style.borderColor = c + '55';
chip.style.background = c + '18';
ov.classList.add('visible');
ring.classList.remove('pop');
void ring.offsetWidth;
ring.classList.add('pop');
bar.style.transition = 'none';
bar.style.width = '100%';
requestAnimationFrame(() => requestAnimationFrame(() => {
bar.style.transition = 'width 1.65s linear';
bar.style.width = '0%';
}));
anunciarTurno(codigo, destino);
setTimeout(() => {
ov.classList.remove('visible');
setTimeout(processQueue, 370);
}, 1950);
}
// ── State ───────────────────────────────────────────────
const lastRec = {};
const lastLugar = {};
// ── Helpers ───────────────────────────────────────────────────
function esc(s) {
const d = document.createElement('div');
d.appendChild(document.createTextNode(String(s ?? '')));
return d.innerHTML;
}
function buildSlot(id, nombre, turno, tipo) {
const has = !!turno;
const c = has ? (turno.prioridad_color || '#1565c0') : null;
const pcSt = c ? '--pc:' + c + ';' : '';
const cls = has ? 'slot-body lit' : 'slot-body';
const hCls = tipo === 'rec' ? 'slot-hdr rec' : 'slot-hdr mue';
const icon = tipo === 'rec' ? 'fas fa-concierge-bell' : 'fas fa-flask';
const cod = has ? esc(turno.codigo) : '—';
const pac = has ? esc(turno.paciente_nombre || '') : '';
const chip = has
? "<i class='fas fa-ticket-alt'></i> " + esc(turno.prioridad_codigo) + ' — ' + esc(turno.prioridad_nombre)
: "<i class='fas fa-hourglass-half'></i> En espera";
const chipSt = has && c ? 'style="color:' + c + ';border-color:' + c + '44;background:' + c + '15"' : '';
return '<div class="slot" id="slot-' + tipo + '-' + id + '">'
+ '<div class="' + hCls + '"><span class="hdot"></span><i class="' + icon + '"></i> ' + esc(nombre) + '</div>'
+ '<div class="' + cls + '" id="body-' + tipo + '-' + id + '" style="' + pcSt + '">'
+ '<div class="ring-pop-el" id="ring-' + tipo + '-' + id + '"></div>'
+ '<div class="lbl-dest">' + esc(nombre) + '</div>'
+ '<div class="big-code" id="cod-' + tipo + '-' + id + '">' + cod + '</div>'
+ '<div class="pac-name" id="pac-' + tipo + '-' + id + '">' + pac + '</div>'
+ '<div class="prio-badge" id="chip-' + tipo + '-' + id + '" ' + chipSt + '>' + chip + '</div>'
+ '</div></div>';
}
function updateSlot(id, turno, tipo) {
const body = document.getElementById('body-' + tipo + '-' + id);
const cod = document.getElementById('cod-' + tipo + '-' + id);
const pac = document.getElementById('pac-' + tipo + '-' + id);
const chip = document.getElementById('chip-' + tipo + '-' + id);
if (!body) return false;
const c = turno ? (turno.prioridad_color || '#1565c0') : null;
if (turno) {
body.className = 'slot-body lit';
body.style.setProperty('--pc', c);
cod.textContent = turno.codigo;
pac.textContent = turno.paciente_nombre || '';
chip.innerHTML = "<i class='fas fa-ticket-alt'></i> " + esc(turno.prioridad_codigo) + ' — ' + esc(turno.prioridad_nombre);
chip.style.color = c; chip.style.borderColor = c + '44'; chip.style.background = c + '15';
} else {
body.className = 'slot-body';
body.style.removeProperty('--pc');
cod.textContent = '—';
pac.textContent = '';
chip.innerHTML = "<i class='fas fa-hourglass-half'></i> En espera";
chip.style.color = chip.style.borderColor = chip.style.background = '';
}
return true;
}
// ── Render ────────────────────────────────────────────────────
function renderSnapshot(snap) {
const recList = snap.activos_recepcion || [];
const mueList = snap.lugares || [];
const recGrid = document.getElementById('rec-grid');
const mueGrid = document.getElementById('lugares-grid');
// Recepción
const curRecIds = [...recGrid.querySelectorAll('.slot')].map(el => +el.id.replace('slot-rec-',''));
const newRecIds = recList.map(d => d.desk_id);
if (curRecIds.length !== newRecIds.length || newRecIds.some(id => !curRecIds.includes(id))) {
recGrid.innerHTML = recList.map(d => buildSlot(d.desk_id, d.desk_nombre, d.turno, 'rec')).join('');
} else {
recList.forEach(d => updateSlot(d.desk_id, d.turno, 'rec'));
}
recList.forEach(d => {
const t = d.turno;
if (!t) { lastRec[d.desk_id] = null; return; }
const clave = t.codigo + '|' + (t.llamado_at || '');
if (clave !== lastRec[d.desk_id]) {
queueAnnouncement({ _key: 'rec-' + d.desk_id + '-' + clave, codigo: t.codigo, destino: d.desk_nombre, paciente: t.paciente_nombre, prio_codigo: t.prioridad_codigo, prio_nombre: t.prioridad_nombre, color: t.prioridad_color });
const ring = document.getElementById('ring-rec-' + d.desk_id);
if (ring) { ring.classList.remove('do-pop'); void ring.offsetWidth; ring.classList.add('do-pop'); }
lastRec[d.desk_id] = clave;
}
});
// Muestras
const curMueIds = [...mueGrid.querySelectorAll('.slot')].map(el => +el.id.replace('slot-mue-',''));
const newMueIds = mueList.map(l => l.lugar_id);
if (curMueIds.length !== newMueIds.length || newMueIds.some(id => !curMueIds.includes(id))) {
mueGrid.innerHTML = mueList.map(l => buildSlot(l.lugar_id, l.lugar_nombre, l.turno, 'mue')).join('');
} else {
mueList.forEach(l => updateSlot(l.lugar_id, l.turno, 'mue'));
}
mueList.forEach(l => {
const t = l.turno;
if (!t) { lastLugar[l.lugar_id] = null; return; }
const clave = t.codigo + '|' + (t.llamado_at || '');
if (clave !== lastLugar[l.lugar_id]) {
queueAnnouncement({ _key: 'mue-' + l.lugar_id + '-' + clave, codigo: t.codigo, destino: l.lugar_nombre, paciente: t.paciente_nombre, prio_codigo: t.prioridad_codigo, prio_nombre: t.prioridad_nombre, color: t.prioridad_color });
const ring = document.getElementById('ring-mue-' + l.lugar_id);
if (ring) { ring.classList.remove('do-pop'); void ring.offsetWidth; ring.classList.add('do-pop'); }
lastLugar[l.lugar_id] = clave;
}
});
// Cola
const cola = snap.cola || [];
const pgCola = document.getElementById('pg-cola');
pgCola.querySelectorAll('.cola-chip').forEach(c => c.remove());
const vaciaMg = document.getElementById('cola-vacia');
if (cola.length === 0) {
vaciaMg.style.display = '';
} else {
vaciaMg.style.display = 'none';
cola.forEach(t => {
const chip = document.createElement('span');
const c = t.prioridad_color || '#1565c0';
chip.className = 'cola-chip';
chip.style.color = c; chip.style.borderColor = c + '55'; chip.style.background = c + '12';
chip.innerHTML = '<span class="cdot" style="background:' + c + ';box-shadow:0 0 5px ' + c + '88"></span>' + esc(t.codigo);
pgCola.appendChild(chip);
});
}
// Stats
const s = snap.stats || {};
document.getElementById('st-espera').textContent = s.en_espera || 0;
document.getElementById('st-atencion').textContent = s.en_atencion || 0;
document.getElementById('st-finalizados').textContent = s.finalizados || 0;
document.getElementById('st-total').textContent = s.total || 0;
document.getElementById('st-tiempo').textContent = s.tiempo_promedio_atencion
? s.tiempo_promedio_atencion.substring(0, 5) : '—';
}
// ── Polling ───────────────────────────────────────────────────
async function cargarSnapshot() {
try {
const res = await fetch(BASE_API + 'get_display_global.php', { cache: 'no-store' });
const json = await res.json();
if (json.ok) renderSnapshot(json);
} catch(_) {}
}
cargarSnapshot();
setInterval(cargarSnapshot, 4000);
</script>
</body>
</html>