'use client'; import { useState, useEffect } from 'react'; import { CloseIcon } from './Icons'; import { fetchSmartFundNetValue } from '../api/fund'; import { DatePicker } from './Common'; import { Dialog, DialogContent, DialogTitle, } from '@/components/ui/dialog'; import { Button } from '@/components/ui/button'; export default function AddHistoryModal({ fund, onClose, onConfirm }) { const [type, setType] = useState(''); const [date, setDate] = useState(new Date().toISOString().split('T')[0]); const [amount, setAmount] = useState(''); const [share, setShare] = useState(''); const [netValue, setNetValue] = useState(null); const [netValueDate, setNetValueDate] = useState(null); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); useEffect(() => { if (!fund || !date) return; const getNetValue = async () => { setLoading(true); setError(null); setNetValue(null); setNetValueDate(null); try { const result = await fetchSmartFundNetValue(fund.code, date); if (result && result.value) { setNetValue(result.value); setNetValueDate(result.date); } else { setError('未找到该日期的净值数据'); } } catch (err) { console.error(err); setError('获取净值失败'); } finally { setLoading(false); } }; const timer = setTimeout(getNetValue, 500); return () => clearTimeout(timer); }, [fund, date]); // Recalculate share when netValue变化或金额变化 useEffect(() => { if (netValue && amount) { setShare((parseFloat(amount) / netValue).toFixed(2)); } }, [netValue, amount]); const handleAmountChange = (e) => { const val = e.target.value; setAmount(val); if (netValue && val) { setShare((parseFloat(val) / netValue).toFixed(2)); } else if (!val) { setShare(''); } }; const handleSubmit = () => { if (!type || !date || !netValue || !amount || !share) return; onConfirm({ fundCode: fund.code, type, date: netValueDate, // Use the date from net value to be precise amount: parseFloat(amount), share: parseFloat(share), price: netValue, timestamp: new Date(netValueDate).getTime() }); onClose(); }; const handleOpenChange = (open) => { if (!open) { onClose?.(); } }; const handleCloseClick = (event) => { event.stopPropagation(); onClose?.(); }; return ( 添加历史记录
添加历史记录
{fund?.name}
{fund?.code}
{loading &&
正在获取净值...
} {error &&
{error}
} {netValue && !loading && (
参考净值: {netValue} ({netValueDate})
)}
*此处补录的买入/卖出仅作记录展示,不会改变当前持仓金额与份额;实际持仓请在持仓设置中维护。
); }