Cambios
This commit is contained in:
+173
-5
@@ -148,7 +148,7 @@ $domIdParam = (int)($_GET['id'] ?? 0);
|
||||
<div class="modal-body">
|
||||
<form id="form-domicilio">
|
||||
<input type="hidden" name="id" id="dom-id">
|
||||
<input type="hidden" name="orden_id" id="dom-orden-id" value="<?= $ordenIdParam ?>">
|
||||
<input type="hidden" name="orden_id_url" id="dom-orden-id" value="<?= $ordenIdParam ?>">
|
||||
|
||||
<!-- ── Paciente ── -->
|
||||
<h6 class="text-muted text-uppercase small fw-semibold border-bottom pb-1 mb-3">
|
||||
@@ -251,6 +251,44 @@ $domIdParam = (int)($_GET['id'] ?? 0);
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- ── Orden Médica ── -->
|
||||
<h6 class="text-muted text-uppercase small fw-semibold border-bottom pb-1 mb-3">
|
||||
<i class="fas fa-file-medical me-1"></i> Orden Médica Adjunta
|
||||
</h6>
|
||||
<div class="mb-3">
|
||||
<input type="hidden" name="orden_id" id="dom-orden-id-form">
|
||||
<!-- Preview archivo actual o pendiente -->
|
||||
<div id="dom-orden-preview" class="d-flex align-items-center gap-3 p-2 rounded border bg-light mb-2 d-none">
|
||||
<div id="dom-orden-thumb-wrap">
|
||||
<img id="dom-orden-img" src="" alt="Orden" style="max-height:80px;max-width:100px;object-fit:contain;border-radius:4px;display:none">
|
||||
<div id="dom-orden-icon" class="d-none">
|
||||
<i class="fas fa-file-alt fa-3x text-secondary"></i>
|
||||
</div>
|
||||
</div>
|
||||
<div class="flex-grow-1 overflow-hidden">
|
||||
<div id="dom-orden-fname" class="small fw-semibold text-truncate"></div>
|
||||
<div id="dom-orden-fsize" class="small text-muted"></div>
|
||||
</div>
|
||||
</div>
|
||||
<div id="dom-orden-empty" class="small text-muted mb-2">
|
||||
<i class="fas fa-paperclip me-1"></i> Sin orden adjunta
|
||||
</div>
|
||||
<div class="d-flex gap-2 flex-wrap align-items-center">
|
||||
<label class="btn btn-sm btn-outline-primary mb-0" for="dom-orden-input">
|
||||
<i class="fas fa-upload me-1"></i>
|
||||
<span id="dom-orden-btn-lbl">Adjuntar orden</span>
|
||||
</label>
|
||||
<button type="button" class="btn btn-sm btn-outline-danger d-none" id="dom-orden-quitar"
|
||||
onclick="domQuitarOrden()">
|
||||
<i class="fas fa-times me-1"></i> Quitar
|
||||
</button>
|
||||
</div>
|
||||
<input type="file" id="dom-orden-input" class="d-none"
|
||||
accept="image/jpeg,image/png,image/gif,image/webp,application/pdf,.pdf,.doc,.docx"
|
||||
onchange="domSeleccionarOrdenFile(this)">
|
||||
<small class="text-muted d-block mt-1">Imagen, PDF o DOC — máx. 10 MB</small>
|
||||
</div>
|
||||
|
||||
<!-- ── Notas ── -->
|
||||
<div class="mb-2">
|
||||
<label class="form-label">Notas internas</label>
|
||||
@@ -270,6 +308,93 @@ $domIdParam = (int)($_GET['id'] ?? 0);
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
|
||||
<script>
|
||||
const modal = new bootstrap.Modal('#modalDomicilio');
|
||||
|
||||
// ── Estado orden en el modal ───────────────────────────────────────────────
|
||||
let _domOrderFile = null; // File pendiente de subir
|
||||
let _domCurrentLocalFile = null; // local_file actual de la orden
|
||||
let _domRemoveOrder = false; // el usuario quiere desvincularse de la orden
|
||||
|
||||
function _domResetOrden() {
|
||||
_domOrderFile = null;
|
||||
_domCurrentLocalFile = null;
|
||||
_domRemoveOrder = false;
|
||||
document.getElementById('dom-orden-id-form').value = '';
|
||||
document.getElementById('dom-orden-input').value = '';
|
||||
_domRenderOrdenPreview();
|
||||
}
|
||||
|
||||
function _domRenderOrdenPreview() {
|
||||
const preview = document.getElementById('dom-orden-preview');
|
||||
const empty = document.getElementById('dom-orden-empty');
|
||||
const img = document.getElementById('dom-orden-img');
|
||||
const icon = document.getElementById('dom-orden-icon');
|
||||
const fname = document.getElementById('dom-orden-fname');
|
||||
const fsize = document.getElementById('dom-orden-fsize');
|
||||
const btnQuitar = document.getElementById('dom-orden-quitar');
|
||||
const btnLbl = document.getElementById('dom-orden-btn-lbl');
|
||||
|
||||
if (_domOrderFile) {
|
||||
// Archivo local seleccionado aún no subido
|
||||
const isImg = _domOrderFile.type.startsWith('image/');
|
||||
if (isImg) {
|
||||
img.src = URL.createObjectURL(_domOrderFile);
|
||||
img.style.display = '';
|
||||
icon.classList.add('d-none');
|
||||
} else {
|
||||
img.style.display = 'none';
|
||||
icon.classList.remove('d-none');
|
||||
}
|
||||
fname.textContent = _domOrderFile.name;
|
||||
fsize.textContent = (_domOrderFile.size / 1024).toFixed(1) + ' KB — pendiente de guardar';
|
||||
preview.classList.remove('d-none');
|
||||
empty.classList.add('d-none');
|
||||
btnQuitar.classList.remove('d-none');
|
||||
btnLbl.textContent = 'Cambiar archivo';
|
||||
} else if (_domCurrentLocalFile && !_domRemoveOrder) {
|
||||
// Archivo ya guardado en servidor
|
||||
const isImg = /\.(jpe?g|png|gif|webp)$/i.test(_domCurrentLocalFile);
|
||||
if (isImg) {
|
||||
img.src = 'uploads/media/' + encodeURIComponent(_domCurrentLocalFile);
|
||||
img.style.display = '';
|
||||
icon.classList.add('d-none');
|
||||
} else {
|
||||
img.style.display = 'none';
|
||||
icon.classList.remove('d-none');
|
||||
}
|
||||
fname.textContent = _domCurrentLocalFile;
|
||||
fsize.textContent = 'Archivo guardado';
|
||||
preview.classList.remove('d-none');
|
||||
empty.classList.add('d-none');
|
||||
btnQuitar.classList.remove('d-none');
|
||||
btnLbl.textContent = 'Cambiar archivo';
|
||||
} else {
|
||||
preview.classList.add('d-none');
|
||||
empty.classList.remove('d-none');
|
||||
btnQuitar.classList.add('d-none');
|
||||
btnLbl.textContent = 'Adjuntar orden';
|
||||
}
|
||||
}
|
||||
|
||||
function domSeleccionarOrdenFile(input) {
|
||||
const file = input.files[0];
|
||||
if (!file) return;
|
||||
if (file.size > 10 * 1024 * 1024) {
|
||||
alert('El archivo supera los 10 MB permitidos');
|
||||
input.value = '';
|
||||
return;
|
||||
}
|
||||
_domOrderFile = file;
|
||||
_domRemoveOrder = false;
|
||||
_domRenderOrdenPreview();
|
||||
}
|
||||
|
||||
function domQuitarOrden() {
|
||||
_domOrderFile = null;
|
||||
_domRemoveOrder = true;
|
||||
document.getElementById('dom-orden-id-form').value = '';
|
||||
document.getElementById('dom-orden-input').value = '';
|
||||
_domRenderOrdenPreview();
|
||||
}
|
||||
const COLOR_DOM = { programado:'secondary', confirmado:'info', en_camino:'primary', en_domicilio:'warning', completado:'success', cancelado:'danger', reprogramado:'dark' };
|
||||
let domSeleccionado = <?= $domIdParam ?: 'null' ?>;
|
||||
let paginaActual = 1;
|
||||
@@ -547,6 +672,7 @@ function abrirFormulario() {
|
||||
document.getElementById('dom-fecha').value = document.getElementById('filtro-fecha').value || '<?= date('Y-m-d') ?>';
|
||||
document.getElementById('dom-tipo-cliente').value = 'particular';
|
||||
toggleSeguro('particular');
|
||||
_domResetOrden();
|
||||
document.getElementById('modal-titulo').textContent = 'Nuevo Domicilio';
|
||||
modal.show();
|
||||
|
||||
@@ -585,6 +711,13 @@ async function editarDomicilio(id) {
|
||||
document.getElementById('dom-valor-cop').value = dom.valor_copago || '';
|
||||
document.getElementById('dom-copago-lab').value = dom.copago_laboratorio || '';
|
||||
document.getElementById('dom-notas').value = dom.notas_admin || '';
|
||||
|
||||
// Orden médica adjunta
|
||||
_domResetOrden();
|
||||
document.getElementById('dom-orden-id-form').value = dom.orden_id || '';
|
||||
_domCurrentLocalFile = dom.orden_local_file || null;
|
||||
_domRenderOrdenPreview();
|
||||
|
||||
modal.show();
|
||||
}
|
||||
|
||||
@@ -596,11 +729,45 @@ function toggleSeguro(val) {
|
||||
async function guardarDomicilio() {
|
||||
const form = document.getElementById('form-domicilio');
|
||||
if (!form.checkValidity()) { form.reportValidity(); return; }
|
||||
if (!document.getElementById('dom-paciente-id').value) {
|
||||
alert('Debes seleccionar un paciente'); return;
|
||||
}
|
||||
const pacienteId = document.getElementById('dom-paciente-id').value;
|
||||
if (!pacienteId) { alert('Debes seleccionar un paciente'); return; }
|
||||
|
||||
const datos = Object.fromEntries(new FormData(form).entries());
|
||||
if (!datos.id) delete datos.id;
|
||||
// Tomar orden_id del campo oculto real (no el parámetro URL)
|
||||
datos.orden_id = document.getElementById('dom-orden-id-form').value || null;
|
||||
delete datos.orden_id_url;
|
||||
|
||||
// ── Gestión del archivo de orden médica ──────────────────────────────
|
||||
if (_domRemoveOrder) {
|
||||
datos.orden_id = null;
|
||||
} else if (_domOrderFile) {
|
||||
// Subir el archivo primero
|
||||
const fd = new FormData();
|
||||
fd.append('file', _domOrderFile);
|
||||
const upR = await fetch('api/lab/upload_orden.php', { method:'POST', body: fd });
|
||||
const upD = await upR.json();
|
||||
if (!upD.success) {
|
||||
mostrarToast('Error subiendo el archivo: ' + (upD.error || 'desconocido'), 'danger');
|
||||
return;
|
||||
}
|
||||
// Actualizar o crear la orden médica
|
||||
const orId = document.getElementById('dom-orden-id-form').value;
|
||||
const orPayload = orId
|
||||
? { id: parseInt(orId), local_file: upD.local_file }
|
||||
: { paciente_id: parseInt(pacienteId), local_file: upD.local_file };
|
||||
const orR = await fetch('api/lab/save_orden.php', {
|
||||
method:'POST', headers:{'Content-Type':'application/json'},
|
||||
body: JSON.stringify(orPayload),
|
||||
});
|
||||
const orD = await orR.json();
|
||||
if (!orD.success) {
|
||||
mostrarToast('Error guardando la orden: ' + (orD.error || 'desconocido'), 'danger');
|
||||
return;
|
||||
}
|
||||
datos.orden_id = orD.id ?? orD.data?.id ?? (parseInt(orId) || null);
|
||||
}
|
||||
// ── Guardar domicilio ────────────────────────────────────────────────
|
||||
if (!datos.orden_id) delete datos.orden_id;
|
||||
|
||||
const r = await fetch('api/lab/save_domicilio.php', {
|
||||
@@ -611,7 +778,8 @@ async function guardarDomicilio() {
|
||||
if (d.success) {
|
||||
modal.hide();
|
||||
mostrarToast(d.message, 'success');
|
||||
const editadoId = datos.id ? parseInt(datos.id) : null;
|
||||
const editadoId = datos.id ? parseInt(datos.id) : (d.id || null);
|
||||
_domResetOrden();
|
||||
await cargarLista(paginaActual);
|
||||
if (editadoId) verDomicilio(editadoId);
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user