'use client'; import { useState } from 'react'; import { X, Plus, Pencil, Trash2, Check } from 'lucide-react'; import type { DictItem, SourceTarget, SourceType } from '@/lib/requirement'; import { SOURCE_TYPE_LABEL } from '@/lib/requirement'; interface DictDrawerProps { open: boolean; title: string; items: DictItem[]; onClose: () => void; onAdd: (name: string) => void; onUpdate: (id: string, name: string) => void; onDelete: (id: string) => void; } interface SourceDrawerProps { open: boolean; items: SourceTarget[]; onClose: () => void; onAdd: (name: string, sourceType: SourceType) => void; onUpdate: (id: string, name: string) => void; onDelete: (id: string) => void; } const SOURCE_TYPES: SourceType[] = ['customer', 'internal', 'operation', 'aftersale', 'market', 'competitor', 'management']; export function DictDrawer({ open, title, items, onClose, onAdd, onUpdate, onDelete }: DictDrawerProps) { if (!open) return null; return ( ); } export function SourceDrawer({ open, items, onClose, onAdd, onUpdate, onDelete }: SourceDrawerProps) { const [activeTab, setActiveTab] = useState('customer'); if (!open) return null; const filtered = items.filter((t) => t.sourceType === activeTab); return ( {/* Tabs */}
{SOURCE_TYPES.map((type) => ( ))}
onAdd(name, activeTab)} onUpdate={onUpdate} onDelete={onDelete} placeholder={`添加${SOURCE_TYPE_LABEL[activeTab]}来源对象`} />
); } function DrawerShell({ title, onClose, children }: { title: string; onClose: () => void; children: React.ReactNode }) { return (
e.stopPropagation()}>
{title}
{children}
); } function ItemList({ items, onAdd, onUpdate, onDelete, placeholder }: { items: { id: string; name: string }[]; onAdd: (name: string) => void; onUpdate: (id: string, name: string) => void; onDelete: (id: string) => void; placeholder?: string; }) { const [newName, setNewName] = useState(''); const [editingId, setEditingId] = useState(null); const [editingName, setEditingName] = useState(''); const handleAdd = () => { const trimmed = newName.trim(); if (!trimmed) return; onAdd(trimmed); setNewName(''); }; const confirmEdit = () => { if (editingId && editingName.trim()) { onUpdate(editingId, editingName.trim()); } setEditingId(null); setEditingName(''); }; return ( <>
setNewName(e.target.value)} onKeyDown={(e) => e.key === 'Enter' && handleAdd()} placeholder={placeholder || '输入名称...'} className="flex-1 h-8 rounded-lg border border-[var(--line)] bg-[var(--bg-card)] px-3 text-[13px] text-[var(--ink)] placeholder:text-[var(--ink-muted)] outline-none focus:border-[var(--accent)]" />
{items.map((item) => (
{editingId === item.id ? (
setEditingName(e.target.value)} onKeyDown={(e) => { if (e.key === 'Enter') confirmEdit(); if (e.key === 'Escape') setEditingId(null); }} autoFocus className="flex-1 h-7 rounded-lg border border-[var(--accent)] bg-[var(--bg-card)] px-2 text-[13px] text-[var(--ink)] outline-none" />
) : ( <> {item.name}
)}
))} {items.length === 0 && (
暂无数据,请添加
)}
); }