import { useState, useEffect } from 'react'; import { toast } from 'sonner'; import { api } from '@/api'; import { fmt, errMessage } from '@/lib/utils'; import { asDollars } from '@/lib/money'; 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', ]; interface StartingAmountsPreview { first_amount?: number; fifteenth_amount?: number; other_amount?: number; paid_total?: number; paid_from_first?: number; paid_from_fifteenth?: number; } interface StartingAmountsEditDialogProps { open: boolean; onClose: () => void; year: number; month: number; onSave: () => void; } function StartingAmountsEditDialog({ open, onClose, year, month, onSave }: StartingAmountsEditDialogProps) { const [loading, setLoading] = useState(false); const [saving, setSaving] = useState(false); const [error, setError] = useState(''); const [firstAmount, setFirstAmount] = useState('0'); const [fifteenthAmount, setFifteenthAmount] = useState('0'); const [otherAmount, setOtherAmount] = useState('0'); const [preview, setPreview] = useState(null); const monthName = `${MONTHS[month - 1]} ${year}`; const localFirst = Number(firstAmount) || 0; const localFifteenth = Number(fifteenthAmount) || 0; const localOther = Number(otherAmount) || 0; const totalStarting = localFirst + localFifteenth + localOther; const paidSoFar = Number(preview?.paid_total || 0); const firstRemaining = localFirst - Number(preview?.paid_from_first || 0); const fifteenthRemaining = localFifteenth - Number(preview?.paid_from_fifteenth || 0); const totalRemaining = totalStarting - paidSoFar; useEffect(() => { let alive = true; async function loadStartingAmounts() { if (!open) return; setLoading(true); setError(''); try { const result = await api.getMonthlyStartingAmounts(year, month) as StartingAmountsPreview; if (!alive) return; setPreview(result); setFirstAmount(String(result.first_amount ?? 0)); setFifteenthAmount(String(result.fifteenth_amount ?? 0)); setOtherAmount(String(result.other_amount ?? 0)); } catch (err) { if (!alive) return; setError(errMessage(err, 'Monthly starting amounts could not be loaded.')); } finally { if (alive) setLoading(false); } } loadStartingAmounts(); return () => { alive = false; }; }, [open, year, month]); async function handleSave(e: React.FormEvent) { e.preventDefault(); const first = Number(firstAmount); const fifteenth = Number(fifteenthAmount); const other = Number(otherAmount); if (![first, fifteenth, other].every(value => Number.isFinite(value) && value >= 0)) { setError('Starting amounts must be non-negative numbers.'); return; } setSaving(true); setError(''); try { await api.updateMonthlyStartingAmounts({ year, month, first_amount: first, fifteenth_amount: fifteenth, other_amount: other, }); toast.success('Monthly starting amounts saved.'); onSave(); } catch (err) { setError(errMessage(err, 'Monthly starting amounts could not be saved.')); } finally { setSaving(false); } } return ( { if (!value) onClose(); }}> Monthly Starting Amounts

{monthName}

{error && (
{error}
)}

Total starting

{fmt(asDollars(totalStarting))}

Paid so far

{fmt(asDollars(paidSoFar))}

Total remaining

{fmt(asDollars(totalRemaining))}

1st remaining {fmt(asDollars(firstRemaining))}
15th remaining {fmt(asDollars(fifteenthRemaining))}
Other {fmt(asDollars(localOther))}
); } export default StartingAmountsEditDialog;