Move category CRUD into the config route so renames sync historical deals, feed per-user categories into the LLM prompt, cache clients per user, and drop the separate classify route.
953 lines
29 KiB
JavaScript
953 lines
29 KiB
JavaScript
import React, { useState, useEffect, useRef } from "react";
|
||
import {
|
||
RotateCcw,
|
||
Link2,
|
||
Brain,
|
||
ChevronLeft,
|
||
ChevronRight,
|
||
ArrowUpDown,
|
||
ArrowUp,
|
||
ArrowDown,
|
||
CheckCircle,
|
||
AlertCircle,
|
||
X,
|
||
} from "lucide-react";
|
||
import {
|
||
getTransactions,
|
||
getDeals,
|
||
updateDeal,
|
||
clearDeals,
|
||
runReconciliation,
|
||
classifyAll,
|
||
getClassifyStatus,
|
||
formatAmount,
|
||
SOURCE_TYPE_MAP,
|
||
getCategories,
|
||
} from "../hooks/useApi";
|
||
|
||
function fmtDatetime(dt) {
|
||
if (!dt) return "";
|
||
return String(dt).substring(0, 19);
|
||
}
|
||
|
||
const DEAL_FILTER_DEFAULT = {
|
||
page: 1,
|
||
limit: 10,
|
||
start_date: "",
|
||
end_date: "",
|
||
direction: "",
|
||
category: "",
|
||
keyword: "",
|
||
sort_by: "deal_date",
|
||
sort_order: "DESC",
|
||
};
|
||
|
||
export default function Transactions() {
|
||
const [data, setData] = useState({
|
||
transactions: [],
|
||
total: 0,
|
||
page: 1,
|
||
totalPages: 0,
|
||
totalIncome: 0,
|
||
totalExpense: 0,
|
||
});
|
||
const [filters, setFilters] = useState({
|
||
page: 1,
|
||
limit: 10,
|
||
start_date: "",
|
||
end_date: "",
|
||
source_type: "",
|
||
direction: "",
|
||
keyword: "",
|
||
sort_by: "transaction_time",
|
||
sort_order: "DESC",
|
||
});
|
||
const [loading, setLoading] = useState(true);
|
||
const [dealLoading, setDealLoading] = useState(false);
|
||
const [categories, setCategories] = useState([]);
|
||
const [reconciling, setReconciling] = useState(false);
|
||
const [classifying, setClassifying] = useState(false);
|
||
const [classifyProgress, setClassifyProgress] = useState(0);
|
||
const [toast, setToast] = useState(null);
|
||
const [deals, setDeals] = useState({
|
||
deals: [],
|
||
total: 0,
|
||
page: 1,
|
||
totalPages: 0,
|
||
totalIncome: 0,
|
||
totalExpense: 0,
|
||
});
|
||
const [clearConfirm, setClearConfirm] = useState(false);
|
||
const [editingDeal, setEditingDeal] = useState(null);
|
||
const [dealFilters, setDealFilters] = useState({ ...DEAL_FILTER_DEFAULT });
|
||
const classifyPollRef = useRef(null);
|
||
const reloadRef = useRef(null);
|
||
|
||
/* sort helpers */
|
||
function dealToggleSort(field) {
|
||
setDealFilters((p) => ({
|
||
...p,
|
||
sort_by: field,
|
||
sort_order:
|
||
p.sort_by === field && p.sort_order === "DESC" ? "ASC" : "DESC",
|
||
page: 1,
|
||
}));
|
||
}
|
||
function DealSortIcon({ field }) {
|
||
if (dealFilters.sort_by !== field)
|
||
return <ArrowUpDown size={12} style={{ opacity: 0.3 }} />;
|
||
return dealFilters.sort_order === "ASC" ? (
|
||
<ArrowUp size={12} />
|
||
) : (
|
||
<ArrowDown size={12} />
|
||
);
|
||
}
|
||
function DealTh({ field, children, style }) {
|
||
return (
|
||
<th
|
||
onClick={() => dealToggleSort(field)}
|
||
style={{ cursor: "pointer", userSelect: "none", ...style }}
|
||
>
|
||
<span style={{ display: "inline-flex", alignItems: "center", gap: 4 }}>
|
||
{children}
|
||
<DealSortIcon field={field} />
|
||
</span>
|
||
</th>
|
||
);
|
||
}
|
||
|
||
function SortIcon({ field }) {
|
||
if (filters.sort_by !== field)
|
||
return <ArrowUpDown size={12} style={{ opacity: 0.3 }} />;
|
||
return filters.sort_order === "ASC" ? (
|
||
<ArrowUp size={12} />
|
||
) : (
|
||
<ArrowDown size={12} />
|
||
);
|
||
}
|
||
function Th({ field, children, style }) {
|
||
return (
|
||
<th
|
||
onClick={() => toggleSort(field)}
|
||
style={{ cursor: "pointer", userSelect: "none", ...style }}
|
||
>
|
||
<span style={{ display: "inline-flex", alignItems: "center", gap: 4 }}>
|
||
{children}
|
||
<SortIcon field={field} />
|
||
</span>
|
||
</th>
|
||
);
|
||
}
|
||
|
||
function showToast(msg, type) {
|
||
setToast({ message: msg, type, key: Date.now() });
|
||
}
|
||
|
||
function stopClassifyPoll() {
|
||
if (classifyPollRef.current) {
|
||
clearInterval(classifyPollRef.current);
|
||
classifyPollRef.current = null;
|
||
}
|
||
}
|
||
|
||
// 轮询服务端全量分类进度
|
||
function pollClassifyStatus() {
|
||
const tick = async () => {
|
||
try {
|
||
const s = await getClassifyStatus();
|
||
if (s.running) {
|
||
setClassifying(true);
|
||
setClassifyProgress(
|
||
s.total ? Math.min(100, Math.round((s.done / s.total) * 100)) : 0,
|
||
);
|
||
} else {
|
||
stopClassifyPoll();
|
||
setClassifying(false);
|
||
setClassifyProgress(100);
|
||
showToast("分类完成!", "success");
|
||
if (reloadRef.current) reloadRef.current();
|
||
}
|
||
} catch (e) {
|
||
stopClassifyPoll();
|
||
setClassifying(false);
|
||
}
|
||
};
|
||
stopClassifyPoll();
|
||
classifyPollRef.current = setInterval(tick, 2000);
|
||
tick();
|
||
}
|
||
|
||
/* 保持 reload 回调最新,避免轮询里拿到旧 filters */
|
||
useEffect(() => {
|
||
reloadRef.current = () => {
|
||
loadTransactions();
|
||
loadDeals(dealFilters);
|
||
};
|
||
});
|
||
|
||
/* 恢复全量分类进度:任务在服务端后台执行,刷新页面后继续显示 */
|
||
useEffect(() => {
|
||
let stopped = false;
|
||
(async () => {
|
||
try {
|
||
const s = await getClassifyStatus();
|
||
if (stopped || !s.running) return;
|
||
setClassifying(true);
|
||
setClassifyProgress(
|
||
s.total ? Math.min(100, Math.round((s.done / s.total) * 100)) : 0,
|
||
);
|
||
pollClassifyStatus();
|
||
} catch (e) {}
|
||
})();
|
||
return () => {
|
||
stopped = true;
|
||
stopClassifyPoll();
|
||
};
|
||
}, []);
|
||
|
||
useEffect(() => {
|
||
loadCategories();
|
||
loadDeals(DEAL_FILTER_DEFAULT);
|
||
}, []);
|
||
useEffect(() => {
|
||
loadDeals(dealFilters);
|
||
}, [dealFilters]);
|
||
useEffect(() => {
|
||
loadTransactions();
|
||
}, [filters]);
|
||
|
||
async function loadCategories() {
|
||
try {
|
||
setCategories(await getCategories());
|
||
} catch (e) {}
|
||
}
|
||
|
||
async function loadDeals(f) {
|
||
setDealLoading(true);
|
||
try {
|
||
const params = {};
|
||
for (const [k, v] of Object.entries(f))
|
||
if (v !== "" && v !== undefined) params[k] = v;
|
||
setDeals(await getDeals(params));
|
||
} catch (e) {}
|
||
setDealLoading(false);
|
||
}
|
||
|
||
async function loadTransactions() {
|
||
setLoading(true);
|
||
try {
|
||
const p = {};
|
||
for (const [k, v] of Object.entries(filters))
|
||
if (v !== "" && v !== undefined) p[k] = v;
|
||
setData(await getTransactions(p));
|
||
} catch (e) {}
|
||
setLoading(false);
|
||
}
|
||
|
||
function updateFilter(k, v) {
|
||
setFilters((prev) => ({ ...prev, [k]: v, page: k === "page" ? v : 1 }));
|
||
}
|
||
function updateDealFilter(k, v) {
|
||
setDealFilters((prev) => ({ ...prev, [k]: v, page: k === "page" ? v : 1 }));
|
||
}
|
||
|
||
function toggleSort(field) {
|
||
setFilters((prev) => ({
|
||
...prev,
|
||
sort_by: field,
|
||
sort_order:
|
||
prev.sort_by === field && prev.sort_order === "DESC" ? "ASC" : "DESC",
|
||
page: 1,
|
||
}));
|
||
}
|
||
|
||
/* deal edit / clear */
|
||
async function handleUpdateDeal() {
|
||
if (!editingDeal) return;
|
||
try {
|
||
await updateDeal(editingDeal.id, {
|
||
category: editingDeal.category,
|
||
ai_description: editingDeal.ai_description,
|
||
});
|
||
setEditingDeal(null);
|
||
loadDeals(dealFilters);
|
||
showToast("已更新", "success");
|
||
} catch (e) {
|
||
showToast("更新失败", "error");
|
||
}
|
||
}
|
||
|
||
async function handleClearDeals() {
|
||
try {
|
||
await clearDeals();
|
||
setDeals({
|
||
deals: [],
|
||
total: 0,
|
||
page: 1,
|
||
totalPages: 0,
|
||
totalIncome: 0,
|
||
totalExpense: 0,
|
||
});
|
||
setClearConfirm(false);
|
||
} catch (e) {}
|
||
}
|
||
|
||
async function handleReconcile() {
|
||
setReconciling(true);
|
||
try {
|
||
const r = await runReconciliation();
|
||
showToast(
|
||
"匹配 " + (r.matched || 0) + " 对,生成 " + (r.deals || 0) + " 条关联",
|
||
"success",
|
||
);
|
||
loadTransactions();
|
||
loadDeals(dealFilters);
|
||
} catch (e) {}
|
||
setReconciling(false);
|
||
}
|
||
|
||
async function handleClassifyAll() {
|
||
try {
|
||
const { total } = await classifyAll();
|
||
if (!total) {
|
||
showToast("所有交易分类均有效!", "info");
|
||
return;
|
||
}
|
||
setClassifying(true);
|
||
setClassifyProgress(0);
|
||
pollClassifyStatus();
|
||
} catch (e) {
|
||
showToast(
|
||
"分类启动失败: " + (e.response?.data?.error || e.message),
|
||
"error",
|
||
);
|
||
}
|
||
}
|
||
|
||
/* ============================== RENDER ============================== */
|
||
const hasTotals = data.totalIncome || data.totalExpense;
|
||
|
||
return (
|
||
<div>
|
||
<div className="page-header">
|
||
<h2>📋 交易明细</h2>
|
||
<p>查看、筛选、排序、对账和分类</p>
|
||
</div>
|
||
|
||
<div
|
||
style={{ display: "flex", gap: 10, marginBottom: 20, flexWrap: "wrap" }}
|
||
>
|
||
<button
|
||
className="btn btn-primary"
|
||
onClick={handleReconcile}
|
||
disabled={reconciling}
|
||
>
|
||
<Link2 size={16} /> {reconciling ? "对账中..." : "自动对账"}
|
||
</button>
|
||
<button
|
||
className="btn btn-secondary"
|
||
onClick={handleClassifyAll}
|
||
disabled={classifying}
|
||
>
|
||
<Brain size={16} />{" "}
|
||
{classifying ? `分类中 ${classifyProgress}%` : "AI 分类全部"}
|
||
</button>
|
||
<button
|
||
className="btn btn-danger"
|
||
onClick={() => setClearConfirm(true)}
|
||
style={{ marginLeft: "auto" }}
|
||
>
|
||
清空关联
|
||
</button>
|
||
</div>
|
||
|
||
{/* --- 交易记录 --- */}
|
||
<div className="card">
|
||
<div className="card-header">
|
||
<h3>交易记录</h3>
|
||
<span style={{ fontSize: 13, color: "var(--text-secondary)" }}>
|
||
共 {data.total} 笔,第 {data.page}/{data.totalPages} 页
|
||
</span>
|
||
</div>
|
||
|
||
<div className="filter-bar" style={{ padding: "0 20px 12px" }}>
|
||
<div className="form-group">
|
||
<label>开始</label>
|
||
<input
|
||
type="date"
|
||
className="form-input"
|
||
value={filters.start_date}
|
||
onChange={(e) => updateFilter("start_date", e.target.value)}
|
||
/>
|
||
</div>
|
||
<div className="form-group">
|
||
<label>结束</label>
|
||
<input
|
||
type="date"
|
||
className="form-input"
|
||
value={filters.end_date}
|
||
onChange={(e) => updateFilter("end_date", e.target.value)}
|
||
/>
|
||
</div>
|
||
<div className="form-group">
|
||
<label>来源</label>
|
||
<select
|
||
className="form-select"
|
||
value={filters.source_type}
|
||
onChange={(e) => updateFilter("source_type", e.target.value)}
|
||
>
|
||
<option value="">全部</option>
|
||
{Object.entries(SOURCE_TYPE_MAP).map(([k, v]) => (
|
||
<option key={k} value={k}>
|
||
{v}
|
||
</option>
|
||
))}
|
||
</select>
|
||
</div>
|
||
<div className="form-group">
|
||
<label>方向</label>
|
||
<select
|
||
className="form-select"
|
||
value={filters.direction}
|
||
onChange={(e) => updateFilter("direction", e.target.value)}
|
||
>
|
||
<option value="">全部</option>
|
||
<option value="income">收入</option>
|
||
<option value="expense">支出</option>
|
||
</select>
|
||
</div>
|
||
<div className="form-group">
|
||
<label>搜索</label>
|
||
<input
|
||
type="text"
|
||
className="form-input"
|
||
placeholder="对方/描述..."
|
||
value={filters.keyword}
|
||
onChange={(e) => updateFilter("keyword", e.target.value)}
|
||
/>
|
||
</div>
|
||
{hasTotals ? (
|
||
<div
|
||
style={{
|
||
alignSelf: "flex-end",
|
||
fontSize: 12,
|
||
color: "var(--text-secondary)",
|
||
whiteSpace: "nowrap",
|
||
paddingBottom: 8,
|
||
marginLeft: "auto",
|
||
}}
|
||
>
|
||
筛选结果 · 收入{" "}
|
||
<span style={{ color: "var(--success)", fontWeight: 600 }}>
|
||
+{(data.totalIncome || 0).toFixed(2)}
|
||
</span>{" "}
|
||
· 支出{" "}
|
||
<span style={{ color: "var(--danger)", fontWeight: 600 }}>
|
||
-{(data.totalExpense || 0).toFixed(2)}
|
||
</span>
|
||
</div>
|
||
) : null}
|
||
</div>
|
||
|
||
{loading ? (
|
||
<div className="loading">
|
||
<div className="spinner"></div>加载中...
|
||
</div>
|
||
) : data.transactions.length > 0 ? (
|
||
<>
|
||
<div className="table-container">
|
||
<table>
|
||
<thead>
|
||
<tr>
|
||
<Th field="direction">方向</Th>
|
||
<Th field="id">ID</Th>
|
||
<Th field="transaction_time">日期</Th>
|
||
<Th field="amount" style={{ textAlign: "right" }}>
|
||
金额
|
||
</Th>
|
||
<Th field="counterparty">对方</Th>
|
||
<th>描述</th>
|
||
<Th field="source_type">来源</Th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
{data.transactions.map((tx) => (
|
||
<tr key={tx.id}>
|
||
<td>
|
||
<span
|
||
className={`tag ${tx.direction === "income" ? "tag-income" : "tag-expense"}`}
|
||
>
|
||
{tx.direction === "income" ? "收入" : "支出"}
|
||
</span>
|
||
</td>
|
||
<td
|
||
style={{ fontSize: 12, color: "var(--text-secondary)" }}
|
||
>
|
||
{tx.id}
|
||
</td>
|
||
<td style={{ whiteSpace: "nowrap", fontSize: 13 }}>
|
||
{fmtDatetime(tx.transaction_time) ||
|
||
fmtDatetime(tx.transaction_date)}
|
||
</td>
|
||
<td
|
||
style={{
|
||
textAlign: "right",
|
||
fontWeight: 600,
|
||
whiteSpace: "nowrap",
|
||
color:
|
||
tx.direction === "income"
|
||
? "var(--success)"
|
||
: "var(--danger)",
|
||
}}
|
||
>
|
||
{formatAmount(tx.amount)}
|
||
</td>
|
||
<td>{tx.counterparty || "--"}</td>
|
||
<td
|
||
style={{
|
||
maxWidth: 140,
|
||
overflow: "hidden",
|
||
textOverflow: "ellipsis",
|
||
whiteSpace: "nowrap",
|
||
fontSize: 13,
|
||
}}
|
||
title={tx.description}
|
||
>
|
||
{tx.description || "--"}
|
||
</td>
|
||
<td>
|
||
<span
|
||
className={`source-type-label source-${tx.source_type}`}
|
||
>
|
||
{SOURCE_TYPE_MAP[tx.source_type] || tx.source_type}
|
||
</span>
|
||
</td>
|
||
</tr>
|
||
))}
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
{data.totalPages > 1 && (
|
||
<div className="pagination">
|
||
<button
|
||
disabled={data.page <= 1}
|
||
onClick={() => updateFilter("page", data.page - 1)}
|
||
>
|
||
<ChevronLeft size={14} /> 上一页
|
||
</button>
|
||
{Array.from(
|
||
{ length: Math.min(data.totalPages, 7) },
|
||
(_, i) => {
|
||
const p = i + Math.max(1, data.page - 3);
|
||
if (p > data.totalPages) return null;
|
||
return (
|
||
<button
|
||
key={p}
|
||
className={data.page === p ? "active" : ""}
|
||
onClick={() => updateFilter("page", p)}
|
||
>
|
||
{p}
|
||
</button>
|
||
);
|
||
},
|
||
)}
|
||
<button
|
||
disabled={data.page >= data.totalPages}
|
||
onClick={() => updateFilter("page", data.page + 1)}
|
||
>
|
||
下一页 <ChevronRight size={14} />
|
||
</button>
|
||
</div>
|
||
)}
|
||
</>
|
||
) : (
|
||
<div className="empty-state">
|
||
<div className="icon">📭</div>
|
||
<p>没有找到交易记录</p>
|
||
</div>
|
||
)}
|
||
</div>
|
||
|
||
{/* --- 关联交易 --- */}
|
||
<div className="card" style={{ marginTop: 20 }}>
|
||
<div className="card-header">
|
||
<h3>关联交易</h3>
|
||
<span style={{ fontSize: 13, color: "var(--text-secondary)" }}>
|
||
共 {deals.total} 条,第 {deals.page}/{deals.totalPages} 页
|
||
</span>
|
||
</div>
|
||
|
||
<div className="filter-bar" style={{ padding: "0 20px 12px" }}>
|
||
<div className="form-group">
|
||
<label>开始</label>
|
||
<input
|
||
type="date"
|
||
className="form-input"
|
||
value={dealFilters.start_date}
|
||
onChange={(e) => updateDealFilter("start_date", e.target.value)}
|
||
/>
|
||
</div>
|
||
<div className="form-group">
|
||
<label>结束</label>
|
||
<input
|
||
type="date"
|
||
className="form-input"
|
||
value={dealFilters.end_date}
|
||
onChange={(e) => updateDealFilter("end_date", e.target.value)}
|
||
/>
|
||
</div>
|
||
<div className="form-group">
|
||
<label>方向</label>
|
||
<select
|
||
className="form-select"
|
||
value={dealFilters.direction}
|
||
onChange={(e) => updateDealFilter("direction", e.target.value)}
|
||
>
|
||
<option value="">全部</option>
|
||
<option value="income">收入</option>
|
||
<option value="expense">支出</option>
|
||
</select>
|
||
</div>
|
||
<div className="form-group">
|
||
<label>分类</label>
|
||
<select
|
||
className="form-select"
|
||
value={dealFilters.category}
|
||
onChange={(e) => updateDealFilter("category", e.target.value)}
|
||
>
|
||
<option value="">全部</option>
|
||
{categories.map((c) => (
|
||
<option key={c.id} value={c.name}>
|
||
{c.name}
|
||
</option>
|
||
))}
|
||
</select>
|
||
</div>
|
||
<div className="form-group">
|
||
<label>搜索</label>
|
||
<input
|
||
type="text"
|
||
className="form-input"
|
||
placeholder="对方/描述..."
|
||
value={dealFilters.keyword}
|
||
onChange={(e) => updateDealFilter("keyword", e.target.value)}
|
||
/>
|
||
</div>
|
||
{deals.totalIncome || deals.totalExpense ? (
|
||
<div
|
||
style={{
|
||
alignSelf: "flex-end",
|
||
fontSize: 12,
|
||
color: "var(--text-secondary)",
|
||
whiteSpace: "nowrap",
|
||
paddingBottom: 8,
|
||
marginLeft: "auto",
|
||
}}
|
||
>
|
||
筛选结果 · 收入{" "}
|
||
<span style={{ color: "var(--success)", fontWeight: 600 }}>
|
||
+{(deals.totalIncome || 0).toFixed(2)}
|
||
</span>{" "}
|
||
· 支出{" "}
|
||
<span style={{ color: "var(--danger)", fontWeight: 600 }}>
|
||
-{(deals.totalExpense || 0).toFixed(2)}
|
||
</span>
|
||
</div>
|
||
) : null}
|
||
</div>
|
||
|
||
{dealLoading ? (
|
||
<div className="loading">
|
||
<div className="spinner"></div>加载中...
|
||
</div>
|
||
) : deals.deals.length > 0 ? (
|
||
<>
|
||
<div className="table-container">
|
||
<table>
|
||
<thead>
|
||
<tr>
|
||
<DealTh field="direction">方向</DealTh>
|
||
<DealTh field="id">ID</DealTh>
|
||
<DealTh field="deal_date">日期</DealTh>
|
||
<DealTh field="amount" style={{ textAlign: "right" }}>
|
||
金额
|
||
</DealTh>
|
||
<DealTh field="counterparty">对方</DealTh>
|
||
<th>关联明细</th>
|
||
<DealTh field="category">分类</DealTh>
|
||
<th>编辑</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
{deals.deals.map((d) => (
|
||
<tr key={d.id}>
|
||
<td>
|
||
<span
|
||
className={`tag ${d.direction === "income" || parseFloat(d.amount) >= 0 ? "tag-income" : "tag-expense"}`}
|
||
>
|
||
{d.direction === "income" || parseFloat(d.amount) >= 0
|
||
? "收入"
|
||
: "支出"}
|
||
</span>
|
||
</td>
|
||
<td
|
||
style={{ fontSize: 12, color: "var(--text-secondary)" }}
|
||
>
|
||
{d.id}
|
||
</td>
|
||
<td style={{ whiteSpace: "nowrap", fontSize: 13 }}>
|
||
{fmtDatetime(d.deal_date) || d.deal_date}
|
||
</td>
|
||
<td
|
||
style={{
|
||
textAlign: "right",
|
||
fontWeight: 600,
|
||
whiteSpace: "nowrap",
|
||
color:
|
||
parseFloat(d.amount) >= 0
|
||
? "var(--success)"
|
||
: "var(--danger)",
|
||
}}
|
||
>
|
||
{formatAmount(d.amount)}
|
||
</td>
|
||
<td>{d.counterparty || "--"}</td>
|
||
<td
|
||
style={{ fontSize: 12, color: "var(--text-secondary)" }}
|
||
>
|
||
{d.transactions?.map((t, i) => (
|
||
<span key={i} style={{ marginRight: 8 }}>
|
||
<span
|
||
className={
|
||
"source-type-label source-" + t.source_type
|
||
}
|
||
>
|
||
{SOURCE_TYPE_MAP[t.source_type]}
|
||
</span>{" "}
|
||
{t.description?.substring(0, 20)}
|
||
</span>
|
||
))}
|
||
</td>
|
||
<td>
|
||
{d.category || (
|
||
<span
|
||
style={{
|
||
color: "var(--text-secondary)",
|
||
fontSize: 11,
|
||
}}
|
||
>
|
||
未分类
|
||
</span>
|
||
)}
|
||
</td>
|
||
<td>
|
||
<button
|
||
className="btn btn-secondary"
|
||
style={{ padding: "4px 8px", fontSize: 12 }}
|
||
onClick={() => setEditingDeal(d)}
|
||
>
|
||
编辑
|
||
</button>
|
||
</td>
|
||
</tr>
|
||
))}
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
{deals.totalPages > 1 && (
|
||
<div className="pagination">
|
||
<button
|
||
disabled={deals.page <= 1}
|
||
onClick={() => updateDealFilter("page", deals.page - 1)}
|
||
>
|
||
<ChevronLeft size={14} /> 上一页
|
||
</button>
|
||
{Array.from(
|
||
{ length: Math.min(deals.totalPages, 7) },
|
||
(_, i) => {
|
||
const pg = i + Math.max(1, deals.page - 3);
|
||
if (pg > deals.totalPages) return null;
|
||
return (
|
||
<button
|
||
key={pg}
|
||
className={deals.page === pg ? "active" : ""}
|
||
onClick={() => updateDealFilter("page", pg)}
|
||
>
|
||
{pg}
|
||
</button>
|
||
);
|
||
},
|
||
)}
|
||
<button
|
||
disabled={deals.page >= deals.totalPages}
|
||
onClick={() => updateDealFilter("page", deals.page + 1)}
|
||
>
|
||
下一页 <ChevronRight size={14} />
|
||
</button>
|
||
</div>
|
||
)}
|
||
</>
|
||
) : (
|
||
<div className="empty-state">
|
||
<div className="icon">🔗</div>
|
||
<p>没有找到关联交易</p>
|
||
</div>
|
||
)}
|
||
</div>
|
||
|
||
{/* --- 清空确认弹窗 --- */}
|
||
{clearConfirm && (
|
||
<div className="modal-overlay">
|
||
<div className="modal" style={{ maxWidth: 400 }}>
|
||
<h3 style={{ marginBottom: 12 }}>清空关联交易</h3>
|
||
<p
|
||
style={{
|
||
color: "var(--text-secondary)",
|
||
marginBottom: 20,
|
||
fontSize: 14,
|
||
}}
|
||
>
|
||
确定要清空所有 Deal 关联记录吗?此操作不可撤销。
|
||
</p>
|
||
<div
|
||
style={{ display: "flex", gap: 10, justifyContent: "flex-end" }}
|
||
>
|
||
<button
|
||
className="btn btn-secondary"
|
||
onClick={() => setClearConfirm(false)}
|
||
>
|
||
取消
|
||
</button>
|
||
<button className="btn btn-danger" onClick={handleClearDeals}>
|
||
确认清空
|
||
</button>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
)}
|
||
|
||
{/* --- Toast --- */}
|
||
{toast && (
|
||
<div
|
||
style={{
|
||
position: "fixed",
|
||
top: 24,
|
||
right: 24,
|
||
zIndex: 3000,
|
||
padding: "14px 20px",
|
||
borderRadius: 10,
|
||
background:
|
||
toast.type === "success"
|
||
? "#10b981"
|
||
: toast.type === "error"
|
||
? "#ef4444"
|
||
: "#4f46e5",
|
||
color: "#fff",
|
||
fontSize: 14,
|
||
fontWeight: 500,
|
||
boxShadow: "0 4px 12px rgba(0,0,0,0.2)",
|
||
display: "flex",
|
||
alignItems: "center",
|
||
gap: 10,
|
||
maxWidth: 400,
|
||
}}
|
||
>
|
||
{toast.type === "success" ? (
|
||
<CheckCircle size={18} />
|
||
) : toast.type === "error" ? (
|
||
<AlertCircle size={18} />
|
||
) : (
|
||
<CheckCircle size={18} />
|
||
)}
|
||
<span style={{ flex: 1 }}>{toast.message}</span>
|
||
<button
|
||
onClick={() => setToast(null)}
|
||
style={{
|
||
background: "none",
|
||
border: "none",
|
||
color: "#fff",
|
||
cursor: "pointer",
|
||
opacity: 0.7,
|
||
}}
|
||
>
|
||
<X size={16} />
|
||
</button>
|
||
</div>
|
||
)}
|
||
|
||
{/* --- 编辑 Deal 弹窗 --- */}
|
||
{editingDeal && (
|
||
<div className="modal-overlay">
|
||
<div
|
||
className="modal"
|
||
style={{ maxWidth: 440 }}
|
||
onClick={(e) => e.stopPropagation()}
|
||
>
|
||
<h3 style={{ marginBottom: 12 }}>编辑 Deal #{editingDeal.id}</h3>
|
||
<div
|
||
style={{
|
||
marginBottom: 12,
|
||
fontSize: 13,
|
||
color: "var(--text-secondary)",
|
||
}}
|
||
>
|
||
{editingDeal.deal_date} | {editingDeal.counterparty} |{" "}
|
||
{formatAmount(editingDeal.amount)}
|
||
</div>
|
||
<div className="form-group">
|
||
<label>分类</label>
|
||
<select
|
||
className="form-select"
|
||
value={editingDeal.category || ""}
|
||
onChange={(e) =>
|
||
setEditingDeal({ ...editingDeal, category: e.target.value })
|
||
}
|
||
>
|
||
<option value="">选择分类</option>
|
||
{categories.map((c) => (
|
||
<option key={c.id} value={c.name}>
|
||
{c.name}
|
||
</option>
|
||
))}
|
||
</select>
|
||
</div>
|
||
<div className="form-group">
|
||
<label>描述</label>
|
||
<input
|
||
type="text"
|
||
className="form-input"
|
||
value={editingDeal.ai_description || ""}
|
||
onChange={(e) =>
|
||
setEditingDeal({
|
||
...editingDeal,
|
||
ai_description: e.target.value,
|
||
})
|
||
}
|
||
placeholder="交易描述..."
|
||
/>
|
||
</div>
|
||
<div
|
||
style={{
|
||
display: "flex",
|
||
gap: 10,
|
||
justifyContent: "flex-end",
|
||
marginTop: 20,
|
||
}}
|
||
>
|
||
<button
|
||
className="btn btn-secondary"
|
||
onClick={() => setEditingDeal(null)}
|
||
>
|
||
取消
|
||
</button>
|
||
<button className="btn btn-primary" onClick={handleUpdateDeal}>
|
||
保存
|
||
</button>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
)}
|
||
</div>
|
||
);
|
||
}
|