From db254ca02f1dffca6af2fe8bd122a8dfc5dbe9d2 Mon Sep 17 00:00:00 2001 From: Lizandro Guarnizo <77708265+lizandrogd@users.noreply.github.com> Date: Wed, 8 Apr 2026 09:47:47 -0500 Subject: [PATCH] direccion --- api/lab/get_direcciones_paciente.php | 56 ++++++++++++++++++++++++++++ enfermero_portal.php | 37 ++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 api/lab/get_direcciones_paciente.php diff --git a/api/lab/get_direcciones_paciente.php b/api/lab/get_direcciones_paciente.php new file mode 100644 index 0000000..270aab4 --- /dev/null +++ b/api/lab/get_direcciones_paciente.php @@ -0,0 +1,56 @@ +fetch( + 'SELECT direccion, barrio, ciudad FROM lab_pacientes WHERE id = ?', + [$pacienteId] + ); + + // Direcciones distintas de domicilios anteriores (máx 10 más recientes) + $previas = $db->fetchAll( + "SELECT DISTINCT direccion, barrio, ciudad + FROM lab_domicilios + WHERE paciente_id = ? + AND direccion IS NOT NULL AND direccion != '' + ORDER BY id DESC + LIMIT 10", + [$pacienteId] + ); + + // Combinar: perfil primero, luego domicilios previos deduplicando + $resultado = []; + $vistas = []; + + $agregar = function(array $row) use (&$resultado, &$vistas) { + $dir = trim($row['direccion'] ?? ''); + if (!$dir) return; + $key = strtolower($dir); + if (isset($vistas[$key])) return; + $vistas[$key] = true; + $resultado[] = [ + 'direccion' => $dir, + 'barrio' => $row['barrio'] ?? '', + 'ciudad' => $row['ciudad'] ?? '', + ]; + }; + + if ($pac) $agregar($pac); + foreach ($previas as $p) $agregar($p); + + jsonOk(['data' => $resultado]); +} catch (Exception $e) { + jsonError($e->getMessage(), 500); +} diff --git a/enfermero_portal.php b/enfermero_portal.php index 36383c7..3bd6acf 100644 --- a/enfermero_portal.php +++ b/enfermero_portal.php @@ -1170,6 +1170,7 @@ const formVer = { +
@@ -1411,6 +1412,39 @@ const agendaNueva = { if (barrio) document.getElementById('na-barrio').value = barrio; if (ciudad) document.getElementById('na-ciudad').value = ciudad; if (direccion) document.getElementById('na-direccion').focus(); + // Cargar sugerencias de direcciones previas + this._cargarDirecciones(id); + }, + + async _cargarDirecciones(pacienteId) { + const wrap = document.getElementById('na-dir-sugerencias'); + wrap.classList.add('d-none'); + wrap.innerHTML = ''; + try { + const r = await fetch(`api/lab/get_direcciones_paciente.php?paciente_id=${pacienteId}`); + const d = await r.json(); + const dirs = d.data || []; + if (!dirs.length) return; + wrap.innerHTML = `
Direcciones anteriores:
` + + dirs.map((d, i) => { + const label = [d.direccion, d.barrio, d.ciudad].filter(Boolean).join(', '); + return ` + ${esc(label)} + `; + }).join(''); + this._dirs = dirs; + wrap.classList.remove('d-none'); + } catch(e) { /* silencioso */ } + }, + + _usarDireccion(i) { + const d = (this._dirs || [])[i]; + if (!d) return; + document.getElementById('na-direccion').value = d.direccion || ''; + document.getElementById('na-barrio').value = d.barrio || ''; + document.getElementById('na-ciudad').value = d.ciudad || ''; + document.getElementById('na-direccion').focus(); }, limpiarPaciente() { @@ -1419,6 +1453,9 @@ const agendaNueva = { document.getElementById('na-paciente-elegido').classList.add('d-none'); document.getElementById('na-paciente-buscar').disabled = false; document.getElementById('na-paciente-buscar').focus(); + document.getElementById('na-dir-sugerencias').classList.add('d-none'); + document.getElementById('na-dir-sugerencias').innerHTML = ''; + this._dirs = []; }, toggleNuevoPac() {