direccion
This commit is contained in:
@@ -0,0 +1,56 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* GET /api/lab/get_direcciones_paciente.php?paciente_id=X
|
||||||
|
* Devuelve las direcciones únicas usadas en domicilios anteriores de un paciente.
|
||||||
|
* También incluye la dirección registrada en el perfil del paciente.
|
||||||
|
*/
|
||||||
|
require_once __DIR__ . '/_helpers.php';
|
||||||
|
requireMethod('GET');
|
||||||
|
|
||||||
|
$pacienteId = (int)($_GET['paciente_id'] ?? 0);
|
||||||
|
if (!$pacienteId) jsonError('paciente_id requerido');
|
||||||
|
|
||||||
|
try {
|
||||||
|
$db = Database::getInstance();
|
||||||
|
|
||||||
|
// Dirección del perfil del paciente
|
||||||
|
$pac = $db->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);
|
||||||
|
}
|
||||||
@@ -1170,6 +1170,7 @@ const formVer = {
|
|||||||
<label class="form-label small fw-semibold">Dirección <span class="text-danger">*</span></label>
|
<label class="form-label small fw-semibold">Dirección <span class="text-danger">*</span></label>
|
||||||
<input type="text" id="na-direccion" class="form-control form-control-sm"
|
<input type="text" id="na-direccion" class="form-control form-control-sm"
|
||||||
placeholder="Calle 123 # 45-67">
|
placeholder="Calle 123 # 45-67">
|
||||||
|
<div id="na-dir-sugerencias" class="d-none mt-1"></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="row g-2 mb-3">
|
<div class="row g-2 mb-3">
|
||||||
<div class="col-6">
|
<div class="col-6">
|
||||||
@@ -1411,6 +1412,39 @@ const agendaNueva = {
|
|||||||
if (barrio) document.getElementById('na-barrio').value = barrio;
|
if (barrio) document.getElementById('na-barrio').value = barrio;
|
||||||
if (ciudad) document.getElementById('na-ciudad').value = ciudad;
|
if (ciudad) document.getElementById('na-ciudad').value = ciudad;
|
||||||
if (direccion) document.getElementById('na-direccion').focus();
|
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 = `<div class="small text-muted mb-1"><i class="fas fa-clock-rotate-left me-1"></i>Direcciones anteriores:</div>`
|
||||||
|
+ dirs.map((d, i) => {
|
||||||
|
const label = [d.direccion, d.barrio, d.ciudad].filter(Boolean).join(', ');
|
||||||
|
return `<span class="badge bg-light text-dark border me-1 mb-1" style="cursor:pointer;font-size:.74rem;font-weight:400"
|
||||||
|
onclick="agendaNueva._usarDireccion(${i})">
|
||||||
|
<i class="fas fa-map-marker-alt me-1 text-primary"></i>${esc(label)}
|
||||||
|
</span>`;
|
||||||
|
}).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() {
|
limpiarPaciente() {
|
||||||
@@ -1419,6 +1453,9 @@ const agendaNueva = {
|
|||||||
document.getElementById('na-paciente-elegido').classList.add('d-none');
|
document.getElementById('na-paciente-elegido').classList.add('d-none');
|
||||||
document.getElementById('na-paciente-buscar').disabled = false;
|
document.getElementById('na-paciente-buscar').disabled = false;
|
||||||
document.getElementById('na-paciente-buscar').focus();
|
document.getElementById('na-paciente-buscar').focus();
|
||||||
|
document.getElementById('na-dir-sugerencias').classList.add('d-none');
|
||||||
|
document.getElementById('na-dir-sugerencias').innerHTML = '';
|
||||||
|
this._dirs = [];
|
||||||
},
|
},
|
||||||
|
|
||||||
toggleNuevoPac() {
|
toggleNuevoPac() {
|
||||||
|
|||||||
Reference in New Issue
Block a user