Files
ai-platform/src/modules/jieya-ibp/components/DemandPanel.tsx
T
2026-10-18 15:27:02 +08:00

125 lines
5.3 KiB
TypeScript

import { useEffect, useMemo, useRef, useState } from 'react';
import { Building2, Search, ShoppingCart, X } from 'lucide-react';
import PanelShell, { CountBadge } from './layout/PanelShell';
import { AutoScrollProvider, AutoScrollToggle } from './layout/AutoScroll';
import { LEFT_MOTION } from '../hooks/useAutoScroll';
import { useTheme } from '../context/ThemeContext';
import SalesOrderList, { filterSalesOrders } from './demand/SalesOrderList';
import CustomerList, { buildCustomers, filterCustomers } from './demand/CustomerList';
import { salesOrders } from '../data/ibp';
type TabKey = 'so' | 'cus';
interface Props {
expanded: boolean;
onToggle: () => void;
/** 当前选中的销售订单号:本页两个页签与右侧供应栏共用同一联动游标 */
selectedSO: string | null;
onSelectSO: (so: string | null) => void;
}
const TABS: { key: TabKey; label: string; short: string; icon: typeof ShoppingCart; placeholder: string }[] = [
{ key: 'so', label: '销售订单', short: '订单', icon: ShoppingCart, placeholder: '搜索订单号 / 客户 / 产品编码' },
{ key: 'cus', label: '客户资料', short: '客户', icon: Building2, placeholder: '搜索客户编码 / 简称 / 分类 / 部门' },
];
/**
* 左侧需求栏:销售订单与客户资料两个页签共用一块弧板,
* 搜索关键词、展开态与选中订单联动在两者之间保持同一份状态。
*/
export default function DemandPanel({ expanded, onToggle, selectedSO, onSelectSO }: Props) {
const { tokens } = useTheme();
const [tab, setTab] = useState<TabKey>('so');
/* 外部定位(对话卡的「定位到订单」/ 本体页明细行)把订单选中时切回销售订单页签,
否则停在客户资料页签会让用户以为点了没反应 */
const prevSO = useRef(selectedSO);
useEffect(() => {
if (selectedSO && selectedSO !== prevSO.current) setTab('so');
prevSO.current = selectedSO;
}, [selectedSO]);
const [query, setQuery] = useState('');
/* 卡片多到一屏装不下时自动缓慢下滚;标题栏按钮可关闭,页签之间共用这一个开关 */
const [autoScroll, setAutoScroll] = useState(true);
/* 徽标数与页签内容走同一组纯函数,标题数字不会和列表条数打架 */
const count = useMemo(() => {
const q = query.trim();
if (tab === 'so') return filterSalesOrders(salesOrders, q).length;
return filterCustomers(buildCustomers(selectedSO), q).length;
}, [tab, query, selectedSO]);
const segBtn = (active: boolean) => ({
display: 'flex' as const, alignItems: 'center' as const, justifyContent: 'center' as const,
gap: 5, flex: 1, minWidth: 0, padding: '5px 8px', borderRadius: 7,
fontSize: 13, fontWeight: 600, cursor: 'pointer', whiteSpace: 'nowrap' as const,
background: active ? tokens.accent : 'transparent',
color: active ? tokens.onAccent : tokens.textMuted,
border: `1px solid ${active ? tokens.accent : tokens.border}`,
});
const current = TABS.find(x => x.key === tab)!;
const Icon = current.icon;
return (
<PanelShell
side="left" expanded={expanded} onToggle={onToggle}
widthCollapsed={268} widthExpanded={468}
icon={<Icon size={16} />}
title={current.label}
badge={<CountBadge n={count} />}
extra={<AutoScrollToggle on={autoScroll} onToggle={() => setAutoScroll(v => !v)} expanded={expanded} />}
animClass="space-animate-left"
tabs={
<>
{TABS.map(x => (
/* 收起态宽度只够两个字:页签用短标签,避免挤成一列竖排 */
<button key={x.key} style={segBtn(tab === x.key)} onClick={() => setTab(x.key)}>
{expanded ? x.label : x.short}
</button>
))}
</>
}
>
{/* 搜索:两个页签共用,占位提示随页签切换 */}
<div style={{ position: 'relative', marginBottom: 10, flex: '0 0 auto' }}>
<Search size={14} style={{ position: 'absolute', left: 10, top: 10, color: tokens.textMuted }} />
<input
value={query}
onChange={e => setQuery(e.target.value)}
placeholder={current.placeholder}
style={{
width: '100%', padding: '8px 30px 8px 31px', fontSize: 13.5, borderRadius: 9,
background: tokens.panelBgAlt, border: `1px solid ${tokens.border}`, color: tokens.text,
outline: 'none',
}}
/>
{query && (
<button onClick={() => setQuery('')} style={{ position: 'absolute', right: 9, top: 11, color: tokens.textMuted, cursor: 'pointer', background: 'none', border: 'none' }}>
<X size={14} />
</button>
)}
</div>
<AutoScrollProvider value={{ enabled: autoScroll, cfg: LEFT_MOTION }}>
{tab === 'so' && (
<SalesOrderList
query={query}
expanded={expanded}
selectedSO={selectedSO}
onSelectSO={onSelectSO}
/>
)}
{tab === 'cus' && (
<CustomerList
query={query}
filterSO={selectedSO}
onClearFilter={() => onSelectSO(null)}
/* 点客户=带着它的编码切到销售订单页签,只看这家下的单 */
onPickCustomer={code => { setQuery(code); setTab('so'); }}
/>
)}
</AutoScrollProvider>
</PanelShell>
);
}