import { useCallback, useEffect, useState } from 'react'; import { useNavigate, useParams } from 'react-router-dom'; import { Button, Card, Descriptions, Empty, Input, Popconfirm, Select, Space, Spin, Statistic, Table, Tag, Tooltip, Typography, } from 'antd'; import type { TableProps } from 'antd'; import { ArrowLeftOutlined, EditOutlined, ReloadOutlined, } from '@ant-design/icons'; import { accountApi, type AccountEventItem, type AccountOverview } from '../api/modules'; import { getErrorMessage } from '../utils/error'; import { message } from '../utils/antdMessage'; import { formatTime } from '../utils/time'; const { Text, Title } = Typography; /** * 事件类型 → 展示样式。 * 颜色用来一眼区分「钱进来 / 分出去 / 状态变化」三类,而不是按类型各给一个颜色。 */ const EVENT_STYLE: Record = { imported: { label: '导入', color: 'default' }, login_ok: { label: '登录', color: 'blue' }, ck_invalid: { label: 'CK 失效', color: 'red' }, ck_cleared: { label: 'CK 已清除', color: 'red' }, relogin_ok: { label: '重登成功', color: 'green' }, relogin_failed: { label: '重登转人工', color: 'orange' }, status_changed: { label: '状态变更', color: 'geekblue' }, bind_ok: { label: '绑定', color: 'cyan' }, recharge: { label: '充值', color: 'green' }, exchange: { label: '兑换', color: 'purple' }, points_credit: { label: '积分到账', color: 'gold' }, elite_opened: { label: '精英宝典', color: 'magenta' }, esports_opened: { label: '电竞手册', color: 'volcano' }, assigned: { label: '分配', color: 'default' }, tag_changed: { label: '标签', color: 'default' }, remark_changed: { label: '备注', color: 'default' }, }; /** 事件粗分类(与后端 `account_events_service.CATEGORY_*` 对应)。 */ const CATEGORY_OPTIONS = [ { value: 'lifecycle', label: '状态变迁' }, { value: 'auth', label: '登录与 CK' }, { value: 'business', label: '业务流水' }, { value: 'ops', label: '人工操作' }, ]; /** 生命周期状态 → 展示色(与账号列表页保持一致,取值域见后端 account_lifecycle)。 */ const STATUS_TONES: Record = { imported: 'default', active: 'green', invalid: 'red', manual: 'orange', disabled: 'default', archived: 'default', }; function eventTag(type: string) { const style = EVENT_STYLE[type] ?? { label: type, color: 'default' }; return {style.label}; } /** 金额展示:充值为正(绿色 ¥),兑换消耗为负(红色积分)。 */ function amountText(item: AccountEventItem) { if (item.amount === null || item.amount === undefined) return null; if (item.event_type === 'recharge') { return +¥{item.amount}; } if (item.event_type === 'exchange') { return {item.amount} 分; } return ( = 0 ? 'success' : 'danger'}> {item.amount > 0 ? '+' : ''} {item.amount} ); } export default function AccountDetailPage() { const { id } = useParams<{ id: string }>(); const accountId = Number(id); const navigate = useNavigate(); const [overview, setOverview] = useState(null); const [loading, setLoading] = useState(true); const [events, setEvents] = useState([]); const [eventTotal, setEventTotal] = useState(0); const [page, setPage] = useState(1); const [pageSize, setPageSize] = useState(20); const [eventType, setEventType] = useState(''); const [category, setCategory] = useState(''); const [eventsLoading, setEventsLoading] = useState(false); const [remarkEditing, setRemarkEditing] = useState(false); const [remarkDraft, setRemarkDraft] = useState(''); const loadOverview = useCallback(async () => { if (!accountId) return; setLoading(true); try { setOverview(await accountApi.overview(accountId)); } catch (e: unknown) { message.error(getErrorMessage(e)); } finally { setLoading(false); } }, [accountId]); const loadEvents = useCallback(async () => { if (!accountId) return; setEventsLoading(true); try { const data = await accountApi.events(accountId, { page, page_size: pageSize, event_type: eventType, category, }); setEvents(data.items); setEventTotal(data.total); } catch (e: unknown) { message.error(getErrorMessage(e)); } finally { setEventsLoading(false); } }, [accountId, page, pageSize, eventType, category]); useEffect(() => { void loadOverview(); }, [loadOverview]); useEffect(() => { void loadEvents(); }, [loadEvents]); const columns: TableProps['columns'] = [ { title: '时间', dataIndex: 'occurred_at', width: 165, render: (val: string) => formatTime(val), }, { title: '类型', dataIndex: 'event_type', width: 100, render: (val: string) => eventTag(val), }, { title: '说明', dataIndex: 'title', ellipsis: true }, { title: '金额 / 变动', dataIndex: 'amount', width: 110, align: 'right', render: (_val, record) => amountText(record) ?? -, }, { title: '变动后余额', dataIndex: 'balance_after', width: 110, align: 'right', render: (val: number | null) => val === null || val === undefined ? - : val, }, ]; if (!accountId) { return ; } const stats = overview?.stats; const status = overview?.status || ''; /** 人工切换生命周期状态:disabled / archived / auto(恢复自动推导)。 */ const handleLifecycle = async (next: 'disabled' | 'archived' | 'auto') => { if (!accountId) return; try { await accountApi.setLifecycle(accountId, next); message.success('状态已更新'); void loadOverview(); void loadEvents(); } catch (e: unknown) { message.error(getErrorMessage(e)); } }; /** 保存备注:备注是自由文本,走单个接口(标签那种短枚举才有批量)。 */ const handleSaveRemark = async () => { if (!accountId) return; try { await accountApi.setRemark(accountId, remarkDraft.trim()); message.success('备注已更新'); setRemarkEditing(false); void loadOverview(); void loadEvents(); } catch (e: unknown) { message.error(getErrorMessage(e)); } }; const handleAutomation = async (autoManaged: boolean) => { if (!accountId) return; try { await accountApi.setAutomation(accountId, autoManaged); message.success(autoManaged ? '已恢复自动化' : '已设为不参与自动化'); void loadOverview(); } catch (e: unknown) { message.error(getErrorMessage(e)); } }; return (
{status === 'disabled' || status === 'archived' ? ( void handleLifecycle('auto')} > ) : ( <> void handleLifecycle('disabled')} > void handleLifecycle('archived')} > )} {overview?.auto_managed === false ? ( ) : ( void handleAutomation(false)} > )} {overview?.username || '—'} {overview?.status_label || '—'} {overview?.tag && {overview.tag}} {overview?.auto_managed === false && 不参与自动化} } style={{ marginBottom: 12 }} > {overview?.nickname || '-'} {overview?.uid || '-'} {formatTime(overview?.created_at)} {overview?.game_name || '-'} {overview?.game_channel ? ({overview.game_channel}) : null} {overview?.bind_status || '-'} {remarkEditing ? ( setRemarkDraft(e.target.value)} onPressEnter={() => void handleSaveRemark()} /> ) : ( {overview?.remark || '-'}