354 lines
13 KiB
TypeScript
354 lines
13 KiB
TypeScript
// ==============================================================================
|
|
// 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<PreauthorizedModalProps> = ({
|
|
isOpen,
|
|
onClose,
|
|
onSelect,
|
|
initialSearch = '',
|
|
}) => {
|
|
const [query, setQuery] = useState<string>(initialSearch);
|
|
const [soloVigentes, setSoloVigentes] = useState<boolean>(true);
|
|
const [preautorizados, setPreautorizados] = useState<Preauthorization[]>([]);
|
|
const [isLoading, setIsLoading] = useState<boolean>(false);
|
|
const [selectedItem, setSelectedItem] = useState<Preauthorization | null>(null);
|
|
|
|
const searchTimerRef = useRef<ReturnType<typeof setTimeout> | 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<HTMLSelectElement>) => {
|
|
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 (
|
|
<div
|
|
style={{
|
|
position: 'fixed',
|
|
inset: 0,
|
|
backgroundColor: 'rgba(0, 0, 0, 0.65)',
|
|
backdropFilter: 'blur(3px)',
|
|
display: 'flex',
|
|
alignItems: 'center',
|
|
justifyContent: 'center',
|
|
zIndex: 1050,
|
|
padding: '1rem',
|
|
}}
|
|
onClick={(e) => {
|
|
if (e.target === e.currentTarget) onClose();
|
|
}}
|
|
>
|
|
<div
|
|
className="card"
|
|
style={{
|
|
width: '100%',
|
|
maxWidth: '920px',
|
|
maxHeight: '90vh',
|
|
display: 'flex',
|
|
flexDirection: 'column',
|
|
boxShadow: 'var(--shadow-xl)',
|
|
border: '1px solid var(--border-subtle)',
|
|
borderRadius: 'var(--radius-md)',
|
|
backgroundColor: 'var(--bg-surface)',
|
|
overflow: 'hidden',
|
|
animation: 'fadeIn 0.15s ease-out',
|
|
}}
|
|
>
|
|
{/* Cabecera */}
|
|
<div
|
|
style={{
|
|
padding: '1rem 1.25rem',
|
|
borderBottom: '1px solid var(--border-subtle)',
|
|
display: 'flex',
|
|
justifyContent: 'space-between',
|
|
alignItems: 'center',
|
|
backgroundColor: 'var(--bg-app)',
|
|
}}
|
|
>
|
|
<div style={{ display: 'flex', alignItems: 'center', gap: '0.6rem' }}>
|
|
<span style={{ fontSize: '1.4rem' }}>📋</span>
|
|
<div>
|
|
<h3 style={{ margin: 0, fontSize: '1.15rem', fontWeight: 700, color: 'var(--text-primary)' }}>
|
|
Visitas Preautorizadas (Invitados)
|
|
</h3>
|
|
<p style={{ margin: 0, fontSize: '0.8rem', color: 'var(--text-secondary)' }}>
|
|
Seleccione un visitante preautorizado para precargar datos y lote al carril de entrada.
|
|
</p>
|
|
</div>
|
|
</div>
|
|
<button
|
|
type="button"
|
|
className="btn btn-secondary"
|
|
onClick={onClose}
|
|
style={{ minHeight: '36px', minWidth: '36px', padding: 0 }}
|
|
>
|
|
✕
|
|
</button>
|
|
</div>
|
|
|
|
{/* Filtros */}
|
|
<div
|
|
style={{
|
|
padding: '0.85rem 1.25rem',
|
|
borderBottom: '1px solid var(--border-subtle)',
|
|
backgroundColor: 'var(--bg-surface)',
|
|
display: 'flex',
|
|
gap: '0.75rem',
|
|
alignItems: 'center',
|
|
}}
|
|
>
|
|
<div style={{ flex: 1, position: 'relative' }}>
|
|
<input
|
|
type="text"
|
|
className="input-field"
|
|
placeholder="🔍 Filtrar por Visitante, Lote, Entidad o Documento..."
|
|
value={query}
|
|
onChange={(e) => handleQueryChange(e.target.value)}
|
|
onKeyDown={(e) => {
|
|
if (e.key === 'Enter') loadData(query, soloVigentes);
|
|
}}
|
|
autoFocus
|
|
style={{ paddingRight: query ? '2.5rem' : '1rem', minHeight: '42px' }}
|
|
/>
|
|
{query && (
|
|
<button
|
|
type="button"
|
|
onClick={() => handleQueryChange('')}
|
|
style={{
|
|
position: 'absolute',
|
|
right: '0.75rem',
|
|
top: '50%',
|
|
transform: 'translateY(-50%)',
|
|
background: 'none',
|
|
border: 'none',
|
|
color: 'var(--text-muted)',
|
|
cursor: 'pointer',
|
|
fontSize: '1rem',
|
|
}}
|
|
>
|
|
✕
|
|
</button>
|
|
)}
|
|
</div>
|
|
|
|
<select
|
|
className="input-field"
|
|
value={soloVigentes ? 'vigentes' : 'todas'}
|
|
onChange={handleToggleVigentes}
|
|
style={{ width: '180px', minHeight: '42px', fontWeight: 600 }}
|
|
>
|
|
<option value="vigentes">Vigentes Hoy</option>
|
|
<option value="todas">Todas las Fechas</option>
|
|
</select>
|
|
|
|
<button
|
|
type="button"
|
|
className="btn btn-primary"
|
|
onClick={() => loadData(query, soloVigentes)}
|
|
style={{ minHeight: '42px', padding: '0 1.25rem', fontWeight: 600 }}
|
|
>
|
|
Filtrar
|
|
</button>
|
|
</div>
|
|
|
|
{/* Tabla */}
|
|
<div style={{ flex: 1, overflowY: 'auto', padding: '0.5rem 1.25rem' }}>
|
|
<div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', margin: '0.4rem 0' }}>
|
|
<span style={{ fontSize: '0.8rem', fontWeight: 700, color: 'var(--text-secondary)', textTransform: 'uppercase' }}>
|
|
Resultados ({preautorizados.length}) {isLoading && '• Cargando...'}
|
|
</span>
|
|
<span style={{ fontSize: '0.75rem', color: 'var(--text-muted)' }}>
|
|
Doble clic para cargar al carril
|
|
</span>
|
|
</div>
|
|
|
|
<div
|
|
style={{
|
|
maxHeight: '400px',
|
|
overflowY: 'auto',
|
|
border: '1px solid var(--border-subtle)',
|
|
borderRadius: 'var(--radius-sm)',
|
|
backgroundColor: 'var(--bg-app)',
|
|
}}
|
|
>
|
|
{preautorizados.length === 0 && !isLoading ? (
|
|
<div style={{ padding: '2rem', textAlign: 'center', color: 'var(--text-secondary)', fontSize: '0.9rem' }}>
|
|
No hay visitas preautorizadas registradas con los filtros seleccionados.
|
|
</div>
|
|
) : (
|
|
<table style={{ width: '100%', borderCollapse: 'collapse', textAlign: 'left', fontSize: '0.85rem' }}>
|
|
<thead>
|
|
<tr style={{ backgroundColor: 'var(--bg-surface)', borderBottom: '1px solid var(--border-subtle)' }}>
|
|
<th style={{ padding: '0.55rem 0.75rem', width: '70px' }}>ID</th>
|
|
<th style={{ padding: '0.55rem 0.75rem', width: '110px' }}>Lote</th>
|
|
<th style={{ padding: '0.55rem 0.75rem' }}>Visitante</th>
|
|
<th style={{ padding: '0.55rem 0.75rem', width: '110px' }}>Documento</th>
|
|
<th style={{ padding: '0.55rem 0.75rem' }}>Entidad / Empresa</th>
|
|
<th style={{ padding: '0.55rem 0.75rem', width: '140px' }}>Vigencia</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{preautorizados.map((p) => {
|
|
const isSel = selectedItem?.id === p.id;
|
|
const nombreCompleto = `${p.apellido}, ${p.nombre}`.trim() || 'Sin Nombre';
|
|
return (
|
|
<tr
|
|
key={p.id}
|
|
onClick={() => 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',
|
|
}}
|
|
>
|
|
<td style={{ padding: '0.55rem 0.75rem', color: 'var(--text-muted)' }}>#{p.id}</td>
|
|
<td style={{ padding: '0.55rem 0.75rem' }}>
|
|
<span
|
|
style={{
|
|
padding: '0.15rem 0.4rem',
|
|
borderRadius: '4px',
|
|
backgroundColor: 'rgba(59, 130, 246, 0.15)',
|
|
color: 'var(--accent-primary)',
|
|
fontWeight: 700,
|
|
}}
|
|
>
|
|
Lote {p.lote_nombre}
|
|
</span>
|
|
</td>
|
|
<td style={{ padding: '0.55rem 0.75rem', color: 'var(--text-primary)' }}>
|
|
<strong>{nombreCompleto}</strong>
|
|
{p.motivo && p.motivo !== p.documento && (
|
|
<div style={{ fontSize: '0.75rem', color: 'var(--text-secondary)' }}>{p.motivo}</div>
|
|
)}
|
|
</td>
|
|
<td style={{ padding: '0.55rem 0.75rem', color: 'var(--text-primary)' }}>
|
|
{p.documento || <span style={{ color: 'var(--text-muted)' }}>-</span>}
|
|
</td>
|
|
<td style={{ padding: '0.55rem 0.75rem', color: 'var(--text-secondary)' }}>
|
|
{p.entidad || '-'}
|
|
</td>
|
|
<td style={{ padding: '0.55rem 0.75rem', fontSize: '0.78rem', color: 'var(--text-secondary)' }}>
|
|
<div>{p.fecha_inicio ? p.fecha_inicio.slice(0, 10) : ''}</div>
|
|
<div style={{ color: 'var(--text-muted)' }}>hasta {p.fecha_fin ? p.fecha_fin.slice(0, 10) : ''}</div>
|
|
</td>
|
|
</tr>
|
|
);
|
|
})}
|
|
</tbody>
|
|
</table>
|
|
)}
|
|
</div>
|
|
</div>
|
|
|
|
{/* Footer */}
|
|
<div
|
|
style={{
|
|
padding: '1rem 1.25rem',
|
|
borderTop: '1px solid var(--border-subtle)',
|
|
backgroundColor: 'var(--bg-app)',
|
|
display: 'flex',
|
|
justifyContent: 'space-between',
|
|
alignItems: 'center',
|
|
}}
|
|
>
|
|
<div style={{ fontSize: '0.85rem', color: 'var(--text-secondary)' }}>
|
|
{selectedItem ? (
|
|
<span>
|
|
Seleccionado: <strong>{selectedItem.apellido}, {selectedItem.nombre}</strong> (Lote {selectedItem.lote_nombre})
|
|
</span>
|
|
) : (
|
|
'Ningún visitante seleccionado'
|
|
)}
|
|
</div>
|
|
<div style={{ display: 'flex', gap: '0.75rem' }}>
|
|
<button type="button" className="btn btn-secondary" onClick={onClose} style={{ minHeight: '44px', padding: '0 1.25rem' }}>
|
|
Cerrar
|
|
</button>
|
|
<button
|
|
type="button"
|
|
className="btn btn-success"
|
|
disabled={!selectedItem}
|
|
onClick={() => handleConfirm()}
|
|
style={{
|
|
minHeight: '44px',
|
|
padding: '0 1.5rem',
|
|
fontWeight: 700,
|
|
backgroundColor: '#10b981',
|
|
color: '#ffffff',
|
|
border: 'none',
|
|
}}
|
|
>
|
|
📋 Cargar al Carril de Entrada
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|
|
|