import { useState, useRef, useEffect } from 'react'; import { Pencil } from 'lucide-react'; import { type MonthlyProjection } from '@/lib/api'; import { formatAmount } from '@/lib/format'; import { cn } from '@/lib/utils'; import { Input } from '@/components/ui/input'; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from '@/components/ui/table'; const MONTH_NAMES = [ '', 'Ene', 'Feb', 'Mar', 'Abr', 'May', 'Jun', 'Jul', 'Ago', 'Sep', 'Oct', 'Nov', 'Dic', ]; const FRESH_START_YEAR = 2026; const FRESH_START_MONTH = 3; interface YearlyOverviewProps { months: MonthlyProjection[]; selectedMonth: number; year: number; onSelectMonth: (month: number) => void; onSaveOverride: (month: number, value: number) => Promise; onClearOverride: (month: number) => Promise; } export default function YearlyOverview({ months, selectedMonth, year, onSelectMonth, onSaveOverride, onClearOverride, }: YearlyOverviewProps) { const currentMonth = new Date().getMonth() + 1; const currentYear = new Date().getFullYear(); const [editingMonth, setEditingMonth] = useState(null); const [editValue, setEditValue] = useState(''); const inputRef = useRef(null); useEffect(() => { if (editingMonth !== null && inputRef.current) { inputRef.current.focus(); inputRef.current.select(); } }, [editingMonth]); const handleStartEdit = (m: MonthlyProjection) => { setEditingMonth(m.month); setEditValue(String(Math.round(m.cumulative_balance))); }; const handleSave = async () => { if (editingMonth === null) return; const trimmed = editValue.trim(); if (trimmed === '') { await onClearOverride(editingMonth); } else { const num = parseFloat(trimmed); if (!isNaN(num)) { await onSaveOverride(editingMonth, num); } } setEditingMonth(null); }; const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === 'Enter') { handleSave(); } else if (e.key === 'Escape') { setEditingMonth(null); } }; return (
Mes Ingresos Egresos Fijos Otros Gastos Gran Total Acum. Anterior Neto Mes Balance Acum. {months.map((m) => { const isSelected = m.month === selectedMonth; const isCurrent = m.month === currentMonth && m.year === currentYear; const isBeforeFreshStart = year === FRESH_START_YEAR && m.month < FRESH_START_MONTH; const isEditing = editingMonth === m.month; return ( onSelectMonth(m.month)} > {MONTH_NAMES[m.month]} {isCurrent && ( )} {formatAmount(m.projected_income, 'CRC')} {formatAmount(m.projected_fixed_expenses, 'CRC')} {formatAmount(m.uncovered_actual, 'CRC')} {formatAmount(m.gran_total_egresos, 'CRC')} = 0 ? 'text-muted-foreground' : 'text-destructive', )} > {isBeforeFreshStart ? '—' : {m.carryover_balance >= 0 ? '+' : ''} {formatAmount(m.carryover_balance, 'CRC')} } = 0 ? 'text-primary' : 'text-destructive', )} > {m.net_balance >= 0 ? '+' : ''} {formatAmount(m.net_balance, 'CRC')} { if (isBeforeFreshStart) return; e.stopPropagation(); if (!isEditing) handleStartEdit(m); }} > {isBeforeFreshStart ? ( ) : isEditing ? ( setEditValue(e.target.value)} onBlur={handleSave} onKeyDown={handleKeyDown} className="h-7 w-36 text-right font-mono text-sm ml-auto" onClick={(e) => e.stopPropagation()} /> ) : ( = 0 ? 'text-primary' : 'text-destructive', )} > {m.balance_overridden && ( )} {m.cumulative_balance >= 0 ? '+' : ''} {formatAmount(m.cumulative_balance, 'CRC')} )} ); })}
); }