This commit is contained in:
Lizandro Guarnizo
2026-06-16 15:37:34 -05:00
parent 7f84c7f3ae
commit acbc1d0714
2 changed files with 91 additions and 90 deletions
+41 -58
View File
@@ -385,57 +385,6 @@ const API = '<?= BASE_URL ?>modules/turnero/api/';
const BASE_WA = '<?= BASE_URL ?>';
const LUGAR_NOMBRE = document.getElementById('lbl-lugar-titulo')?.textContent || '<?= addslashes($lugarNombre) ?>';
// ── Audio / anuncio ───────────────────────────────────────────
let _audioCtx = null;
let _sonidoListo = false;
function _desbloquearAudio() {
if (_sonidoListo) return;
try {
_audioCtx = new (window.AudioContext || window.webkitAudioContext)();
const g = _audioCtx.createGain(); g.gain.value = 0.001;
const o = _audioCtx.createOscillator();
o.connect(g); g.connect(_audioCtx.destination);
o.start(); o.stop(_audioCtx.currentTime + 0.05);
} catch (_) {}
_sonidoListo = true;
}
document.addEventListener('click', _desbloquearAudio, { once: false });
document.addEventListener('touchstart', _desbloquearAudio, { once: false });
function _playBeep() {
if (!_audioCtx) return;
try {
const osc = _audioCtx.createOscillator();
const gain = _audioCtx.createGain();
osc.type = 'sine';
osc.connect(gain); gain.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);
gain.gain.setValueAtTime(0.35, _audioCtx.currentTime);
gain.gain.exponentialRampToValueAtTime(0.0001, _audioCtx.currentTime + 0.65);
osc.start(_audioCtx.currentTime);
osc.stop(_audioCtx.currentTime + 0.65);
} catch (_) {}
}
function anunciarTurno(codigo) {
_desbloquearAudio();
_playBeep();
if ('speechSynthesis' in window) {
setTimeout(() => {
window.speechSynthesis.cancel();
const letras = codigo.split('').join(' ');
const utt = new SpeechSynthesisUtterance(`Turno ${letras}, pase a ${LUGAR_NOMBRE}`);
utt.lang = 'es-CO';
utt.rate = 0.85;
utt.pitch = 1.05;
utt.volume = 1;
window.speechSynthesis.speak(utt);
}, 700);
}
}
// ── Arranque ──────────────────────────────────────────────────
document.addEventListener('DOMContentLoaded', () => {
@@ -564,7 +513,6 @@ async function rellamarDesdeCard(turnoId) {
});
const json = await res.json();
if (!json.ok || !json.turno) return;
anunciarTurno(json.turno.codigo);
await abrirFicha(json.turno);
}
@@ -594,7 +542,6 @@ async function _llamar(extra) {
if (!json.ok) { mostrarError(json.error); return; }
if (!json.turno) { mostrarToast('Cola vacía — no hay turnos en espera.', 'warn', 3000); return; }
mostrarLlamando(json.turno.codigo);
anunciarTurno(json.turno.codigo);
await abrirFicha(json.turno);
cargarCola();
} catch (err) {
@@ -639,10 +586,10 @@ async function cargarFichaSolicitud(turnoId) {
const json = await res.json();
if (!json.ok) return;
const sol = json.solicitud;
const pac = json.paciente;
const exams = json.examenes || [];
const consts = json.consentimientos || [];
const sol = json.solicitud;
const pac = json.paciente;
const exams = json.examenes || [];
let consts = json.consentimientos || [];
// Datos del paciente
if (pac) {
@@ -669,6 +616,42 @@ async function cargarFichaSolicitud(turnoId) {
listaEx.innerHTML = '<span class="text-muted small">Sin exámenes registrados</span>';
}
// Auto-crear tokens para formularios del lugar que aún no tengan registro
if (lugarId) {
const resF = await fetch(`${API}get_lugar_formularios.php?lugar_id=${lugarId}`);
const jsonF = await resF.json();
const forms = (jsonF.ok && jsonF.formularios) ? jsonF.formularios : [];
const existIds = new Set(consts.map(c => parseInt(c.formulario_id)));
const faltantes = forms.filter(f => !existIds.has(parseInt(f.id)));
if (faltantes.length > 0) {
await Promise.all(faltantes.map(f =>
fetch(API + 'create_consent_token.php', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ turno_id: turnoId, formulario_id: f.id }),
}).catch(() => {})
));
// Recargar consents tras la creación
const res2 = await fetch(`${API}get_consentimientos.php?turno_id=${turnoId}`);
const json2 = await res2.json();
if (json2.ok) consts = json2.consentimientos || [];
// Si alguno no se pudo crear (sin paciente), mostrarlo igual como pendiente visual
const createdIds = new Set(consts.map(c => parseInt(c.formulario_id)));
faltantes.forEach(f => {
if (!createdIds.has(parseInt(f.id))) {
consts.push({
id: null, turno_id: turnoId,
formulario_id: f.id, formulario_nombre: f.nombre,
estado: 'pendiente', token: null,
});
}
});
}
}
// Consentimientos
renderConsentimientos(consts);
@@ -921,7 +904,7 @@ async function rellamarTurno() {
});
const json = await res.json();
if (!json.ok) mostrarError(json.error);
else if (json.turno) { mostrarLlamando(json.turno.codigo); anunciarTurno(json.turno.codigo); }
else if (json.turno) { mostrarLlamando(json.turno.codigo); }
} catch (err) {
mostrarError(err.message);
} finally {