import React, { useState, useEffect } from 'react'; import { Box, Typography, TextField, Button, CircularProgress, Alert, FormControl, InputLabel, Select, MenuItem } from '@mui/material'; import empresaService from '../../services/Distribucion/empresaService'; import type { EmpresaDto } from '../../models/dtos/Distribucion/EmpresaDto'; interface SeleccionaReporteControlDevolucionesProps { onGenerarReporte: (params: { fecha: string; idEmpresa: number; }) => Promise; onCancel?: () => void; // Hacer onCancel opcional si no siempre se usa isLoading?: boolean; apiErrorMessage?: string | null; } const SeleccionaReporteControlDevoluciones: React.FC = ({ onGenerarReporte, onCancel, isLoading, apiErrorMessage }) => { const [fecha, setFecha] = useState(new Date().toISOString().split('T')[0]); const [idEmpresa, setIdEmpresa] = useState(''); const [empresas, setEmpresas] = useState([]); const [loadingEmpresas, setLoadingEmpresas] = useState(false); const [localError, setLocalError] = useState(null); useEffect(() => { const fetchEmpresas = async () => { setLoadingEmpresas(true); try { const data = await empresaService.getAllEmpresas(); // Solo habilitadas setEmpresas(data); } catch (error) { console.error("Error al cargar empresas:", error); setLocalError('Error al cargar empresas.'); } finally { setLoadingEmpresas(false); } }; fetchEmpresas(); }, []); const validate = (): boolean => { if (!fecha) { setLocalError('Debe seleccionar una fecha.'); return false; } if (!idEmpresa) { setLocalError('Debe seleccionar una empresa.'); return false; } setLocalError(null); return true; }; const handleGenerar = () => { if (!validate()) return; onGenerarReporte({ fecha, idEmpresa: Number(idEmpresa) }); }; return ( ParĂ¡metros: Control de Devoluciones setFecha(e.target.value)} margin="normal" fullWidth required disabled={isLoading} InputLabelProps={{ shrink: true }} /> Empresa {(localError && !apiErrorMessage) && {localError}} {apiErrorMessage && {apiErrorMessage}} {onCancel && } ); }; export default SeleccionaReporteControlDevoluciones;