"use client"; import { Pencil, Plus, Trash2, Network } from "lucide-react"; import { useTranslation } from "react-i18next"; import { AdminSubnav, AdminSubnavButton } from "@/components/admin/admin-subnav"; import { AdminNoResourceState, AdminTableNoResourceRow } from "@/components/admin/admin-no-resource-state"; import { AdminLoadingInline } from "@/components/admin/admin-loading-state"; import { AdminStatusBadge } from "@/components/admin/admin-status-badge"; import { AdminRowActionsMenu } from "@/components/admin/admin-row-actions-menu"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import { AgentsPlayersPanel } from "@/modules/agents/agents-players-panel"; import { AgentProfileFields, type AgentProfileFieldsProps } from "@/modules/agents/agent-profile-fields"; import { formatCredit } from "@/modules/agents/agent-line-sidebar"; import { Button } from "@/components/ui/button"; import { percentValueToUi } from "@/lib/admin-rate-percent"; import { resolveRoleStatusTone } from "@/lib/admin-status-tone"; import { AdminTableMoney, adminMoneyCellClassName } from "@/components/admin/admin-table-money"; import type { AgentNodeRow, AgentProfileRow } from "@/types/api/admin-agent"; export type AgentDetailTab = "profile" | "downline" | "players"; export type AgentLineDetailPanelProps = { node: AgentNodeRow | null; profile: AgentProfileRow | null; profileLoading: boolean; profileError?: string | null; childAgents: AgentNodeRow[]; childCountById: Map; directPlayerCount?: number; onDirectPlayerCountChange?: (count: number) => void; siteCode: string; parentName: string | null; detailTab: AgentDetailTab; onDetailTabChange: (tab: AgentDetailTab) => void; canViewProfileTab: boolean; canEditProfileTab: boolean; canSaveProfileTab: boolean; canViewDownlineTab: boolean; canViewPlayersTab: boolean; canManageNode: boolean; canCreateChild: boolean; canCreatePlayerAction: boolean; canDeleteChild: (node: AgentNodeRow) => boolean; onEditChild: (node: AgentNodeRow) => void; onDeleteChild: (node: AgentNodeRow) => void; onAddChild: () => void; onEditCurrent: () => void; onSelectChild: (node: AgentNodeRow) => void; profileFields: AgentProfileFieldsProps | null; profileSaving: boolean; onSaveProfile: () => void; }; export function AgentLineDetailPanel({ node, profile, profileLoading, profileError = null, childAgents, childCountById, directPlayerCount, onDirectPlayerCountChange, siteCode, parentName, detailTab, onDetailTabChange, canViewProfileTab, canEditProfileTab, canSaveProfileTab, canViewDownlineTab, canViewPlayersTab, canManageNode, canCreateChild, canCreatePlayerAction, canDeleteChild, onEditChild, onDeleteChild, onAddChild, onEditCurrent, onSelectChild, profileFields, profileSaving, onSaveProfile, }: AgentLineDetailPanelProps): React.ReactElement { const { t } = useTranslation(["agents", "common"]); if (node === null) { return (

{t("lineUi.selectAgent", { defaultValue: "选择左侧代理" })}

); } const tabs: { key: AgentDetailTab; label: string; count?: number; visible: boolean }[] = [ { key: "profile", label: t("lineUi.tabProfileShort", { defaultValue: "占成授信" }), visible: canViewProfileTab, }, { key: "downline", label: t("lineUi.tabDownline", { defaultValue: "直属下级" }), count: childAgents.length, visible: canViewDownlineTab, }, { key: "players", label: t("lineUi.tabPlayers", { defaultValue: "直属玩家" }), count: directPlayerCount, visible: canViewPlayersTab, }, ]; const metaParts = [ parentName ? t("lineUi.parentMeta", { defaultValue: "上级 {{name}}", name: parentName }) : null, node.username?.trim() ? node.username : node.code, ].filter(Boolean); return (

{node.name}

{node.status === 1 ? t("common:status.enabled", { defaultValue: "启用" }) : t("common:status.disabled", { defaultValue: "停用" })}
{metaParts.length > 0 ? (

{metaParts.join(" · ")}

) : null}
{canManageNode ? ( ) : null}
{tabs .filter((tab) => tab.visible) .map((tab) => ( onDetailTabChange(tab.key)} count={tab.count} > {tab.label} ))}
{profileError ? (

{profileError}

) : null} {profileLoading && detailTab === "profile" ? ( ) : null} {detailTab === "profile" && canViewProfileTab && profileFields && !profileLoading ? (
{canSaveProfileTab ? (
) : null}
) : null} {detailTab === "downline" && canViewDownlineTab ? ( ) : null} {detailTab === "players" && canViewPlayersTab ? ( ) : null}
); } function AgentHeaderMetrics({ profile, loading, }: { profile: AgentProfileRow | null; loading: boolean; }): React.ReactElement { const { t } = useTranslation("agents"); if (loading) { return

; } const items = [ `${t("profile.totalShareRate", { defaultValue: "占成" })} ${profile?.total_share_rate ?? 0}%`, `${t("profile.creditLimit", { defaultValue: "授信" })} ${formatCredit(profile?.credit_limit ?? 0)}`, `${t("lineUi.availableCredit", { defaultValue: "可下发" })} ${formatCredit(profile?.available_credit ?? 0)}`, `${t("profile.rebateLimit", { defaultValue: "回水上限" })} ${percentValueToUi(profile?.rebate_limit ?? 0)}%`, ]; return (

{items.join(" · ")}

); } function DownlineSection({ childAgents, childCountById, canManageNode, canCreateChild, canDeleteChild, onEditChild, onDeleteChild, onSelectChild, onAddChild, }: { childAgents: AgentNodeRow[]; childCountById: Map; canManageNode: boolean; canCreateChild: boolean; canDeleteChild: (node: AgentNodeRow) => boolean; onEditChild: (node: AgentNodeRow) => void; onDeleteChild: (node: AgentNodeRow) => void; onSelectChild: (node: AgentNodeRow) => void; onAddChild: () => void; }): React.ReactElement { const { t } = useTranslation(["agents", "common"]); if (childAgents.length === 0) { return ( {canManageNode && canCreateChild ? ( ) : null} ); } return (
{canManageNode && canCreateChild ? (
) : null}
); } function DownlineTable({ childAgents, childCountById, canManageNode, canDeleteChild, onEditChild, onDeleteChild, onSelectChild, }: { childAgents: AgentNodeRow[]; childCountById: Map; canManageNode: boolean; canDeleteChild: (node: AgentNodeRow) => boolean; onEditChild: (node: AgentNodeRow) => void; onDeleteChild: (node: AgentNodeRow) => void; onSelectChild: (node: AgentNodeRow) => void; }): React.ReactElement { const { t } = useTranslation(["agents", "common"]); return (
{t("agentName", { defaultValue: "名称" })} {t("loginUsername", { defaultValue: "登录名" })} {t("profile.totalShareRate", { defaultValue: "占成" })} {t("profile.creditLimit", { defaultValue: "授信" })} {t("lineUi.availableCredit", { defaultValue: "可下发" })} {t("lineUi.downlineColumns.downlineCount", { defaultValue: "下级" })} {t("common:status.label", { defaultValue: "状态" })} {canManageNode ? ( {t("common:table.actions", { defaultValue: "操作" })} ) : null} {childAgents.map((child) => { const summary = child.profile_summary; return ( onSelectChild(child)} >
{child.name}
{child.code}
{child.username ?? "—"} {summary ? `${summary.total_share_rate ?? 0}%` : "—"} {summary ? {formatCredit(summary.credit_limit)} : "—"} {summary ? ( {formatCredit(summary.available_credit)} ) : ( "—" )} {childCountById.get(child.id) ?? 0} {child.status === 1 ? t("common:status.enabled", { defaultValue: "启用" }) : t("common:status.disabled", { defaultValue: "停用" })} {canManageNode ? ( e.stopPropagation()} > onEditChild(child), }, { key: "delete", label: t("lineUi.deleteDownline", { defaultValue: "删除" }), icon: Trash2, destructive: true, disabled: !canDeleteChild(child), onClick: () => onDeleteChild(child), }, ]} /> ) : null}
); })}
); }