import React, { useState, useEffect } from 'react'; import { toast } from 'sonner'; import { api } from '@/api.js'; import { fmt } from '@/lib/utils'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter, } from '@/components/ui/dialog'; const MONTHS = [ 'January','February','March','April','May','June', 'July','August','September','October','November','December', ]; function MonthlyStateDialog({ row, year, month, open, onOpenChange, onSaved }) { const [actualAmount, setActualAmount] = useState(''); const [notes, setNotes] = useState(''); const [isSkipped, setIsSkipped] = useState(false); const [saving, setSaving] = useState(false); // Populate from current row state when dialog opens useEffect(() => { if (open) { setActualAmount(row.actual_amount != null ? String(row.actual_amount) : ''); setNotes(row.monthly_notes || ''); setIsSkipped(!!row.is_skipped); } }, [open, row]); async function handleSave(e) { e.preventDefault(); const amt = actualAmount.trim() ? parseFloat(actualAmount) : null; if (amt !== null && (isNaN(amt) || amt < 0)) { toast.error('Amount must be a positive number or empty'); return; } setSaving(true); try { await api.saveBillMonthlyState(row.id, { year, month, actual_amount: amt, notes: notes.trim() || null, is_skipped: isSkipped, }); toast.success(`${MONTHS[month - 1]} state saved`); onSaved(); onOpenChange(false); } catch (err) { toast.error(err.message); } finally { setSaving(false); } } return ( {row.name} {MONTHS[month - 1]} {year}

Monthly overrides — changes only affect {MONTHS[month - 1]}

{/* Actual amount this month */}
setActualAmount(e.target.value)} className="font-mono bg-background/50 border-border/60" />

Leave blank to use the template default ({fmt(row.expected_amount)}).

{/* Monthly notes */}
setNotes(e.target.value)} placeholder="e.g. higher than usual, double-billed…" className="bg-background/50 border-border/60" />
{/* Skip this month */}
); } export default MonthlyStateDialog;