up
This commit is contained in:
@@ -389,7 +389,7 @@ require_once __DIR__ . '/../../../shared/components/sidebar.php';
|
||||
</div>
|
||||
|
||||
<!-- ── Sección 5: Consentimientos ── -->
|
||||
<div class="ficha-section" id="sec-consentimientos" style="display:none!important">
|
||||
<div class="ficha-section" id="sec-consentimientos" style="display:none">
|
||||
<div class="d-flex align-items-center justify-content-between mb-2">
|
||||
<h6 class="mb-0"><i class="fas fa-file-signature me-1"></i>Consentimientos informados</h6>
|
||||
<button class="btn btn-sm btn-outline-primary py-0 px-2"
|
||||
@@ -448,11 +448,63 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
pollingColaId = setInterval(cargarCola, 3000);
|
||||
document.getElementById('inp-buscar-pac')
|
||||
.addEventListener('keydown', e => { if (e.key === 'Enter') buscarPaciente(); });
|
||||
document.getElementById('sel-lugar')
|
||||
.addEventListener('change', onLugarChange);
|
||||
// Refrescar consentimientos al volver a la pestaña (firma en otra pestaña)
|
||||
document.addEventListener('visibilitychange', () => {
|
||||
if (!document.hidden && turnoActivo) refrescarConsentimientos(turnoActivo.id);
|
||||
});
|
||||
});
|
||||
// ── Formularios del lugar ────────────────────────────────────
|
||||
async function onLugarChange() {
|
||||
const sec = document.getElementById('sec-consentimientos');
|
||||
const lista = document.getElementById('lista-consentimientos');
|
||||
const lugarId = parseInt(document.getElementById('sel-lugar').value);
|
||||
if (!lugarId || !pacienteActivo) { sec.style.display = 'none'; return; }
|
||||
|
||||
try {
|
||||
const res = await fetch(`${API}get_lugar_formularios.php?lugar_id=${lugarId}`);
|
||||
const json = await res.json();
|
||||
if (!json.ok || !json.data?.formularios?.length) { sec.style.display = 'none'; return; }
|
||||
|
||||
lista.innerHTML = json.data.formularios.map(f => {
|
||||
const nom = escHtml(f.nombre);
|
||||
return `<div class="consent-item pendiente">
|
||||
<i class="fas fa-file-signature"></i>
|
||||
<span class="c-nom">${nom}</span>
|
||||
<div class="consent-acciones">
|
||||
<button class="btn btn-outline-primary"
|
||||
onclick="abrirFormularioLugar(${f.id}, '${nom.replace(/'/g, "\\'")}')"
|
||||
title="Abrir formulario en nueva pestaña">
|
||||
<i class="fas fa-external-link-alt"></i> Firmar
|
||||
</button>
|
||||
</div>
|
||||
</div>`;
|
||||
}).join('');
|
||||
sec.style.display = '';
|
||||
} catch (_) {
|
||||
sec.style.display = 'none';
|
||||
}
|
||||
}
|
||||
async function abrirFormularioLugar(formularioId, nombre) {
|
||||
if (!turnoActivo) return;
|
||||
// Si ya hay solicitud guardada, usa el consent token
|
||||
try {
|
||||
const res = await fetch(API + 'create_consent_token.php', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ turno_id: turnoActivo.id, formulario_id: formularioId }),
|
||||
});
|
||||
const json = await res.json();
|
||||
if (json.ok && json.data?.url) {
|
||||
window.open(json.data.url, '_blank');
|
||||
} else {
|
||||
window.open('<?= BASE_URL ?>lab_formularios.php', '_blank');
|
||||
}
|
||||
} catch (_) {
|
||||
window.open('<?= BASE_URL ?>lab_formularios.php', '_blank');
|
||||
}
|
||||
}
|
||||
|
||||
// ── Cola ──────────────────────────────────────────────────────
|
||||
async function cargarCola() {
|
||||
@@ -769,11 +821,11 @@ function renderConsentimientos(lista) {
|
||||
const ya = ['firmado','rechazado'].includes(c.estado);
|
||||
const token = escHtml(c.token || '');
|
||||
const nom = escHtml(c.formulario_nombre || 'Consentimiento');
|
||||
const nomJs = JSON.stringify(c.formulario_nombre || 'Consentimiento');
|
||||
const fId = c.formulario_id;
|
||||
|
||||
// Botón de firma (solo si no está firmado/rechazado)
|
||||
const btnFirmar = (!ya && c.token)
|
||||
? `<button class="btn btn-outline-primary" onclick="abrirFirmaPresencialRec('${token}')"
|
||||
const btnFirmar = (!ya)
|
||||
? `<button class="btn btn-outline-primary" onclick="firmarConsentimiento(${fId}, '${nom.replace(/'/g, "\\'")}')"
|
||||
title="Abrir firma en nueva pestaña">
|
||||
<i class="fas fa-external-link-alt"></i> Firmar
|
||||
</button>` : '';
|
||||
@@ -798,11 +850,23 @@ function renderConsentimientos(lista) {
|
||||
}
|
||||
|
||||
// ── Firmar en nueva pestaña ──────────────────────────────────
|
||||
function abrirFirmaPresencialRec(token) {
|
||||
window.open(
|
||||
BASE_WA + 'ver_formulario_enviado.php?token=' + encodeURIComponent(token) + '&firmar=1',
|
||||
'_blank'
|
||||
);
|
||||
async function firmarConsentimiento(formularioId, nombre) {
|
||||
if (!turnoActivo) { mostrarError('No hay turno activo.'); return; }
|
||||
try {
|
||||
const res = await fetch(API + 'create_consent_token.php', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ turno_id: turnoActivo.id, formulario_id: formularioId }),
|
||||
});
|
||||
const json = await res.json();
|
||||
if (json.ok && json.data?.url) {
|
||||
window.open(json.data.url, '_blank');
|
||||
} else {
|
||||
mostrarError(json.error || 'No se pudo crear el token de firma');
|
||||
}
|
||||
} catch (err) {
|
||||
mostrarError('Error al abrir firma: ' + err.message);
|
||||
}
|
||||
}
|
||||
function verFirmado(token) {
|
||||
window.open(BASE_WA + 'ver_formulario_enviado.php?token=' + encodeURIComponent(token), '_blank');
|
||||
|
||||
Reference in New Issue
Block a user