import { detailLinkState } from "../lib/detail-navigation"; import { isImeComposing } from "../lib/ime"; import { PageIntro } from "../components/PageIntro"; import { OpsDialog, OpsNotice } from "../components/OperationsKit"; import { FilterSelect } from "../components/ui/filter-select"; import { DateRangeFilter } from "../components/ui/date-range-filter"; import { TicketState } from "../components/ticketState"; import { WorkOrderLedgerList } from "../components/work-order/WorkOrderLedgerList"; import { useEffect, useId, useRef, useState, type FormEvent, type KeyboardEvent } from "react"; import { ArrowRight, ClipboardCheck, ClipboardList, Clock3, Headphones, Inbox, Plus, Search, Wrench, X, } from "lucide-react"; import { Button } from "../components/ui/button"; import { Input } from "../components/ui/input"; import { SearchInput } from "../components/ui/search-input"; import { FieldLabel } from "../components/ui/field-label"; import { Link, useLocation, useNavigate, useSearchParams } from "react-router"; import { customerGetList, orderGetList, rowsOf } from "../lib/api/bos"; import { ticketCreate, TICKET_STATUSES, TICKET_TYPES, typeLabel, statusFilterLabel, } from "../lib/api/ticket"; import { classifyTicketError, TICKET_PAGE_SIZE, useTicketList, useTodoCounts } from "../lib/ticket"; import { useTicketStatusCounts } from "../lib/work-order-counts"; import { useSubmit } from "../lib/useLoad"; import { fetchAllPages } from "../lib/fetch-all-pages"; import { LIST_PAGE_SIZE } from "../lib/list-paging"; import { useDemoIdentity } from "../lib/demo-session"; import { ListPager } from "../components/ListPager"; import "../styles/operations.css"; import "../styles/orders.css"; import "../styles/work-orders.css"; import "../styles/work-order-ledger.css"; /** 状态页签按处理阶段排列,终态放最后。 */ const TAB_ORDER = ["unassigned", "pending", "processing", "completed", "rejected", "settled", "terminated"] as const; /** * `completed` 在跨类型视图(状态 Tab/筛选器/当前视图 chip)里只写短名「待确认」 * (业主 2026-11-18 二次定案:两半全名拼起来撑破 Tab 行):两类行内叫法 * 「已完成待确认」(主线,等运营商回执)/「已处理待确认」(故障、售后,等提交方确认) * 留在徽标与这行悬停说明里,⛔ 在筛选维度上只写其中一半。 */ const COMPLETED_SCOPE_HINT = "同一状态码 completed,两类都不是终态:主线=已完成待确认(等运营商回执);故障/售后=已处理待确认(等提交方确认)"; /** * 代客建单(`/ticket/create@v1`)。 * * 客户与订阅都从真接口选(⛔ 手输 ID:建单口的 `customer_id` 要过可见性判定, * 手输别人的客户只会拿到 `11104`)。建单口只收故障/售后两类(create.rs `CREATE_TYPES`), * 开户/到期停机/服务开通由对应业务动作生成,在此提交会被服务端拒。 */ const CREATABLE = [ { code: "fault", label: "故障报修", Icon: Wrench }, { code: "after_sale", label: "售后服务", Icon: Headphones }, ] as const; type TicketCustomer = { customer_id: string; customer_name: string | null }; type TicketLookupState = "loading" | "ready" | "error"; function TicketCreateDialog({ onClose, onCreated }: { onClose: () => void; onCreated: (message: string) => void }) { const id = useId(); const customerTrigger = useRef(null); const searchInput = useRef(null); const customerList = useRef(null); const submit = useSubmit(); const [ticketType, setTicketType] = useState<(typeof CREATABLE)[number]["code"]>("fault"); const [keyword, setKeyword] = useState(""); const [customer, setCustomer] = useState(null); const [orderId, setOrderId] = useState(""); const [title, setTitle] = useState(""); const [detail, setDetail] = useState(""); const [customers, setCustomers] = useState([]); const [customerLookup, setCustomerLookup] = useState("loading"); const [customerOpen, setCustomerOpen] = useState(false); const [activeCustomer, setActiveCustomer] = useState(0); const [subscriptions, setSubscriptions] = useState<{ value: string; label: string }[]>([]); const [subscriptionLookup, setSubscriptionLookup] = useState("loading"); const [invalid, setInvalid] = useState<"customer" | "title" | null>(null); const customerId = customer?.customer_id ?? ""; const customerPopupId = `${id}-customer-picker`; const customerOptionListId = `${id}-customer-options`; const customerHints = { loading: "正在查找客户…", error: "客户读取失败,请重新搜索", ready: "没有找到客户" }; const subscriptionHints = { loading: "正在读取订阅…", error: "订阅读取失败", ready: subscriptions.length ? "不关联订阅" : "该客户暂无订阅" }; const subscriptionPlaceholder = customerId ? subscriptionHints[subscriptionLookup] : "请先选择归属客户"; const selectedSubscription = subscriptions.find((item) => item.value === orderId); function pickCustomer(next: TicketCustomer | null) { closeCustomerPicker(); if (next?.customer_id === customer?.customer_id) return; setCustomer(next); setOrderId(""); setSubscriptions([]); setSubscriptionLookup("loading"); if (invalid === "customer") setInvalid(null); } function searchCustomers(value: string) { setActiveCustomer(0); if (value === keyword) return; setKeyword(value); setCustomers([]); setCustomerLookup("loading"); } function openCustomerPicker() { searchCustomers(""); setCustomerOpen(true); } function closeCustomerPicker() { setCustomerOpen(false); customerTrigger.current?.focus({ preventScroll: true }); } function handleCustomerKeys(event: KeyboardEvent) { if (isImeComposing(event)) return; if (event.key === "ArrowDown" || event.key === "ArrowUp") { event.preventDefault(); if (!customers.length) return; const step = event.key === "ArrowDown" ? 1 : -1; const next = (activeCustomer + step + customers.length) % customers.length; setActiveCustomer(next); customerList.current?.children[next]?.scrollIntoView({ block: "nearest" }); } else if (event.key === "Enter") { event.preventDefault(); const next = customers[activeCustomer]; if (next) pickCustomer(next); } } useEffect(() => { customerTrigger.current?.focus({ preventScroll: true }); }, []); useEffect(() => { if (customerOpen) searchInput.current?.focus({ preventScroll: true }); }, [customerOpen]); useEffect(() => { let alive = true; const timer = window.setTimeout(async () => { try { const r = await customerGetList({ page: 0, page_size: 20, keyword: keyword.trim() || undefined }); if (!alive) return; setCustomers(rowsOf(r)); setCustomerLookup("ready"); } catch { if (!alive) return; setCustomers([]); setCustomerLookup("error"); } }, 300); return () => { alive = false; window.clearTimeout(timer); }; }, [keyword]); useEffect(() => { if (!customerId) return; let alive = true; fetchAllPages((page, page_size) => orderGetList({ customer_id: customerId, page, page_size }), { pageSize: LIST_PAGE_SIZE }) .then((r) => { if (!alive) return; setSubscriptions(r.items.map((item) => ({ value: item.subscription_id, label: [item.plan_name ?? item.subscription_id, item.contract_no].filter(Boolean).join(" · "), }))); setSubscriptionLookup("ready"); }) .catch(() => { if (alive) setSubscriptionLookup("error"); }); return () => { alive = false; }; }, [customerId]); async function submitForm(event: FormEvent) { event.preventDefault(); if (submit.busy) return; submit.clear(); if (!customerId) { setInvalid("customer"); customerTrigger.current?.focus(); return; } if (title.trim().length < 2) { setInvalid("title"); return; } setInvalid(null); await submit.run(() => ticketCreate({ ticket_type: ticketType, customer_id: customerId, title: title.trim(), detail: detail.trim() || undefined, subscription_id: orderId || undefined, }).then((r) => onCreated(`工单 ${r.ticket_no} 已创建。`))); } const failure = submit.error ? classifyTicketError(submit.error) : null; const error = invalid === "customer" ? "请选择归属客户。" : invalid === "title" ? "事项标题至少填写 2 个字。" : failure?.message; return
{ if (!event.currentTarget.contains(event.relatedTarget)) setCustomerOpen(false); }}> 归属客户
{customerOpen && }
工单类型
{CREATABLE.map(({ code, label, Icon }) => )}
{ setTitle(event.target.value); if (invalid === "title") setInvalid(null); }} />