import { useEffect, useMemo, useState } from "react"; import { Trash2, Plus, Copy } from "lucide-react"; import type { Material } from "../../lib/types"; import { normalize } from "../../lib/normalize"; import { useMaterials } from "../../hooks"; import { matchMaterial, materialBaseName } from "../../lib/quoteUtils"; import { Combobox } from "../../components/Combobox"; import { NumberInput } from "../../components/NumberInput"; import { MultiSelectCombobox } from "../../components/MultiSelectCombobox"; import { api, apiV26 } from "../../lib/api"; import { faNum } from "../../lib/formatters"; export interface ItemDraft { tempId: string; cat: string; brand: string; specs: string[]; qty: number | null; unit: string; price: number | null; total: number | null; } export interface MaterialRec { topic: string; category: string; brand: string; specs: string[]; } export interface Step2Props { items: ItemDraft[]; onItemsChange: (items: ItemDraft[]) => void; selectedTopic: string; onRemember: (rec: MaterialRec) => void; } export function itemRowLabel(it: ItemDraft): string { const parts = [it.cat, it.brand, it.specs.join(" - ")].filter(Boolean); const name = parts.length ? parts.join(" - ") : "(قلم بدون‌نام)"; const qtyPart = it.qty != null && it.unit ? `${it.qty} ${it.unit}` : it.qty != null ? String(it.qty) : ""; return qtyPart ? `${name} -- ${qtyPart}` : name; } export function materialOptions( materials: Material[], _topic: string, item: ItemDraft, field: "brand" | "spec", allItems: ItemDraft[] = [], apiBrands: string[] = [], apiSpecs: string[] = [], ): string[] { let list = materials; const cat = item.cat.trim(); const fromRows = (sel: (it: ItemDraft) => string | string[]): string[] => { const out = new Set(); allItems.forEach((it) => { if (it.tempId === item.tempId) return; const v = sel(it); if (Array.isArray(v)) v.forEach((x) => x && out.add(x)); else if (v) out.add(v); }); return [...out]; }; if (field === "brand") { const allBrands = new Set(materials.map((m) => m.brand).filter(Boolean)); apiBrands.forEach((b) => b && allBrands.add(b)); fromRows((it) => it.brand).forEach((b) => allBrands.add(b)); return [...allBrands].sort((a, b) => a.localeCompare(b, "fa")); } if (cat) list = list.filter( (m) => normalize(materialBaseName(m.name, m.brand, m.specs)) === normalize(cat), ); const base = new Set(); apiSpecs.forEach((s) => s && base.add(s)); materials.forEach((m) => (m.specs || []).forEach((s) => s && base.add(s))); list.forEach((m) => (m.specs || []).forEach((s) => s && base.add(s))); fromRows((it) => it.specs).forEach((s) => base.add(s)); return [...base].sort((a, b) => a.localeCompare(b, "fa")); } export function Step2LineItems({ items, onItemsChange, selectedTopic, onRemember }: Step2Props) { const materials = useMaterials(); const [unitOptions, setUnitOptions] = useState(() => { const all = new Set(); items.forEach((it) => { if (it.unit) all.add(it.unit); }); return [...all]; }); const allUnits = useMemo(() => { const out = new Set(unitOptions); items.forEach((it) => { if (it.unit) out.add(it.unit); }); return [...out]; }, [items, unitOptions]); const allMaterialNames = useMemo(() => { const set = new Set(); materials.forEach((m) => { const base = materialBaseName(m.name, m.brand, m.specs); if (base) set.add(base); }); return [...set].sort((a, b) => a.localeCompare(b, "fa")); }, [materials]); const [brandOptions, setBrandOptions] = useState([]); const [specOptions, setSpecOptions] = useState([]); useEffect(() => { let alive = true; void api .unitsList() .then((list) => alive && setUnitOptions((prev) => { const merged = new Set(prev); list.forEach((u) => { if (u.name_fa || u.code) merged.add(u.name_fa || u.code); }); return [...merged]; })) .catch(() => {}); void api .brandsList() .then((list) => alive && setBrandOptions(list.map((b) => b.name).filter(Boolean))) .catch(() => {}); void api .specsList() .then((list) => alive && setSpecOptions(list)) .catch(() => {}); return () => { alive = false; }; }, []); const patch = (tempId: string, p: Partial) => { onItemsChange(items.map((it) => (it.tempId === tempId ? { ...it, ...p } : it))); }; const remove = (tempId: string) => { onItemsChange(items.filter((it) => it.tempId !== tempId)); }; const add = () => { onItemsChange([ ...items, { tempId: "it" + Date.now() + Math.random().toString(36).slice(2, 6), cat: "", brand: "", specs: [], qty: null, unit: "", price: null, total: null }, ]); }; // قابلیت تکرار قلم در ردیف بعد با کپی عمیق فیلدها و شناسه یکتا const duplicate = (index: number) => { const target = items[index]; if (!target) return; const clonedItem: ItemDraft = { ...target, tempId: "it" + Date.now() + Math.random().toString(36).slice(2, 6), specs: [...target.specs], }; const nextItems = [...items]; nextItems.splice(index + 1, 0, clonedItem); onItemsChange(nextItems); }; const rememberField = (field: "i-cat" | "i-brand" | "i-spec", it: ItemDraft) => { if (!selectedTopic) return; let rec: MaterialRec; if (field === "i-cat") { if (!it.cat.trim()) return; rec = { topic: selectedTopic, category: it.cat.trim(), brand: "", specs: [] }; } else if (field === "i-brand") { if (!it.brand.trim()) return; rec = { topic: selectedTopic, category: it.cat.trim(), brand: it.brand.trim(), specs: [] }; } else { if (!it.specs.length) return; rec = { topic: selectedTopic, category: it.cat.trim(), brand: it.brand.trim(), specs: it.specs }; } const dup = !!matchMaterial(materials, rec.category, rec.brand, rec.specs); if (dup) return; onRemember(rec); }; const commonInputClass = "!h-9 !text-xs font-normal text-right placeholder:text-xs placeholder:font-normal"; return (
{/* هدر */}
۲

اقلام سفارش

{faNum(items.length)} قلم

مشخصات، تعداد و قیمت هر قلم را ثبت کنید. گزینه‌های جدید با تایپ ذخیره می‌شوند.

{/* لیست کارت‌های اقلام */}
{items.map((it, idx) => (
{/* سربرگ قلم */}
{faNum(idx + 1)} {it.cat || "قلم جدید"} {it.brand ? `• ${it.brand}` : ""}
{/* اکشن‌های کارت: کپی و حذف */}
{items.length > 1 && ( )}
{/* ردیف ۱: متریال و برند */}
patch(it.tempId, { cat: x.label })} items={allMaterialNames} allowAdd addSuffix="به عنوان متریال جدید" placeholder="نام متریال" onInputChange={(v) => patch(it.tempId, { cat: v })} onBlur={() => rememberField("i-cat", it)} inputClassName={commonInputClass} className="min-w-0" />
patch(it.tempId, { brand: x.label })} items={materialOptions(materials, selectedTopic, it, "brand", items, brandOptions)} allowAdd addSuffix="به عنوان برند جدید" placeholder="برند" onInputChange={(v) => patch(it.tempId, { brand: v })} onBlur={() => rememberField("i-brand", it)} inputClassName={commonInputClass} className="min-w-0" />
{/* ردیف ۲: مشخصه فنی */}
patch(it.tempId, { specs })} options={materialOptions(materials, selectedTopic, it, "spec", items, [], specOptions)} placeholder="انتخاب مشخصات (مثلاً ضخامت، رنگ، تیپ...)" className="min-w-0" />
{/* بخش مقادیر و محاسبات مالی */}
{/* ردیف ۳: تعداد + واحد سنجش */}
{ patch(it.tempId, { qty, total: qty != null && it.price != null ? Math.round(qty * it.price) : it.total, }); }} placeholder="مقدار" className="!text-right text-right" inputClassName={commonInputClass} />
patch(it.tempId, { unit: x.label })} items={allUnits.filter((u) => normalize(u) !== normalize(it.unit))} allowAdd addSuffix="به عنوان واحد جدید" placeholder="واحد (شاخه، متر، کیلو...)" onInputChange={(v) => patch(it.tempId, { unit: v })} onAdd={(v) => { const name = v.trim(); if (!name) return; void apiV26.catalog .createUnit(name) .then(() => { setUnitOptions((prev) => prev.some((u) => normalize(u) === normalize(name)) ? prev : [...prev, name], ); }) .catch(() => {}); }} inputClassName={commonInputClass} className="min-w-0" />
{/* ردیف ۴: قیمت هر واحد */}
{ patch(it.tempId, { price, total: it.qty != null && price != null ? Math.round(it.qty * price) : it.total, }); }} placeholder="قیمت هر واحد" showWords className="!text-right text-right" inputClassName={commonInputClass} />
{/* ردیف ۵: قیمت کل قلم */}
{ patch(it.tempId, { total, price: total != null && it.qty != null && it.qty > 0 ? Math.round(total / it.qty) : it.price, }); }} placeholder="قیمت کل قلم" showWords className="!text-right text-right" inputClassName={commonInputClass} />
))}
{/* دکمه افزودن قلم جدید */}
); }