import React, { useState } from 'react'; import { BillReminder } from '../types'; import { v4 as uuidv4 } from 'uuid'; interface BillRemindersProps { reminders: BillReminder[]; onAdd: (reminder: BillReminder) => void; onUpdate: (reminder: BillReminder) => void; onDelete: (id: string) => void; onToggle: (id: string) => void; } // Helper to calculate days remaining export const getDaysRemaining = (r: BillReminder): number => { const today = new Date(); today.setHours(0, 0, 0, 0); let targetDate = new Date(); targetDate.setHours(0, 0, 0, 0); if (r.frequency === 'monthly') { const day = parseInt(r.targetDate); targetDate.setDate(day); // If the day has passed this month, move to next month if (targetDate < today) { targetDate.setMonth(targetDate.getMonth() + 1); } } else { // Once targetDate = new Date(r.targetDate); targetDate.setHours(0, 0, 0, 0); } const diffTime = targetDate.getTime() - today.getTime(); const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24)); return diffDays; }; const BillReminders: React.FC = ({ reminders, onAdd, onUpdate, onDelete, onToggle }) => { const [isFormOpen, setIsFormOpen] = useState(false); const [editingId, setEditingId] = useState(null); const [title, setTitle] = useState(''); const [amount, setAmount] = useState(''); const [frequency, setFrequency] = useState<'monthly' | 'once'>('monthly'); const [targetDate, setTargetDate] = useState(''); // '15' or '2023-10-15' const resetForm = () => { setTitle(''); setAmount(''); setFrequency('monthly'); setTargetDate(''); setEditingId(null); setIsFormOpen(false); }; const startEdit = (r: BillReminder) => { setEditingId(r.id); setTitle(r.title); setAmount(r.amount ? r.amount.toString() : ''); setFrequency(r.frequency); setTargetDate(r.targetDate); setIsFormOpen(true); }; const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); // Amount is now optional, check title and targetDate only if (!title || !targetDate) return; const parsedAmount = parseFloat(amount); const reminderData: BillReminder = { id: editingId || uuidv4(), title, amount: isNaN(parsedAmount) ? undefined : parsedAmount, frequency, targetDate, isCompleted: editingId ? reminders.find(r => r.id === editingId)?.isCompleted || false : false }; if (editingId) { onUpdate(reminderData); } else { onAdd(reminderData); } resetForm(); }; const getDueText = (r: BillReminder) => { if (r.frequency === 'monthly') { return `每月 ${r.targetDate} 日`; } return r.targetDate; // YYYY-MM-DD }; const renderCountdown = (r: BillReminder) => { const days = getDaysRemaining(r); if (r.isCompleted) return 已完成; if (days < 0) return 逾期 {Math.abs(days)} 天; if (days === 0) return 今天到期; if (days <= 3) return 剩 {days} 天; return 剩 {days} 天; }; return (

待办 & 还款提醒

{isFormOpen && (

{editingId ? '编辑提醒' : '新提醒'}

setTitle(e.target.value)} className="w-full p-2 bg-gray-50 border-none rounded-xl text-sm focus:ring-2 focus:ring-indigo-500 outline-none transition-all" />
setAmount(e.target.value)} className="flex-1 p-2 bg-gray-50 border-none rounded-xl text-sm focus:ring-2 focus:ring-indigo-500 outline-none transition-all" />
{frequency === 'monthly' ? (
每月 setTargetDate(e.target.value)} className="w-12 p-1 border-b border-indigo-200 text-center bg-transparent focus:border-indigo-500 outline-none" /> 号还款
) : ( setTargetDate(e.target.value)} className="w-full p-2 bg-gray-50 border-none rounded-xl text-sm text-gray-600 outline-none" /> )}
)}
{reminders.length === 0 && !isFormOpen && (

📅

暂无待办事项
)} {reminders.map(r => (
{/* Decorative side bar */}
{/* Left: Info */}
{/* Row 1: Checkbox + Title + Amount */}
{r.title} {r.amount != null && ( ¥{r.amount} )}
{/* Row 2: Date + Countdown */}
{getDueText(r)}
{renderCountdown(r)}
{/* Right: Actions (Separated) */}
))}
); }; export default BillReminders;