feat(turnero): múltiples consentimientos por tipo de examen

- save_ex_tipo.php: GET devuelve form_ids[], POST acepta form_ids[] y
  hace DELETE+INSERT por cada uno (la tabla ya soportaba muchos a muchos)
- configuracion.php: reemplaza select único por checkboxes en formulario
  Agregar y modal Editar; JS lee los checks y envía form_ids[]

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Lizandro Guarnizo
2026-07-08 08:10:57 -05:00
co-authored by Claude Sonnet 4.6
parent b6179574c3
commit b2bc0f950a
2 changed files with 62 additions and 45 deletions
+44 -24
View File
@@ -675,13 +675,21 @@ $tab = $_GET['tab'] ?? 'lugares';
<input type="text" id="ex-categoria" class="form-control form-control-sm" placeholder="Hematología">
</div>
<div class="col-md-3">
<label class="form-label small fw-semibold">Consentimiento</label>
<select id="ex-form" class="form-select form-select-sm">
<option value="">— Sin consentimiento —</option>
<label class="form-label small fw-semibold">Consentimientos</label>
<div id="ex-forms-wrap" class="border rounded p-2" style="max-height:120px;overflow-y:auto;background:#fffbeb">
<?php foreach ($formulariosCons as $fc): ?>
<option value="<?= $fc['id'] ?>"><?= htmlspecialchars($fc['nombre']) ?></option>
<div class="form-check mb-1">
<input class="form-check-input ex-form-chk" type="checkbox"
value="<?= $fc['id'] ?>" id="ex-fc-<?= $fc['id'] ?>">
<label class="form-check-label small" for="ex-fc-<?= $fc['id'] ?>">
<?= htmlspecialchars($fc['nombre']) ?>
</label>
</div>
<?php endforeach; ?>
</select>
<?php if (empty($formulariosCons)): ?>
<span class="text-muted small">Sin formularios activos</span>
<?php endif; ?>
</div>
</div>
<div class="col-12 text-end">
<button class="btn btn-primary btn-sm" onclick="guardarExamen()">
@@ -738,13 +746,21 @@ $tab = $_GET['tab'] ?? 'lugares';
<input type="text" id="edit-ex-categoria" class="form-control form-control-sm">
</div>
<div class="col-md-6">
<label class="form-label small fw-semibold">Consentimiento</label>
<select id="edit-ex-form" class="form-select form-select-sm">
<option value="">— Sin consentimiento —</option>
<label class="form-label small fw-semibold">Consentimientos</label>
<div id="edit-ex-forms-wrap" class="border rounded p-2" style="max-height:120px;overflow-y:auto;background:#fffbeb">
<?php foreach ($formulariosCons as $fc): ?>
<option value="<?= $fc['id'] ?>"><?= htmlspecialchars($fc['nombre']) ?></option>
<div class="form-check mb-1">
<input class="form-check-input edit-ex-form-chk" type="checkbox"
value="<?= $fc['id'] ?>" id="edit-ex-fc-<?= $fc['id'] ?>">
<label class="form-check-label small" for="edit-ex-fc-<?= $fc['id'] ?>">
<?= htmlspecialchars($fc['nombre']) ?>
</label>
</div>
<?php endforeach; ?>
</select>
<?php if (empty($formulariosCons)): ?>
<span class="text-muted small">Sin formularios activos</span>
<?php endif; ?>
</div>
</div>
<div class="col-12">
<div class="form-check">
@@ -1340,15 +1356,17 @@ async function eliminarDispositivo(id, nombre) {
// TAB 2: EXÁMENES
// ─────────────────────────────────────────────────────────────
async function guardarExamen(id = null) {
const codigo = (id ? document.getElementById('edit-ex-codigo') : document.getElementById('ex-codigo'))?.value.trim().toUpperCase();
const nombre = (id ? document.getElementById('edit-ex-nombre') : document.getElementById('ex-nombre'))?.value.trim();
const categoria = (id ? document.getElementById('edit-ex-categoria'): document.getElementById('ex-categoria'))?.value.trim();
const formId = parseInt((id ? document.getElementById('edit-ex-form') : document.getElementById('ex-form'))?.value) || null;
const prefix = id ? 'edit-ex' : 'ex';
const codigo = document.getElementById(prefix + '-codigo')?.value.trim().toUpperCase();
const nombre = document.getElementById(prefix + '-nombre')?.value.trim();
const categoria = document.getElementById(prefix + '-categoria')?.value.trim();
const activo = id ? (document.getElementById('edit-ex-activo')?.checked ? 1 : 0) : 1;
const chkClass = id ? 'edit-ex-form-chk' : 'ex-form-chk';
const form_ids = [...document.querySelectorAll(`.${chkClass}:checked`)].map(c => parseInt(c.value));
if (!codigo || !nombre) { toast('Código y nombre son requeridos', 'error'); return; }
const body = { codigo, nombre, categoria, formulario_id: formId, activo };
const body = { codigo, nombre, categoria, form_ids, activo };
if (id) body.id = id;
try {
@@ -1359,25 +1377,27 @@ async function guardarExamen(id = null) {
if (!json.ok) { toast(json.error || 'Error', 'error'); return; }
toast(id ? 'Examen actualizado' : 'Examen creado');
if (id) bootstrap.Modal.getInstance(document.getElementById('modalEditarExamen'))?.hide();
// Limpiar checkboxes del formulario de agregar
if (!id) document.querySelectorAll('.ex-form-chk').forEach(c => c.checked = false);
setTimeout(() => location.reload(), 600);
} catch (e) { toast('Error de conexión', 'error'); }
}
function editarExamen(id) {
const row = document.querySelector(`[data-exam-id="${id}"]`);
const tags = row ? row.querySelectorAll('.consent-tag') : [];
// Leer datos de los atributos del servidor (via fetch de datos ya cargados)
fetch(API + 'save_ex_tipo.php?id=' + id)
.then(r => r.json())
.then(json => {
if (!json.ok) return;
const d = json.data;
document.getElementById('edit-ex-id').value = d.id;
document.getElementById('edit-ex-codigo').value = d.codigo;
document.getElementById('edit-ex-nombre').value = d.nombre;
document.getElementById('edit-ex-categoria').value= d.categoria || '';
document.getElementById('edit-ex-form').value = d.formulario_id || '';
document.getElementById('edit-ex-activo').checked = !!d.activo;
document.getElementById('edit-ex-id').value = d.id;
document.getElementById('edit-ex-codigo').value = d.codigo;
document.getElementById('edit-ex-nombre').value = d.nombre;
document.getElementById('edit-ex-categoria').value = d.categoria || '';
document.getElementById('edit-ex-activo').checked = !!d.activo;
// Marcar consentimientos vinculados
document.querySelectorAll('.edit-ex-form-chk').forEach(chk => {
chk.checked = (d.form_ids || []).includes(parseInt(chk.value));
});
new bootstrap.Modal(document.getElementById('modalEditarExamen')).show();
})
.catch(() => toast('Error cargando datos', 'error'));