import React, { useState, useEffect } from 'react'; import { Modal, Box, Typography, TextField, Button, CircularProgress, Alert } from '@mui/material'; import type { StockBobinaDto } from '../../../models/dtos/Impresion/StockBobinaDto'; import type { UpdateFechaRemitoLoteDto } from '../../../models/dtos/Impresion/UpdateFechaRemitoLoteDto'; const modalStyle = { position: 'absolute' as 'absolute', top: '50%', left: '50%', transform: 'translate(-50%, -50%)', width: { xs: '90%', sm: 450 }, bgcolor: 'background.paper', border: '2px solid #000', boxShadow: 24, p: 4 }; interface Props { open: boolean; onClose: () => void; onSubmit: (data: UpdateFechaRemitoLoteDto) => Promise; bobinaContexto: StockBobinaDto | null; errorMessage: string | null; clearErrorMessage: () => void; } const StockBobinaFechaRemitoModal: React.FC = ({ open, onClose, onSubmit, bobinaContexto, errorMessage, clearErrorMessage }) => { const [nuevaFecha, setNuevaFecha] = useState(''); const [loading, setLoading] = useState(false); const [localError, setLocalError] = useState(null); useEffect(() => { if (open && bobinaContexto) { setNuevaFecha(bobinaContexto.fechaRemito.split('T')[0]); // Iniciar con la fecha actual setLocalError(null); clearErrorMessage(); } }, [open, bobinaContexto, clearErrorMessage]); if (!bobinaContexto) return null; const handleSubmit = async (event: React.FormEvent) => { event.preventDefault(); if (!nuevaFecha) { setLocalError('Debe seleccionar una nueva fecha.'); return; } setLoading(true); try { const data: UpdateFechaRemitoLoteDto = { idPlanta: bobinaContexto.idPlanta, remito: bobinaContexto.remito, fechaRemitoActual: bobinaContexto.fechaRemito.split('T')[0], nuevaFechaRemito: nuevaFecha }; await onSubmit(data); onClose(); } catch (err) { // El error de la API es manejado por el prop `errorMessage` } finally { setLoading(false); } }; return ( Corregir Fecha de Remito Esto cambiará la fecha para todas las bobinas del remito {bobinaContexto.remito} en la planta {bobinaContexto.nombrePlanta}. { setNuevaFecha(e.target.value); setLocalError(null); }} required fullWidth margin="normal" InputLabelProps={{ shrink: true }} error={!!localError} helperText={localError} autoFocus /> {errorMessage && {errorMessage}} ); }; export default StockBobinaFechaRemitoModal;