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 StartingAmountsEditDialog({ open, onClose, year, month, onSave }) { 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); 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(err.message || 'Monthly starting amounts could not be loaded.'); } finally { if (alive) setLoading(false); } } loadStartingAmounts(); return () => { alive = false; }; }, [open, year, month]); async function handleSave(e) { 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(err.message || 'Monthly starting amounts could not be saved.'); } finally { setSaving(false); } } return ( { if (!value) onClose(); }}> Monthly Starting Amounts

{monthName}

{error && (
{error}
)}

Total starting

{fmt(totalStarting)}

Paid so far

{fmt(paidSoFar)}

Total remaining

{fmt(totalRemaining)}

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