import { useBusinessView } from "../lib/use-business-view"; import { PageIntro } from "../components/PageIntro"; import { useState, type ReactNode } from "react"; import { ArrowLeft, ArrowRight, CalendarPlus, PencilLine, Plus, RefreshCw, Trash2 } from "lucide-react"; import { Link, useLocation, useNavigate, useParams } from "react-router"; import { Button } from "../components/ui/button"; import { OpsMore, OpsNotice, OpsStatus, type OpsMoreItem } from "../components/OperationsKit"; import { Tri } from "../components/triState"; import { orderView, useOrders, type OrderView } from "../lib/order-demo"; import { WORD_FETCH_FAILED, WORD_NOT_OPENED, WORD_UNCONFIGURED } from "../lib/api/lockedCopy"; import { rowsOf, orderDelete, orderGetList, customerGet } from "../lib/api/bos"; import { ACCT_STATUS, label } from "../lib/api/labels"; import { useLoad } from "../lib/useLoad"; import { OrderEditor, type OrderEditorContext } from "../components/OrderChangeDialogs"; import { detailBack, detailLinkState } from "../lib/detail-navigation"; import { useHorizontalOverflow } from "../lib/use-horizontal-overflow"; import { ListPager } from "../components/ListPager"; import { LIST_PAGE_SIZE, useLocalPage } from "../lib/list-paging"; import "../styles/operations.css"; const statusTone = (status: string) => status === "生效中" ? "success" : status === "提前停机" ? "danger" : status === "已到期" ? "neutral" : "warning"; /** 三态文本格:键缺失=取数失败,null=未配置,⛔ 用 0/「—」冒充有值。 */ function cell(value: string | null | undefined, missing: string = WORD_UNCONFIGURED) { if (value === undefined) return ; if (value === null || value === "") return ; return value; } const moneyView = (c: { tone: "val" | "missing" | "fail"; text: string }) => (c.tone === "val" ? c.text : ); const exitsView = (exits: string[] | null | undefined) => (exits === undefined ? : exits === null || !exits.length ? : exits.join("、")); export function CustomerOrdersPage() { const view = useBusinessView([]); const location = useLocation(); const navigate = useNavigate(); const { no } = useParams(); const param = decodeURIComponent(no ?? ""); const back = detailBack(location.state, param ? `/customers/${encodeURIComponent(param)}` : "/customers", "返回客户档案"); const outgoingState = detailLinkState(location.pathname, location.state); const tableOverflow = useHorizontalOverflow(); /** * 客户档案改读真实接口(`/customer/get@v1`)。 * * ⚠️ 2026-11-18 修(巡检 F-1):此前从 `useBusinessView` 的演示客户 store 反查 ⇒ 页头 * 「客户账户」恒「未配置」、且不渲染「查看客户档案」链接,尽管订阅行都正常。 * 该口是客户侧自读口,行集由后端按主体收窄;运营侧(admin/运主/运子)同样可读。 */ const account = useLoad(() => (param ? customerGet({ customer_id: param }) : Promise.resolve(null)), [param]); const profile = account.data?.customer ?? null; const customerName = profile?.customer_name ?? null; /** 按客户 `get_list` 服务端分页,⛔ page_size=200 再本地切。 */ const customerIdForList = profile?.customer_id || param; const [listPage, setListPage] = useState(0); const orders = useOrders(customerIdForList || undefined, { page: listPage, pageSize: LIST_PAGE_SIZE }); const activeProbe = useLoad( () => (customerIdForList ? orderGetList({ customer_id: customerIdForList, status: "active", page: 0, page_size: 1 }) : Promise.resolve(null)), [customerIdForList], ); const rows = orders.data === undefined || orders.data === null ? [] : rowsOf(orders.data.items).map(orderView); const listTotal = Number(orders.data?.total ?? rows.length); const customerId = rows[0]?.customerId || profile?.customer_id || param; const [context, setContext] = useState(null); const [confirm, setConfirm] = useState(null); const [deleteBlocked, setDeleteBlocked] = useState(""); const [pendingDelete, setPendingDelete] = useState(false); const [notice, setNotice] = useState(""); const paging = useLocalPage(listTotal, LIST_PAGE_SIZE, listPage); const pageRows = rows; if (orders.loading) return
{back.label}

正在从订阅接口读取该客户的订阅…

; if (orders.error) return
{back.label}

订阅取数失败

{orders.error.message}

; const active = Number(activeProbe.data?.total ?? 0); /** 「分账号额度」汇总=本页订阅行 `quota_slots` 之和(全量汇总需另口;本页如实标本页)。 */ const quotaRows = rows.filter((item) => typeof item.quotaSlots === "number"); const quotaSummary = quotaRows.length ? quotaRows.reduce((sum, item) => sum + (item.quotaSlots ?? 0), 0) : ; async function removeOrder() { if (!confirm) return; setPendingDelete(true); setDeleteBlocked(""); try { const result = await orderDelete({ subscription_id: confirm.no }); if (result && result.deleted === false) setDeleteBlocked("服务端未删除本条订阅(应答未给出原因);是否仍被引用由服务端判定,页面不自行放行。"); else { setConfirm(null); setNotice("订阅已删除。"); orders.reload(); } } catch (cause) { setDeleteBlocked((cause as Error).message); } finally { setPendingDelete(false); } } return
{back.label}

套餐订阅管理

{view.readOnly ? "核对这家客户订了哪些套餐,以及服务何时到期。" : "客户需要开通新服务、调整套餐或叠加订阅时,在这里办理。"}

{customerName ?? cell(rows[0]?.customer)} · {customerId}

{!view.readOnly && }
客户账户{profile ? {label(ACCT_STATUS, profile.acct_status)} : account.error ? : account.loading ? 读取中… : }
全部订阅{listTotal}
生效中{active}
分账号额度{quotaSummary}
{profile && 查看客户档案 }
{Array.from({ length: 9 }, (_, index) => )}{pageRows.map((item) => { const canUpdown = item.statusLabel === "生效中"; const orderActions: { key: string; label: string; icon: ReactNode; danger?: boolean; info?: boolean; show: boolean; onSelect: () => void }[] = [ { key: "view", label: "详情", icon: null, info: false, show: true, onSelect: () => navigate(`/orders/${encodeURIComponent(item.no)}`, { state: outgoingState }) }, { key: "updown", label: "升降配", icon: , show: canUpdown, onSelect: () => setContext({ mode: "updown", record: item }) }, // D-03:叠加=新建并存订阅(原订阅不动),走 create@v1;⛔ 再开「就地改结束日」的 StackEditor。 { key: "stack", label: "叠加", icon: , show: canUpdown, onSelect: () => setContext({ mode: "new" }) }, { key: "edit", label: "编辑", icon: , show: item.editable === true, onSelect: () => setContext({ mode: "edit", record: item }) }, { key: "delete", label: "删除", icon: , danger: true, show: true, onSelect: () => { setDeleteBlocked(""); setConfirm(item); } }, ]; const shown = orderActions.filter((action) => action.show && (!view.readOnly || action.key === "view")); /** * 行内只留「详情」;升降配/叠加改收进「更多」菜单——台账一行九列, * 三颗按钮直出会把操作列挤到换行,且升降配要动合同与档位,误点成本高。 */ const visible = shown.filter((action) => action.key === "view"); const moreItems: OpsMoreItem[] = shown.filter((action) => action.key !== "view").map(({ key, label, icon, onSelect, danger }) => ({ key, label, icon, onSelect, ...(danger ? { title: "删除订阅(服务端按引用关系判定)" } : {}) })); return ; })}
套餐 / 合同 / 订单号线路出口方式状态合同期间订阅价格月结算标准实际开通日操作
{cell(item.planName)}{cell(item.contractNo)} · {cell(item.orderNo, "无订单号")}{exitsView(item.exits)}{cell(item.modeLabel)}{item.statusLabel ? {item.statusLabel} : }{item.replacedBy && 已被 {item.replacedBy.subscription_id} 替换}{cell(item.start)}至 {cell(item.end)}{moneyView(item.annual)}{moneyView(item.settle)}{item.openedOn === undefined ? : item.openedOn || }
{visible.map((action) => )}
{!rows.length && !orders.loading &&

还没有订阅

先为客户登记一份合同,再新增套餐订阅。

}
{context && setContext(null)} onSaved={(message) => { setContext(null); setNotice(message); setListPage(0); orders.reload(); }} />} {confirm &&

删除订阅?

{confirm.planName ?? WORD_UNCONFIGURED} · {confirm.no}。是否可删除由服务端按引用关系判定,页面不自行放行。

{deleteBlocked &&

{deleteBlocked}

}
} {notice && setNotice("")} />}
; }