168 lines
8.7 KiB
TypeScript
168 lines
8.7 KiB
TypeScript
import { useEffect, useMemo, useState } from 'react';
|
|
import { AlarmClock, AlertOctagon, ClipboardList, ShoppingCart, TrendingUp, Moon, Sun, FileWarning } from 'lucide-react';
|
|
import { useTheme } from '../context/ThemeContext';
|
|
import { useText } from '../i18n/useText';
|
|
import { materialBalance, purchaseOrders, salesOrders } from '../data/ibp';
|
|
import { CURRENT_PERIOD, periodLabel } from '../lib/time';
|
|
import { wan } from '../lib/money';
|
|
|
|
interface HeaderProps {
|
|
/** 全局时间游标:仅用于右上角「数据期」标注,KPI 取自内置业务单据快照 */
|
|
period: number;
|
|
}
|
|
|
|
/** 单据金额合计(元);先按 2 位小数收敛浮点误差,展示口径交给 lib/money */
|
|
const sum = <T,>(rows: T[], pick: (r: T) => number) =>
|
|
+rows.reduce((acc, r) => acc + (pick(r) || 0), 0).toFixed(2);
|
|
|
|
export default function MecHeader({ period }: HeaderProps) {
|
|
const { tokens, theme, toggleTheme } = useTheme();
|
|
const { t } = useText();
|
|
const [time, setTime] = useState(() => new Date());
|
|
|
|
useEffect(() => {
|
|
const id = setInterval(() => setTime(new Date()), 1000);
|
|
return () => clearInterval(id);
|
|
}, []);
|
|
|
|
const pad = (n: number) => String(n).padStart(2, '0');
|
|
const timeStr = `${pad(time.getHours())}:${pad(time.getMinutes())}:${pad(time.getSeconds())}`;
|
|
const dateStr = `${time.getFullYear()}-${pad(time.getMonth() + 1)}-${pad(time.getDate())} ${time.toLocaleDateString('zh-CN', { weekday: 'short' })}`;
|
|
|
|
const isLight = theme === 'light';
|
|
|
|
/* KPI 全部取自内置单据快照:履约看订单与交期,异常看物料缺口与未审采购 */
|
|
const kpi = useMemo(() => {
|
|
const d = new Date();
|
|
const today = `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`;
|
|
const gap = materialBalance().filter(m => m.缺口 > 0);
|
|
return {
|
|
soCount: salesOrders.length,
|
|
soAmount: sum(salesOrders, s => s.含税金额),
|
|
poCount: new Set(purchaseOrders.map(p => p.采购订单号)).size,
|
|
gapCount: gap.length,
|
|
overdue: salesOrders.filter(s => s.预计发货日期 < today).length,
|
|
draftPo: new Set(purchaseOrders.filter(p => p.订单状态 !== '生效').map(p => p.采购订单号)).size,
|
|
};
|
|
}, []);
|
|
|
|
const stats = [
|
|
{ label: '销售订单', value: String(kpi.soCount), unit: '张', color: tokens.accent, Icon: ClipboardList },
|
|
{ label: '订单金额', value: wan(kpi.soAmount), unit: '万元', color: tokens.accent, Icon: TrendingUp },
|
|
{ label: '采购订单', value: String(kpi.poCount), unit: '张', color: tokens.accent, Icon: ShoppingCart },
|
|
{ label: '物料缺口', value: String(kpi.gapCount), unit: '种', color: kpi.gapCount ? tokens.danger : tokens.success, Icon: AlertOctagon },
|
|
{ label: '交期逾期', value: String(kpi.overdue), unit: '单', color: kpi.overdue ? tokens.warning : tokens.success, Icon: AlarmClock },
|
|
{ label: '待审采购单', value: String(kpi.draftPo), unit: '张', color: kpi.draftPo ? tokens.warning : tokens.success, Icon: FileWarning },
|
|
];
|
|
|
|
return (
|
|
<header style={{
|
|
height: 88, flexShrink: 0, display: 'flex', alignItems: 'center', padding: '0 24px',
|
|
position: 'relative', zIndex: 10, overflow: 'visible',
|
|
}}>
|
|
<div style={{
|
|
position: 'absolute', top: 0, left: 0, right: 0, height: 1,
|
|
background: tokens.headerScanLine, boxShadow: `0 0 12px ${tokens.accentGlow}`, zIndex: 2,
|
|
}} />
|
|
<svg style={{
|
|
position: 'absolute', bottom: -26, left: 0, width: '100%', height: 28,
|
|
pointerEvents: 'none', zIndex: 0, overflow: 'visible',
|
|
}} viewBox="0 0 1000 28" preserveAspectRatio="none">
|
|
<path d="M 0,0 C 250,28 750,28 1000,0" fill="none" stroke={tokens.svgStroke} strokeWidth="1" vectorEffect="non-scaling-stroke" />
|
|
<path d="M 0,0 C 250,28 750,28 1000,0" fill="none" stroke={tokens.accent} strokeWidth="6" opacity="0.10" vectorEffect="non-scaling-stroke" />
|
|
<circle cx="0" cy="0" r="2.5" fill={tokens.svgDot} />
|
|
<circle cx="1000" cy="0" r="2.5" fill={tokens.svgDot} />
|
|
</svg>
|
|
|
|
{/* 品牌 + 标题 */}
|
|
<div style={{ display: 'flex', alignItems: 'center', gap: 14, flex: 1, position: 'relative', zIndex: 1 }}>
|
|
{/* 品牌 logo:白底反预乘抠成透明,深浅主题各用一版——
|
|
浅色版保留原墨色,深色版把中性色(深蓝/灰)换成近白、品红方块原样保留 */}
|
|
<img
|
|
src={isLight ? '/mod-assets/jieya-ibp/jieya-logo-on-light.png' : '/mod-assets/jieya-ibp/jieya-logo-on-dark.png'}
|
|
alt="JIEYA 洁雅家居"
|
|
width={172}
|
|
height={48}
|
|
style={{ display: 'block', width: 172, height: 'auto', flexShrink: 0 }}
|
|
/>
|
|
<div style={{ minWidth: 0 }}>
|
|
<h1 style={{
|
|
color: tokens.text, fontSize: 21, fontWeight: 800, letterSpacing: '0.04em',
|
|
margin: 0, textShadow: tokens.titleGlow, whiteSpace: 'nowrap',
|
|
}}>{t.appTitle}</h1>
|
|
<div style={{
|
|
color: tokens.textSub, fontSize: 12.5, marginTop: 3, letterSpacing: '0.04em',
|
|
whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis',
|
|
}}>{t.appSubTitle}</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div style={{ width: 1, height: 44, background: `linear-gradient(to bottom, transparent, ${tokens.accent}33, transparent)`, flexShrink: 0, margin: '0 22px', position: 'relative', zIndex: 1 }} />
|
|
|
|
{/* KPI 胶囊带 */}
|
|
<div style={{ display: 'flex', alignItems: 'center', gap: 8, flex: 2.1, justifyContent: 'center', position: 'relative', zIndex: 1, marginTop: 22 }}>
|
|
{stats.map((item, idx) => {
|
|
const { Icon } = item;
|
|
return (
|
|
<div key={item.label} style={{ display: 'flex', alignItems: 'center', flex: 1, minWidth: 0 }}>
|
|
<div style={{
|
|
textAlign: 'center', padding: '6px 10px', borderRadius: 10, width: '100%',
|
|
background: tokens.kpiCardBg, border: `1px solid ${tokens.kpiCardBorder}`,
|
|
backdropFilter: 'blur(8px)', whiteSpace: 'nowrap',
|
|
}}>
|
|
<div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 5 }}>
|
|
<Icon size={15} color={item.color} strokeWidth={2} />
|
|
<span style={{
|
|
color: item.color, fontSize: 23, fontWeight: 800, lineHeight: 1,
|
|
textShadow: isLight ? 'none' : `0 0 10px ${item.color}80`,
|
|
}}>{item.value}</span>
|
|
<span style={{ color: tokens.textSub, fontSize: 11.5, fontWeight: 500 }}>{item.unit}</span>
|
|
</div>
|
|
<div style={{ color: tokens.textSub, fontSize: 11.5, marginTop: 3, letterSpacing: '0.03em', fontWeight: 600 }}>
|
|
{item.label}
|
|
</div>
|
|
</div>
|
|
{idx < stats.length - 1 && (
|
|
<div style={{ width: 1, height: 36, background: `${tokens.accent}18`, margin: '0 3px', flexShrink: 0 }} />
|
|
)}
|
|
</div>
|
|
);
|
|
})}
|
|
</div>
|
|
|
|
<div style={{ width: 1, height: 44, background: `linear-gradient(to bottom, transparent, ${tokens.accent}33, transparent)`, flexShrink: 0, margin: '0 22px', position: 'relative', zIndex: 1 }} />
|
|
|
|
{/* 时钟 + 语言 / 主题 */}
|
|
<div style={{ display: 'flex', alignItems: 'center', gap: 12, flex: 1, justifyContent: 'flex-end', position: 'relative', zIndex: 1 }}>
|
|
<div style={{ textAlign: 'right' }}>
|
|
<div style={{
|
|
color: period === CURRENT_PERIOD ? tokens.textMuted : tokens.warning, fontSize: 11.5, marginTop: 2,
|
|
letterSpacing: '0.04em', fontVariantNumeric: 'tabular-nums', fontWeight: 700,
|
|
}}>{t.header.dataAt} {periodLabel(period)}</div>
|
|
<div style={{
|
|
color: tokens.accent, fontSize: 16, fontWeight: 700, fontVariantNumeric: 'tabular-nums',
|
|
lineHeight: 1, letterSpacing: '0.05em', textShadow: isLight ? 'none' : `0 0 16px ${tokens.accentGlow}`,
|
|
}}>{timeStr}</div>
|
|
<div style={{ color: tokens.textMuted, fontSize: 11.5, marginTop: 2, letterSpacing: '0.04em' }}>{dateStr}</div>
|
|
</div>
|
|
|
|
<div style={{ display: 'flex', flexDirection: 'column', gap: 6 }}>
|
|
<button
|
|
onClick={toggleTheme}
|
|
className="ghost-btn"
|
|
style={{
|
|
display: 'flex', alignItems: 'center', gap: 5, padding: '4px 11px', borderRadius: 20,
|
|
border: `1px solid ${tokens.border}`, background: tokens.panelBgAlt,
|
|
color: tokens.textSub, fontSize: 11.5, fontWeight: 600, cursor: 'pointer', flexShrink: 0,
|
|
}}
|
|
>
|
|
{theme === 'dark' ? <Sun size={12} /> : <Moon size={12} />}
|
|
{theme === 'dark' ? t.common.light : t.common.dark}
|
|
</button>
|
|
</div>
|
|
|
|
</div>
|
|
</header>
|
|
);
|
|
}
|