import { useMemo, useState } from "react"; import type { Supplier } from "../../lib/types"; import { normalize } from "../../lib/normalize"; import { useSuppliers } from "../../hooks"; import { useAppStore } from "../../store"; import { TopicGrid } from "../../components/TopicGrid"; import { Combobox } from "../../components/Combobox"; import { cn } from "../../lib/utils"; export interface Step1Props { selectedTopic: string; onTopicSelect: (topic: string) => void; supplierName: string; onSupplierName: (name: string) => void; newSup: { location: string; phone: string; contact: string }; onNewSup: (patch: Partial<{ location: string; phone: string; contact: string }>) => void; isOfficial: boolean; onOfficial: (v: boolean) => void; invalid: boolean; } // مرحله ۱ — تاپیک + تأمین‌کننده (چیپ‌های سریع + فرم تأمین‌کننده‌ی جدید) export function Step1TopicSupplier({ selectedTopic, onTopicSelect, supplierName, onSupplierName, newSup, onNewSup, isOfficial, onOfficial, invalid, }: Step1Props) { const suppliers = useSuppliers(); const topics = useAppStore((s) => s.topics); const [selectedSupId, setSelectedSupId] = useState(null); const topicSups = useMemo( () => (selectedTopic ? suppliers.filter((s) => normalize(s.topic) === normalize(selectedTopic)) : []), [suppliers, selectedTopic], ); const counts = useMemo(() => { const out: Record = {}; topics.forEach((t) => { out[t.name] = suppliers.filter((s) => normalize(s.topic) === normalize(t.name)).length; }); return out; }, [topics, suppliers]); // آیا تأمین‌کننده‌ی فعلی در لیست هست؟ const matched = useMemo(() => { const n = normalize(supplierName); if (!n) return null; return topicSups.find((s) => normalize(s.name) === n) || null; }, [topicSups, supplierName]); const showNewSupForm = supplierName.trim().length > 0 && !matched; const pickSupplier = (s: Supplier) => { onSupplierName(s.name); setSelectedSupId(s.id); }; return (

۱ موضوع (دسته‌بندی کالا)

اول موضوع کالای موردنیاز را از بین تصاویر انتخاب کنید. تأمین‌کننده‌ها بر اساس موضوع فیلتر می‌شوند.
{selectedTopic && (

۱ تأمین‌کننده

{!topicSups.length ? (
هنوز تأمین‌کننده‌ای در این موضوع ثبت نشده — پایین را تایپ کنید تا به‌عنوان تأمین‌کننده‌ی جدید ذخیره شود.
) : ( topicSups.map((s) => ( )) )}
{ onSupplierName(it.label); setSelectedSupId(it.id === it.label ? null : it.id); }} items={topicSups.map((s) => ({ id: s.id, name: s.name }))} allowAdd addSuffix="به عنوان تأمین‌کنندهٔ جدید" placeholder="تایپ کنید... مثلاً زاگرس" onInputChange={(v) => { onSupplierName(v); setSelectedSupId(null); }} />
{showNewSupForm && (
این تأمین‌کننده در لیست نیست. جزئیات را تکمیل کنید تا ذخیره شود:
onNewSup({ location: e.target.value })} />
onNewSup({ phone: e.target.value })} />
onNewSup({ contact: e.target.value })} />
)} {supplierName.trim() && ( )}
)}
); }