2025-07-15 15:20:36 -03:00
|
|
|
import React, { useState, useRef } from 'react';
|
2025-07-03 11:44:10 -03:00
|
|
|
import {
|
|
|
|
|
Box, CircularProgress, Alert, Paper, Typography, Dialog,
|
|
|
|
|
DialogTitle, DialogContent, IconButton
|
|
|
|
|
} from '@mui/material';
|
|
|
|
|
import { PiCow } from "react-icons/pi";
|
|
|
|
|
import ScaleIcon from '@mui/icons-material/Scale';
|
|
|
|
|
import { PiChartLineUpBold } from "react-icons/pi";
|
|
|
|
|
import CloseIcon from '@mui/icons-material/Close';
|
2025-07-01 16:05:26 -03:00
|
|
|
|
2025-07-15 15:20:36 -03:00
|
|
|
// Importaciones de nuestro proyecto
|
2025-07-01 16:05:26 -03:00
|
|
|
import type { CotizacionGanado } from '../models/mercadoModels';
|
|
|
|
|
import { useApiData } from '../hooks/useApiData';
|
2025-07-15 15:20:36 -03:00
|
|
|
import { useIsHoliday } from '../hooks/useIsHoliday';
|
2025-07-14 12:50:29 -03:00
|
|
|
import { formatCurrency, formatInteger, formatDateOnly } from '../utils/formatters';
|
2025-07-03 11:44:10 -03:00
|
|
|
import { AgroHistoricalChartWidget } from './AgroHistoricalChartWidget';
|
2025-07-15 15:20:36 -03:00
|
|
|
import { HolidayAlert } from './common/HolidayAlert';
|
2025-07-01 16:05:26 -03:00
|
|
|
|
2025-07-15 15:20:36 -03:00
|
|
|
/**
|
|
|
|
|
* Sub-componente para una única tarjeta de categoría de ganado.
|
|
|
|
|
*/
|
|
|
|
|
const AgroCard = ({ registro, onChartClick }: { registro: CotizacionGanado, onChartClick: (event: React.MouseEvent<HTMLButtonElement>) => void }) => {
|
2025-07-01 16:05:26 -03:00
|
|
|
return (
|
2025-07-15 15:20:36 -03:00
|
|
|
<Paper elevation={2} sx={{ p: 2, flex: '1 1 250px', minWidth: '250px', maxWidth: '300px', position: 'relative', display: 'flex', flexDirection: 'column' }}>
|
|
|
|
|
{/* Contenido principal de la tarjeta */}
|
|
|
|
|
<Box sx={{ flexGrow: 1 }}>
|
|
|
|
|
<IconButton
|
|
|
|
|
aria-label="ver historial"
|
|
|
|
|
onClick={onChartClick}
|
|
|
|
|
sx={{
|
|
|
|
|
position: 'absolute', top: 8, right: 8,
|
|
|
|
|
backgroundColor: 'rgba(255, 255, 255, 0.7)',
|
|
|
|
|
backdropFilter: 'blur(2px)',
|
|
|
|
|
border: '1px solid rgba(0, 0, 0, 0.1)',
|
|
|
|
|
boxShadow: '0 2px 5px rgba(0,0,0,0.1)',
|
|
|
|
|
transition: 'all 0.2s ease-in-out',
|
|
|
|
|
'&:hover': {
|
|
|
|
|
transform: 'translateY(-2px)',
|
|
|
|
|
boxShadow: '0 4px 10px rgba(0,0,0,0.2)',
|
|
|
|
|
backgroundColor: 'rgba(255, 255, 255, 0.9)',
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<PiChartLineUpBold size="20" />
|
|
|
|
|
</IconButton>
|
2025-07-03 11:44:10 -03:00
|
|
|
|
2025-07-15 15:20:36 -03:00
|
|
|
<Typography variant="h6" component="h3" sx={{ fontWeight: 'bold', borderBottom: 1, borderColor: 'divider', pb: 1, mb: 2, pr: 5 }}>
|
|
|
|
|
{registro.categoria}
|
|
|
|
|
<Typography variant="body2" color="text.secondary">{registro.especificaciones}</Typography>
|
|
|
|
|
</Typography>
|
2025-07-03 11:44:10 -03:00
|
|
|
|
2025-07-15 15:20:36 -03:00
|
|
|
<Box sx={{ display: 'flex', justifyContent: 'space-between', mb: 1 }}>
|
|
|
|
|
<Typography variant="body2" color="text.secondary">Precio Máximo:</Typography>
|
|
|
|
|
<Typography variant="body2" sx={{ fontWeight: 'bold', color: 'success.main' }}>${formatCurrency(registro.maximo)}</Typography>
|
|
|
|
|
</Box>
|
|
|
|
|
<Box sx={{ display: 'flex', justifyContent: 'space-between', mb: 1 }}>
|
|
|
|
|
<Typography variant="body2" color="text.secondary">Precio Mínimo:</Typography>
|
|
|
|
|
<Typography variant="body2" sx={{ fontWeight: 'bold', color: 'error.main' }}>${formatCurrency(registro.minimo)}</Typography>
|
|
|
|
|
</Box>
|
|
|
|
|
<Box sx={{ display: 'flex', justifyContent: 'space-between', mb: 2 }}>
|
|
|
|
|
<Typography variant="body2" color="text.secondary">Precio Mediano:</Typography>
|
|
|
|
|
<Typography variant="body2" sx={{ fontWeight: 'bold' }}>${formatCurrency(registro.mediano)}</Typography>
|
|
|
|
|
</Box>
|
2025-07-01 16:05:26 -03:00
|
|
|
</Box>
|
2025-07-03 11:44:10 -03:00
|
|
|
|
2025-07-14 12:50:29 -03:00
|
|
|
{/* Pie de la tarjeta */}
|
2025-07-15 15:20:36 -03:00
|
|
|
<Box sx={{ mt: 'auto', pt: 1, borderTop: 1, borderColor: 'divider' }}>
|
2025-07-14 12:50:29 -03:00
|
|
|
<Box sx={{ display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 3 }}>
|
|
|
|
|
<Box sx={{ textAlign: 'center' }}>
|
|
|
|
|
<PiCow size="28" />
|
|
|
|
|
<Typography variant="body1" sx={{ fontWeight: 'bold' }}>{formatInteger(registro.cabezas)}</Typography>
|
|
|
|
|
<Typography variant="caption" color="text.secondary">Cabezas</Typography>
|
|
|
|
|
</Box>
|
|
|
|
|
<Box sx={{ textAlign: 'center' }}>
|
|
|
|
|
<ScaleIcon color="action" />
|
|
|
|
|
<Typography variant="body1" sx={{ fontWeight: 'bold' }}>{formatInteger(registro.kilosTotales)}</Typography>
|
|
|
|
|
<Typography variant="caption" color="text.secondary">Kilos</Typography>
|
|
|
|
|
</Box>
|
2025-07-01 16:05:26 -03:00
|
|
|
</Box>
|
2025-07-14 12:50:29 -03:00
|
|
|
|
|
|
|
|
<Typography variant="caption" sx={{ display: 'block', textAlign: 'left', color: 'text.secondary', mt: 1, pt: 1, borderTop: 1, borderColor: 'divider' }}>
|
2025-07-15 15:20:36 -03:00
|
|
|
{formatDateOnly(registro.fechaRegistro)}
|
2025-07-14 12:50:29 -03:00
|
|
|
</Typography>
|
2025-07-01 16:05:26 -03:00
|
|
|
</Box>
|
|
|
|
|
</Paper>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2025-07-15 15:20:36 -03:00
|
|
|
/**
|
|
|
|
|
* Widget autónomo para las tarjetas de resumen del Mercado Agroganadero.
|
|
|
|
|
*/
|
2025-07-01 16:05:26 -03:00
|
|
|
export const MercadoAgroCardWidget = () => {
|
2025-07-15 15:20:36 -03:00
|
|
|
const { data, loading: dataLoading, error: dataError } = useApiData<CotizacionGanado[]>('/mercados/agroganadero');
|
|
|
|
|
const isHoliday = useIsHoliday('BA');
|
|
|
|
|
|
2025-07-03 11:44:10 -03:00
|
|
|
const [selectedCategory, setSelectedCategory] = useState<CotizacionGanado | null>(null);
|
2025-07-15 15:20:36 -03:00
|
|
|
const triggerButtonRef = useRef<HTMLButtonElement | null>(null);
|
2025-07-03 11:44:10 -03:00
|
|
|
|
2025-07-15 15:20:36 -03:00
|
|
|
const handleChartClick = (registro: CotizacionGanado, event: React.MouseEvent<HTMLButtonElement>) => {
|
|
|
|
|
triggerButtonRef.current = event.currentTarget;
|
2025-07-03 11:44:10 -03:00
|
|
|
setSelectedCategory(registro);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleCloseDialog = () => {
|
|
|
|
|
setSelectedCategory(null);
|
2025-07-15 15:20:36 -03:00
|
|
|
setTimeout(() => {
|
|
|
|
|
triggerButtonRef.current?.focus();
|
|
|
|
|
}, 0);
|
2025-07-03 11:44:10 -03:00
|
|
|
};
|
2025-07-01 16:05:26 -03:00
|
|
|
|
2025-07-15 15:20:36 -03:00
|
|
|
const isLoading = dataLoading || isHoliday === null;
|
|
|
|
|
|
|
|
|
|
if (isLoading) {
|
2025-07-01 16:05:26 -03:00
|
|
|
return <Box sx={{ display: 'flex', justifyContent: 'center', p: 4 }}><CircularProgress /></Box>;
|
|
|
|
|
}
|
2025-07-15 15:20:36 -03:00
|
|
|
|
|
|
|
|
if (dataError) {
|
|
|
|
|
return <Alert severity="error">{dataError}</Alert>;
|
2025-07-01 16:05:26 -03:00
|
|
|
}
|
2025-07-15 15:20:36 -03:00
|
|
|
|
2025-07-01 16:05:26 -03:00
|
|
|
if (!data || data.length === 0) {
|
2025-07-15 15:20:36 -03:00
|
|
|
if (isHoliday) {
|
|
|
|
|
return <HolidayAlert />;
|
|
|
|
|
}
|
2025-07-01 16:05:26 -03:00
|
|
|
return <Alert severity="info">No hay datos del mercado agroganadero disponibles.</Alert>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
2025-07-03 11:44:10 -03:00
|
|
|
<>
|
2025-07-15 15:20:36 -03:00
|
|
|
{isHoliday && (
|
|
|
|
|
<Box sx={{ mb: 2 }}>
|
|
|
|
|
<HolidayAlert />
|
|
|
|
|
</Box>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
<Box sx={{ display: 'flex', flexWrap: 'wrap', gap: { xs: 2, sm: 2, md: 3 }, justifyContent: 'center' }}>
|
2025-07-03 11:44:10 -03:00
|
|
|
{data.map(registro => (
|
2025-07-15 15:20:36 -03:00
|
|
|
<AgroCard key={registro.id} registro={registro} onChartClick={(event) => handleChartClick(registro, event)} />
|
2025-07-03 11:44:10 -03:00
|
|
|
))}
|
|
|
|
|
</Box>
|
2025-07-15 15:20:36 -03:00
|
|
|
|
2025-07-03 11:44:10 -03:00
|
|
|
<Dialog
|
|
|
|
|
open={Boolean(selectedCategory)}
|
|
|
|
|
onClose={handleCloseDialog}
|
|
|
|
|
maxWidth="md"
|
|
|
|
|
fullWidth
|
2025-07-15 15:20:36 -03:00
|
|
|
sx={{ '& .MuiDialog-paper': { overflow: 'visible' } }}
|
2025-07-03 11:44:10 -03:00
|
|
|
>
|
|
|
|
|
<IconButton
|
|
|
|
|
aria-label="close"
|
|
|
|
|
onClick={handleCloseDialog}
|
|
|
|
|
sx={{
|
2025-07-15 15:20:36 -03:00
|
|
|
position: 'absolute', top: -15, right: -15,
|
2025-07-03 11:44:10 -03:00
|
|
|
color: (theme) => theme.palette.grey[500],
|
2025-07-15 15:20:36 -03:00
|
|
|
backgroundColor: 'white', boxShadow: 3,
|
|
|
|
|
'&:hover': { backgroundColor: 'grey.100' },
|
2025-07-03 11:44:10 -03:00
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<CloseIcon />
|
|
|
|
|
</IconButton>
|
|
|
|
|
|
|
|
|
|
<DialogTitle sx={{ m: 0, p: 2 }}>
|
2025-07-15 15:20:36 -03:00
|
|
|
Historial de 30 días para {selectedCategory?.categoria} ({selectedCategory?.especificaciones})
|
2025-07-03 11:44:10 -03:00
|
|
|
</DialogTitle>
|
|
|
|
|
<DialogContent dividers>
|
|
|
|
|
{selectedCategory && (
|
|
|
|
|
<AgroHistoricalChartWidget
|
|
|
|
|
categoria={selectedCategory.categoria}
|
|
|
|
|
especificaciones={selectedCategory.especificaciones}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</DialogContent>
|
|
|
|
|
</Dialog>
|
|
|
|
|
</>
|
2025-07-01 16:05:26 -03:00
|
|
|
);
|
|
|
|
|
};
|