// ============================================================================== // Modal de Visitas Preautorizadas (Invitados) // Paridad 1:1 con DialogoBuscarPreautorizados (pct_dialogs.py - OneverAccess / OemspotPySYS) // ============================================================================== import React, { useState, useEffect, useRef, useCallback } from 'react'; import { Preauthorization } from '../../types'; import { accessApi } from '../../api/accessApi'; interface PreauthorizedModalProps { isOpen: boolean; onClose: () => void; onSelect: (preaut: Preauthorization) => void; initialSearch?: string; } export const PreauthorizedModal: React.FC = ({ isOpen, onClose, onSelect, initialSearch = '', }) => { const [query, setQuery] = useState(initialSearch); const [soloVigentes, setSoloVigentes] = useState(true); const [preautorizados, setPreautorizados] = useState([]); const [isLoading, setIsLoading] = useState(false); const [selectedItem, setSelectedItem] = useState(null); const searchTimerRef = useRef | null>(null); const loadData = useCallback(async (searchTerm: string, vigentes: boolean) => { setIsLoading(true); try { const data = await accessApi.getPreauthorizations(searchTerm, vigentes); setPreautorizados(data); if (data.length > 0) { setSelectedItem((prev) => (prev ? (data.find((p) => p.id === prev.id) || data[0]) : data[0])); } else { setSelectedItem(null); } } catch (e) { console.error('Error al cargar preautorizados:', e); setPreautorizados([]); setSelectedItem(null); } finally { setIsLoading(false); } }, []); useEffect(() => { if (isOpen) { setQuery(initialSearch); loadData(initialSearch, soloVigentes); } else { setQuery(''); setSelectedItem(null); } }, [isOpen, initialSearch, loadData, soloVigentes]); const handleQueryChange = (text: string) => { setQuery(text); if (searchTimerRef.current) { clearTimeout(searchTimerRef.current); } searchTimerRef.current = setTimeout(() => { loadData(text, soloVigentes); }, 250); }; const handleToggleVigentes = (e: React.ChangeEvent) => { const val = e.target.value === 'vigentes'; setSoloVigentes(val); loadData(query, val); }; const handleConfirm = (item?: Preauthorization | null) => { const target = item || selectedItem; if (!target) return; onSelect(target); onClose(); }; if (!isOpen) return null; return (
{ if (e.target === e.currentTarget) onClose(); }} >
{/* Cabecera */}
πŸ“‹

Visitas Preautorizadas (Invitados)

Seleccione un visitante preautorizado para precargar datos y lote al carril de entrada.

{/* Filtros */}
handleQueryChange(e.target.value)} onKeyDown={(e) => { if (e.key === 'Enter') loadData(query, soloVigentes); }} autoFocus style={{ paddingRight: query ? '2.5rem' : '1rem', minHeight: '42px' }} /> {query && ( )}
{/* Tabla */}
Resultados ({preautorizados.length}) {isLoading && 'β€’ Cargando...'} Doble clic para cargar al carril
{preautorizados.length === 0 && !isLoading ? (
No hay visitas preautorizadas registradas con los filtros seleccionados.
) : ( {preautorizados.map((p) => { const isSel = selectedItem?.id === p.id; const nombreCompleto = `${p.apellido}, ${p.nombre}`.trim() || 'Sin Nombre'; return ( setSelectedItem(p)} onDoubleClick={() => handleConfirm(p)} style={{ cursor: 'pointer', backgroundColor: isSel ? 'rgba(37, 99, 235, 0.18)' : 'transparent', borderBottom: '1px solid var(--border-subtle)', fontWeight: isSel ? 700 : 400, borderLeft: isSel ? '4px solid var(--accent-primary)' : '4px solid transparent', transition: 'background 0.1s ease', }} > ); })}
ID Lote Visitante Documento Entidad / Empresa Vigencia
#{p.id} Lote {p.lote_nombre} {nombreCompleto} {p.motivo && p.motivo !== p.documento && (
{p.motivo}
)}
{p.documento || -} {p.entidad || '-'}
{p.fecha_inicio ? p.fecha_inicio.slice(0, 10) : ''}
hasta {p.fecha_fin ? p.fecha_fin.slice(0, 10) : ''}
)}
{/* Footer */}
{selectedItem ? ( Seleccionado: {selectedItem.apellido}, {selectedItem.nombre} (Lote {selectedItem.lote_nombre}) ) : ( 'NingΓΊn visitante seleccionado' )}
); };