|
|
@@ -8,7 +8,7 @@ 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, ChevronDown, ClipboardCheck, ClipboardList, Clock3, Headphones, Inbox, Plus, Search, Wrench, X,
|
|
|
+ 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";
|
|
|
@@ -59,6 +59,7 @@ type TicketLookupState = "loading" | "ready" | "error";
|
|
|
|
|
|
function TicketCreateDialog({ onClose, onCreated }: { onClose: () => void; onCreated: (message: string) => void }) {
|
|
|
const id = useId();
|
|
|
+ const customerTrigger = useRef<HTMLButtonElement>(null);
|
|
|
const searchInput = useRef<HTMLInputElement>(null);
|
|
|
const customerList = useRef<HTMLUListElement>(null);
|
|
|
const submit = useSubmit();
|
|
|
@@ -72,26 +73,29 @@ function TicketCreateDialog({ onClose, onCreated }: { onClose: () => void; onCre
|
|
|
const [customerLookup, setCustomerLookup] = useState<TicketLookupState>("loading");
|
|
|
const [customerOpen, setCustomerOpen] = useState(false);
|
|
|
const [activeCustomer, setActiveCustomer] = useState(0);
|
|
|
- const [orders, setSubs] = useState<{ subscription_id: string; plan_name: string | null; contract_no: string | null }[]>([]);
|
|
|
+ const [subscriptions, setSubscriptions] = useState<{ value: string; label: string }[]>([]);
|
|
|
const [subscriptionLookup, setSubscriptionLookup] = useState<TicketLookupState>("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: orders.length ? "不关联订阅" : "该客户暂无订阅" };
|
|
|
+ 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);
|
|
|
- setCustomerOpen(!next);
|
|
|
setOrderId("");
|
|
|
- setSubs([]);
|
|
|
+ setSubscriptions([]);
|
|
|
setSubscriptionLookup("loading");
|
|
|
if (invalid === "customer") setInvalid(null);
|
|
|
}
|
|
|
|
|
|
function searchCustomers(value: string) {
|
|
|
- setCustomerOpen(true);
|
|
|
setActiveCustomer(0);
|
|
|
if (value === keyword) return;
|
|
|
setKeyword(value);
|
|
|
@@ -99,30 +103,34 @@ function TicketCreateDialog({ onClose, onCreated }: { onClose: () => void; onCre
|
|
|
setCustomerLookup("loading");
|
|
|
}
|
|
|
|
|
|
+ function openCustomerPicker() {
|
|
|
+ searchCustomers("");
|
|
|
+ setCustomerOpen(true);
|
|
|
+ }
|
|
|
+
|
|
|
+ function closeCustomerPicker() {
|
|
|
+ setCustomerOpen(false);
|
|
|
+ customerTrigger.current?.focus({ preventScroll: true });
|
|
|
+ }
|
|
|
+
|
|
|
function handleCustomerKeys(event: KeyboardEvent<HTMLInputElement>) {
|
|
|
- if (customer || isImeComposing(event)) return;
|
|
|
+ if (isImeComposing(event)) return;
|
|
|
if (event.key === "ArrowDown" || event.key === "ArrowUp") {
|
|
|
event.preventDefault();
|
|
|
- setCustomerOpen(true);
|
|
|
if (!customers.length) return;
|
|
|
const step = event.key === "ArrowDown" ? 1 : -1;
|
|
|
- const next = customerOpen
|
|
|
- ? (activeCustomer + step + customers.length) % customers.length
|
|
|
- : step === 1 ? 0 : customers.length - 1;
|
|
|
+ const next = (activeCustomer + step + customers.length) % customers.length;
|
|
|
setActiveCustomer(next);
|
|
|
customerList.current?.children[next]?.scrollIntoView({ block: "nearest" });
|
|
|
- } else if (customerOpen && event.key === "Enter") {
|
|
|
+ } else if (event.key === "Enter") {
|
|
|
event.preventDefault();
|
|
|
const next = customers[activeCustomer];
|
|
|
if (next) pickCustomer(next);
|
|
|
- } else if (customerOpen && event.key === "Escape") {
|
|
|
- event.preventDefault();
|
|
|
- event.stopPropagation();
|
|
|
- setCustomerOpen(false);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- useEffect(() => { searchInput.current?.focus({ preventScroll: true }); }, []);
|
|
|
+ useEffect(() => { customerTrigger.current?.focus({ preventScroll: true }); }, []);
|
|
|
+ useEffect(() => { if (customerOpen) searchInput.current?.focus({ preventScroll: true }); }, [customerOpen]);
|
|
|
|
|
|
useEffect(() => {
|
|
|
let alive = true;
|
|
|
@@ -147,7 +155,10 @@ function TicketCreateDialog({ onClose, onCreated }: { onClose: () => void; onCre
|
|
|
fetchAllPages((page, page_size) => orderGetList({ customer_id: customerId, page, page_size }), { pageSize: LIST_PAGE_SIZE })
|
|
|
.then((r) => {
|
|
|
if (!alive) return;
|
|
|
- setSubs(r.items.map((s) => ({ subscription_id: s.subscription_id, plan_name: s.plan_name, contract_no: s.contract_no ?? null })));
|
|
|
+ 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"); });
|
|
|
@@ -160,7 +171,7 @@ function TicketCreateDialog({ onClose, onCreated }: { onClose: () => void; onCre
|
|
|
submit.clear();
|
|
|
if (!customerId) {
|
|
|
setInvalid("customer");
|
|
|
- searchInput.current?.focus();
|
|
|
+ customerTrigger.current?.focus();
|
|
|
return;
|
|
|
}
|
|
|
if (title.trim().length < 2) { setInvalid("title"); return; }
|
|
|
@@ -184,40 +195,58 @@ function TicketCreateDialog({ onClose, onCreated }: { onClose: () => void; onCre
|
|
|
<div className="ticket-create-field" onBlur={(event) => {
|
|
|
if (!event.currentTarget.contains(event.relatedTarget)) setCustomerOpen(false);
|
|
|
}}>
|
|
|
- <label htmlFor={`${id}-customer`} className="field-label-title">归属客户<em aria-hidden="true">*</em></label>
|
|
|
- <div className="ticket-create-search">
|
|
|
- <Search size={16} aria-hidden="true" className="ticket-create-search-icon" />
|
|
|
- <Input id={`${id}-customer`} ref={searchInput} role="combobox" autoComplete="off" maxLength={50}
|
|
|
- aria-autocomplete="list" aria-expanded={customerOpen} aria-controls={customerOpen ? customerOptionListId : undefined}
|
|
|
- aria-activedescendant={customerOpen && customers[activeCustomer] ? `${customerOptionListId}-${activeCustomer}` : undefined}
|
|
|
- aria-required="true" aria-invalid={invalid === "customer" || undefined} aria-describedby={invalid === "customer" ? `${id}-error` : undefined}
|
|
|
- placeholder="输入客户名称或编号" value={customer?.customer_name || customerId || keyword} readOnly={Boolean(customer)}
|
|
|
- onChange={(event) => searchCustomers(event.target.value)} onKeyDown={handleCustomerKeys}
|
|
|
- onClick={() => { if (!customer) setCustomerOpen(true); }} />
|
|
|
- {customer ? <button type="button" className="ticket-create-search-clear" aria-label="清除归属客户" onClick={() => {
|
|
|
- pickCustomer(null);
|
|
|
- searchCustomers("");
|
|
|
- searchInput.current?.focus();
|
|
|
- }}><X size={15} aria-hidden="true" /></button> : <ChevronDown size={15} aria-hidden="true" className="ticket-create-search-chevron" />}
|
|
|
- {customerOpen && <div className="ticket-create-customer-popup">
|
|
|
- <ul ref={customerList} id={customerOptionListId} role="listbox" aria-label="客户搜索结果">
|
|
|
- {customers.map((item, index) => <li key={item.customer_id} id={`${customerOptionListId}-${index}`} role="option"
|
|
|
- aria-selected={index === activeCustomer} onMouseEnter={() => setActiveCustomer(index)}
|
|
|
- onMouseDown={(event) => event.preventDefault()} onClick={() => pickCustomer(item)}>
|
|
|
- <span>{item.customer_name || item.customer_id}</span>
|
|
|
- {item.customer_name && <small>{item.customer_id}</small>}
|
|
|
- </li>)}
|
|
|
- </ul>
|
|
|
- {!customers.length && <p role="status">{customerHints[customerLookup]}</p>}
|
|
|
+ <span id={`${id}-customer-label`} className="field-label-title">归属客户<em aria-hidden="true">*</em></span>
|
|
|
+ <div className="ticket-create-picker">
|
|
|
+ <button type="button" ref={customerTrigger} className="ticket-create-select" data-placeholder={!customerId || undefined}
|
|
|
+ aria-labelledby={`${id}-customer-label ${id}-customer-value`} aria-haspopup="dialog" aria-expanded={customerOpen}
|
|
|
+ aria-controls={customerOpen ? customerPopupId : undefined} aria-invalid={invalid === "customer" || undefined}
|
|
|
+ aria-describedby={invalid === "customer" ? `${id}-error` : undefined} title={customer?.customer_name || customerId || undefined}
|
|
|
+ onClick={() => { if (customerOpen) closeCustomerPicker(); else openCustomerPicker(); }}
|
|
|
+ onKeyDown={(event) => {
|
|
|
+ if (event.key !== "ArrowDown" && event.key !== "ArrowUp") return;
|
|
|
+ event.preventDefault();
|
|
|
+ openCustomerPicker();
|
|
|
+ }}>
|
|
|
+ <span className="ticket-create-select-value" id={`${id}-customer-value`}>{customer?.customer_name || customerId || "请选择客户"}</span>
|
|
|
+ </button>
|
|
|
+ {customerOpen && <div id={customerPopupId} className="ticket-create-customer-popup" role="dialog" aria-label="选择归属客户"
|
|
|
+ onKeyDown={(event) => {
|
|
|
+ if (event.key !== "Escape" || isImeComposing(event)) return;
|
|
|
+ event.preventDefault();
|
|
|
+ event.stopPropagation();
|
|
|
+ closeCustomerPicker();
|
|
|
+ }}>
|
|
|
+ <div className="ticket-create-search">
|
|
|
+ <Search size={15} aria-hidden="true" />
|
|
|
+ <Input ref={searchInput} role="combobox" aria-label="搜索客户" aria-autocomplete="list" aria-expanded="true" aria-controls={customerOptionListId}
|
|
|
+ aria-activedescendant={customers[activeCustomer] ? `${customerOptionListId}-${activeCustomer}` : undefined}
|
|
|
+ autoComplete="off" maxLength={50} placeholder="输入客户名称或编号" value={keyword}
|
|
|
+ onChange={(event) => searchCustomers(event.target.value)} onKeyDown={handleCustomerKeys} />
|
|
|
+ </div>
|
|
|
+ <div className="ticket-create-customer-results">
|
|
|
+ <ul ref={customerList} id={customerOptionListId} role="listbox" aria-label="客户搜索结果">
|
|
|
+ {customers.map((item, index) => <li key={item.customer_id} id={`${customerOptionListId}-${index}`} role="option"
|
|
|
+ aria-selected={index === activeCustomer} onMouseEnter={() => setActiveCustomer(index)}
|
|
|
+ onMouseDown={(event) => event.preventDefault()} onClick={() => pickCustomer(item)}>
|
|
|
+ <span>{item.customer_name || item.customer_id}</span>
|
|
|
+ {item.customer_name && <small>{item.customer_id}</small>}
|
|
|
+ </li>)}
|
|
|
+ </ul>
|
|
|
+ {!customers.length && <p role="status">{customerHints[customerLookup]}</p>}
|
|
|
+ </div>
|
|
|
+ {customer && <button type="button" className="ticket-create-search-clear" onClick={() => pickCustomer(null)}>清除选择</button>}
|
|
|
</div>}
|
|
|
</div>
|
|
|
</div>
|
|
|
<FieldLabel className="ticket-create-field" label="关联订阅">
|
|
|
- <select className="ticket-create-control" value={orderId} data-placeholder={!orderId || undefined}
|
|
|
- disabled={!customerId || subscriptionLookup !== "ready" || orders.length === 0} onChange={(event) => setOrderId(event.target.value)}>
|
|
|
- <option value="">{customerId ? subscriptionHints[subscriptionLookup] : "请先选择归属客户"}</option>
|
|
|
- {orders.map((order) => <option key={order.subscription_id} value={order.subscription_id}>{order.plan_name ?? order.subscription_id}{order.contract_no ? ` · ${order.contract_no}` : ""}</option>)}
|
|
|
- </select>
|
|
|
+ <span className="ticket-create-select" data-placeholder={!selectedSubscription || undefined} title={selectedSubscription?.label}>
|
|
|
+ <span className="ticket-create-select-value" aria-hidden="true">{selectedSubscription?.label ?? subscriptionPlaceholder}</span>
|
|
|
+ <select value={orderId} disabled={!customerId || subscriptionLookup !== "ready" || subscriptions.length === 0}
|
|
|
+ onChange={(event) => setOrderId(event.target.value)}>
|
|
|
+ <option value="">{subscriptionPlaceholder}</option>
|
|
|
+ {subscriptions.map((item) => <option key={item.value} value={item.value}>{item.label}</option>)}
|
|
|
+ </select>
|
|
|
+ </span>
|
|
|
</FieldLabel>
|
|
|
</div>
|
|
|
</section>
|