-
-
-
![]()
-
-
-
-
Orden médica adjunta
-
-
-
-
-
-
+
+ Órdenes médicas (opcional)
+
-
-
+
+
@@ -6869,8 +6862,10 @@ const labDomicilio = (() => {
// Estado interno
let _msgId = null;
let _localFile = null;
- let _newOrderFile = null; // nuevo archivo de orden cargado desde el modal
- let _convId = null;
+ let _newOrderFiles = []; // archivos nuevos seleccionados por el usuario
+ let _newOrderDataUrls = []; // data-URLs para previsualizar (índice paralelo)
+ let _localFiles = []; // filenames ya en servidor (del mensaje WA original)
+ let _convId = null;
let _pacienteId = null;
let _isNewPaciente = false; // true = paciente nuevo (no existe en BD aún)
let _pacNombre = '';
@@ -6962,9 +6957,11 @@ const labDomicilio = (() => {
/* Abre el modal desde el botón del mensaje imagen */
abrir(msgId, localFile, convId) {
- _msgId = msgId || null;
- _localFile = localFile|| null;
- _convId = convId ? parseInt(convId) : null;
+ _msgId = msgId || null;
+ _localFiles = localFile ? [localFile] : [];
+ _newOrderFiles = [];
+ _newOrderDataUrls = [];
+ _convId = convId ? parseInt(convId) : null;
// Guardar también el userId actual para usarContacto cuando convId no esté disponible
if (!_convId && typeof chatApp !== 'undefined' && chatApp.currentUserId) {
_convId = '__user:' + chatApp.currentUserId;
@@ -6984,10 +6981,12 @@ const labDomicilio = (() => {
/* Reset completo (nombre paciente + todos los campos) */
_resetFull() {
- _pacienteId = null;
- _pacNombre = '';
- _histDirs = [];
- _newOrderFile = null;
+ _pacienteId = null;
+ _pacNombre = '';
+ _histDirs = [];
+ _newOrderFiles = [];
+ _newOrderDataUrls = [];
+ _localFiles = [];
$('labdom-pac-busq').value = '';
$('labdom-pac-list').style.display = 'none';
$('labdom-pac-buscar').classList.remove('d-none');
@@ -7009,70 +7008,82 @@ const labDomicilio = (() => {
labDomicilio._resetDomicilio();
},
- /* Actualiza la previa de orden médica en el modal (imagen o icono de archivo) */
+ /* Renderiza la lista de archivos de la orden médica */
_updateOrdenPreview() {
- const prevDiv = document.getElementById('labdom-img-prev');
- const addBtn = document.getElementById('labdom-btn-add-orden');
- const imgEl = document.getElementById('labdom-img-el');
- const iconEl = document.getElementById('labdom-file-icon');
- const nameEl = document.getElementById('labdom-file-name');
- if (!prevDiv) return;
- if (_localFile || _newOrderFile) {
- prevDiv.style.display = '';
- if (addBtn) addBtn.style.display = 'none';
- const fileName = _newOrderFile ? _newOrderFile.name : (_localFile || '');
- if (nameEl) nameEl.textContent = fileName;
- const isImage = /\.(jpg|jpeg|png|gif|webp|bmp)$/i.test(fileName);
- if (isImage) {
- if (imgEl) {
- imgEl.style.display = '';
- if (_newOrderFile) {
- const reader = new FileReader();
- reader.onload = ev => { imgEl.src = ev.target.result; };
- reader.readAsDataURL(_newOrderFile);
- } else {
- imgEl.src = `uploads/media/${_localFile}`;
- }
- }
- if (iconEl) iconEl.style.display = 'none';
- } else {
- if (imgEl) imgEl.style.display = 'none';
- if (iconEl) {
- const ext = (fileName || '').split('.').pop().toLowerCase();
- const iconMap = { pdf: 'fas fa-file-pdf text-danger', doc: 'fas fa-file-word text-primary', docx: 'fas fa-file-word text-primary', mp4: 'fas fa-file-video text-warning', mkv: 'fas fa-file-video text-warning', mp3: 'fas fa-file-audio text-success', ogg: 'fas fa-file-audio text-success', webm: 'fas fa-file-video text-warning' };
- iconEl.innerHTML = `
`;
- iconEl.style.display = 'flex';
- }
- }
- } else {
- prevDiv.style.display = 'none';
- if (addBtn) addBtn.style.display = '';
- }
+ const list = $('labdom-ordenes-list');
+ if (!list) return;
+
+ const iconCls = ext => {
+ const m = { pdf:'fas fa-file-pdf text-danger', doc:'fas fa-file-word text-primary', docx:'fas fa-file-word text-primary', mp4:'fas fa-file-video text-warning', mkv:'fas fa-file-video text-warning', mp3:'fas fa-file-audio text-success', ogg:'fas fa-file-audio text-success', webm:'fas fa-file-video text-warning' };
+ return m[ext] || 'fas fa-file text-secondary';
+ };
+ const isImgExt = ext => /^(jpg|jpeg|png|gif|webp|bmp)$/.test(ext);
+ const chip = (label, thumb, delCall) => `
+
${thumb}
+
${escLab(label.length > 13 ? label.slice(0,10)+'\u2026' : label)}
+
+
`;
+
+ let html = '';
+ _localFiles.forEach((fname, i) => {
+ const ext = (fname||'').split('.').pop().toLowerCase();
+ const thumb = isImgExt(ext)
+ ? `
})
`
+ : `
`;
+ html += chip(fname, thumb, `labDomicilio._eliminarOrden('local',${i})`);
+ });
+ _newOrderFiles.forEach((file, i) => {
+ const ext = (file.name||'').split('.').pop().toLowerCase();
+ const dUrl = _newOrderDataUrls[i] || '';
+ const thumb = (isImgExt(ext) && dUrl)
+ ? `

`
+ : `
`;
+ html += chip(file.name, thumb, `labDomicilio._eliminarOrden('new',${i})`);
+ });
+ list.innerHTML = html;
},
- /* Abre el selector de archivo para cambiar o agregar la orden médica */
- _cambiarOrden() {
+ /* Abre el selector de archivo (soporta múltiples) y los agrega a la lista */
+ _agregarOrden() {
const inp = document.getElementById('labdom-orden-input');
if (!inp) return;
const handler = (e) => {
inp.removeEventListener('change', handler);
- const file = e.target.files && e.target.files[0];
- if (!file) return;
- _newOrderFile = file;
- _localFile = null;
- labDomicilio._updateOrdenPreview();
+ const files = e.target.files ? Array.from(e.target.files) : [];
+ if (!files.length) return;
+ let pending = files.length;
+ files.forEach(file => {
+ const ext = (file.name||'').split('.').pop().toLowerCase();
+ const isImage = /^(jpg|jpeg|png|gif|webp|bmp)$/.test(ext);
+ if (isImage) {
+ const reader = new FileReader();
+ reader.onload = ev => {
+ _newOrderFiles.push(file);
+ _newOrderDataUrls.push(ev.target.result);
+ if (--pending === 0) labDomicilio._updateOrdenPreview();
+ };
+ reader.readAsDataURL(file);
+ } else {
+ _newOrderFiles.push(file);
+ _newOrderDataUrls.push('');
+ if (--pending === 0) labDomicilio._updateOrdenPreview();
+ }
+ });
inp.value = '';
};
inp.addEventListener('change', handler);
inp.click();
},
- /* Elimina la orden médica adjunta */
- _eliminarOrden() {
- if (!confirm('¿Eliminar la orden médica adjunta?')) return;
- _localFile = null;
- _newOrderFile = null;
- _msgId = null;
+ /* Elimina un archivo específico de la lista */
+ _eliminarOrden(type, idx) {
+ if (type === 'local') {
+ _localFiles.splice(idx, 1);
+ if (!_localFiles.length && !_newOrderFiles.length) _msgId = null;
+ } else {
+ _newOrderFiles.splice(idx, 1);
+ _newOrderDataUrls.splice(idx, 1);
+ }
labDomicilio._updateOrdenPreview();
},
@@ -7410,42 +7421,43 @@ const labDomicilio = (() => {
notas_admin: fv('labdom-notas') || null,
};
- // Subir nuevo archivo de orden si el usuario cambió o agregó uno desde el modal
- if (_newOrderFile) {
+ // ── Subir archivos nuevos de orden ─────────────────────────────
+ for (const file of _newOrderFiles) {
try {
const fd = new FormData();
- fd.append('file', _newOrderFile);
+ fd.append('file', file);
const upRes = await fetch('api/lab/upload_orden.php', { method: 'POST', body: fd });
const upData = await upRes.json();
- if (upData.success && upData.local_file) {
- _localFile = upData.local_file;
- _newOrderFile = null;
- }
- } catch (_e) { /* silencioso — no bloquea el flujo */ }
+ if (upData.success && upData.local_file) _localFiles.push(upData.local_file);
+ } catch (_e) { /* silencioso */ }
}
+ _newOrderFiles = [];
+ _newOrderDataUrls = [];
- // Si hay orden médica (original o recién subida), crear registro y enlazar
- if (_localFile) {
+ // ── Crear registro de orden por cada archivo ───────────────────
+ if (_localFiles.length) {
try {
const convIdNum = (_convId && !String(_convId).startsWith('__user:'))
? parseInt(_convId) : null;
- const orRes = await fetch('api/lab/crear_desde_whatsapp.php', {
- method: 'POST',
- headers: { 'Content-Type': 'application/json' },
- body: JSON.stringify({
- paciente_id: _pacienteId,
- local_file: _localFile,
- media_message_id: _msgId || null,
- conversation_id: convIdNum,
- examenes_solicitados: datos.examenes_solicitados,
- estado: 'pendiente',
- }),
- });
- const orData = await orRes.json();
- if (orData.success && orData.orden_id) {
- datos.orden_id = orData.orden_id;
+ for (let _fi = 0; _fi < _localFiles.length; _fi++) {
+ const orRes = await fetch('api/lab/crear_desde_whatsapp.php', {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({
+ paciente_id: _pacienteId,
+ local_file: _localFiles[_fi],
+ media_message_id: _fi === 0 ? (_msgId || null) : null,
+ conversation_id: convIdNum,
+ examenes_solicitados: datos.examenes_solicitados,
+ estado: 'pendiente',
+ }),
+ });
+ const orData = await orRes.json();
+ if (orData.success && orData.orden_id && _fi === 0) {
+ datos.orden_id = orData.orden_id;
+ }
}
- } catch (_) { /* no bloquea si falla la creación de la orden */ }
+ } catch (_) { /* no bloquea */ }
}
try {
@@ -7598,9 +7610,10 @@ const labDomicilio = (() => {
pacienteId: _pacienteId,
pacNombre: _pacNombre,
histDirs: [..._histDirs],
- msgId: _msgId,
- localFile: _localFile,
- newOrderFile: _newOrderFile,
+ msgId: _msgId,
+ localFiles: [..._localFiles],
+ newOrderFiles: [..._newOrderFiles],
+ newOrderDataUrls: [..._newOrderDataUrls],
cardVisible: !document.getElementById('labdom-pac-card')?.classList.contains('d-none'),
pacInfo: {
nombre: document.getElementById('labdom-pac-nombre')?.value || '',
@@ -7687,9 +7700,10 @@ const labDomicilio = (() => {
_pacienteId = sesion.pacienteId;
_pacNombre = sesion.pacNombre;
_histDirs = sesion.histDirs || [];
- _msgId = sesion.msgId;
- _localFile = sesion.localFile;
- _newOrderFile = sesion.newOrderFile || null;
+ _msgId = sesion.msgId;
+ _localFiles = sesion.localFiles || (sesion.localFile ? [sesion.localFile] : []);
+ _newOrderFiles = sesion.newOrderFiles || (sesion.newOrderFile ? [sesion.newOrderFile] : []);
+ _newOrderDataUrls = sesion.newOrderDataUrls || [];
// Restaurar campos del formulario
const f = sesion.fields || {};