File size: 4,045 Bytes
70e641d
 
 
 
e4a79ec
 
70e641d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4a79ec
 
70e641d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Inyecci贸n de valores en el formulario, compartida por el importador de PDF (pdf-parser.ts)
// y el de analizadores (lab-import.ts). La clave de uni贸n es el atributo `name` del input
// (== clave can贸nica de valores_referencia.json). Fuente 煤nica para no duplicar la l贸gica.

import { revelarPanelDeCampo } from './panel-vacio.js';

export type ValoresInyectables = Record<string, number | string>;

export interface PacienteInyectable {
  especie?: string;
  raza?: string;
  sexo?: string;
  edad?: number | string;
  edadUnidad?: string;
}

interface OpcionesInyeccion {
  resaltar?: boolean; // marca los campos rellenados con un destello transitorio
}

function resaltarCampo(el: HTMLElement): void {
  el.classList.add('campo-importado');
  setTimeout(() => el.classList.remove('campo-importado'), 2500);
}

// Rellena los inputs num茅ricos y los <select> semicuantitativos (uri-*) por su `name`.
// Dispara `evaluar()` una sola vez si se rellen贸 algo, igual que el importador de PDF.
export function aplicarValoresAFormulario(
  resultados: ValoresInyectables,
  evaluar: () => void,
  opciones: OpcionesInyeccion = {},
): number {
  let contador = 0;
  for (const [campo, value] of Object.entries(resultados)) {
    const el = document.querySelector(`[name="${campo}"]`) as HTMLInputElement | HTMLSelectElement | null;
    if (!el) continue;
    // Un valor importado a un panel en estado vac铆o quedar铆a escondido tras la zona de arrastre.
    revelarPanelDeCampo(el);
    const valorCadena = String(value);
    if (el.tagName === 'SELECT') {
      const select = el as HTMLSelectElement;
      if ([...select.options].some((o) => o.value === valorCadena)) {
        select.value = valorCadena;
        contador++;
        if (opciones.resaltar) resaltarCampo(select);
      }
    } else {
      el.value = valorCadena;
      contador++;
      if (opciones.resaltar) resaltarCampo(el);
    }
  }
  if (contador > 0) evaluar();
  return contador;
}

// Rellena los campos de paciente (pt-*) y sus espejos m贸viles (mob-pt-*), disparando el
// evento que reactiva el an谩lisis.
export function aplicarPacienteAFormulario(patient: PacienteInyectable): number {
  const MAPA = [
    { id: 'pt-especie', mobId: 'mob-pt-especie', key: 'especie', evt: 'change' },
    { id: 'pt-raza', mobId: 'mob-pt-raza', key: 'raza', evt: 'input' },
    { id: 'pt-edad', mobId: 'mob-pt-edad', key: 'edad', evt: 'input' },
    { id: 'pt-edad-unidad', mobId: 'mob-pt-edad-unidad', key: 'edadUnidad', evt: 'change' },
    { id: 'pt-sexo', mobId: 'mob-pt-sexo', key: 'sexo', evt: 'change' },
  ] as const;
  let contador = 0;
  for (const { id, mobId, key, evt } of MAPA) {
    const val = patient[key];
    if (val === undefined) continue;
    const el = document.getElementById(id) as HTMLInputElement | HTMLSelectElement | null;
    const mob = document.getElementById(mobId) as HTMLInputElement | HTMLSelectElement | null;
    if (!el) continue;
    const valorCadena = String(val);
    if (el.tagName === 'SELECT') {
      const select = el as HTMLSelectElement;
      const opcion = [...select.options].find((o) => o.value === valorCadena || o.text === valorCadena);
      if (!opcion) continue;
      select.value = opcion.value;
      if (mob) mob.value = opcion.value;
    } else {
      el.value = valorCadena;
      if (mob) mob.value = valorCadena;
    }
    el.dispatchEvent(new Event(evt, { bubbles: true }));
    contador++;
  }
  return contador;
}

// Toast ligero reutilizado por ambos importadores.
export function mostrarToast(mensaje: string, error = false): void {
  let el = document.getElementById('pdf-toast') as (HTMLElement & { _t?: ReturnType<typeof setTimeout> }) | null;
  if (!el) {
    el = document.createElement('div');
    el.id = 'pdf-toast';
    document.body.appendChild(el);
  }
  el.textContent = mensaje;
  el.className = 'pdf-toast' + (error ? ' pdf-toast--error' : '');
  el.classList.add('pdf-toast--show');
  clearTimeout(el._t);
  el._t = setTimeout(() => el!.classList.remove('pdf-toast--show'), 3500);
}