/* Bonyan ERP — إدارة المخازن: قوائم المخازن والفئات المُدارة - إضافة/تعديل/حذف مخزن أو فئة (الحذف ممنوع إن كانت مستخدمة في مواد). - إعادة التسمية تنعكس تلقائيًا على كل المواد المرتبطة (محليًا وفي القاعدة). */ function WarehousesScreen({ user }) { const I = window.BnyIcon; const D = window.BnyData; const { Card, Button, Input, Badge } = window.BonyanDesignSystem_ad3ca0; const isMobile = window.useBnyMobile(); const [, forceRender] = React.useReducer((x) => x + 1, 0); const canEdit = window.BnyCan(user, "inventory_edit"); if (!canEdit) { return (

لا تملك صلاحية الوصول

إدارة المخازن متاحة لمن يملك صلاحية «تعديل المواد والإعدادات».

); } return (
} items={D.warehouses || []} countOf={(w) => window.BnyOps.countMaterialsIn("wh", w.name)} countLabel={(n) => n + " مادة"} placeholder="اسم مخزن جديد (مثال: فرع الجديدة)" onAdd={async (name) => { await window.BnyOps.addWarehouse(name); forceRender(); }} onRename={async (item, name) => { await window.BnyOps.renameWarehouse(item, name); forceRender(); }} onDelete={async (item) => { await window.BnyOps.deleteWarehouse(item); forceRender(); }} hint="إعادة تسمية المخزن تُحدِّث كل المواد المرتبطة به تلقائيًا. لا يمكن حذف مخزن فيه مواد — انقلها أولًا من شاشة المواد." /> } items={D.categories || []} countOf={(c) => window.BnyOps.countMaterialsIn("cat", c.name)} countLabel={(n) => n + " مادة"} placeholder="اسم فئة جديدة (مثال: عوازل)" onAdd={async (name) => { await window.BnyOps.addCategory(name); forceRender(); }} onRename={async (item, name) => { await window.BnyOps.renameCategory(item, name); forceRender(); }} onDelete={async (item) => { await window.BnyOps.deleteCategory(item); forceRender(); }} hint="الفئة إلزامية عند إضافة أي مادة. إعادة التسمية تُحدِّث المواد المرتبطة، ولا يمكن حذف فئة مستخدمة." />
); } /* قائمة مُدارة عامة: إضافة + تعديل inline + حذف مع منع الاستخدام */ function ManagedList({ title, icon, items, countOf, countLabel, placeholder, onAdd, onRename, onDelete, hint }) { const I = window.BnyIcon; const { Card, Button, Input, Badge } = window.BonyanDesignSystem_ad3ca0; const [newName, setNewName] = React.useState(""); const [editId, setEditId] = React.useState(null); const [editVal, setEditVal] = React.useState(""); const [busy, setBusy] = React.useState(false); const [err, setErr] = React.useState(""); const flash = (m) => { setErr(m); setTimeout(() => setErr(""), 4000); }; const run = async (fn) => { if (busy) return; setBusy(true); try { await fn(); setErr(""); } catch (e) { flash(String((e && e.message) || e)); } finally { setBusy(false); } }; const add = () => run(async () => { await onAdd(newName); setNewName(""); }); const saveEdit = (item) => run(async () => { await onRename(item, editVal); setEditId(null); }); const del = (item) => run(async () => { await onDelete(item); }); return (

{hint}

{/* إضافة */}
setNewName(e.target.value)} placeholder={placeholder} onKeyDown={(e) => { if (e.key === "Enter") add(); }} />
{err &&
{err}
} {/* القائمة */}
{items.map((item, i) => { const used = countOf(item); const editing = editId === item.id; return (
0 || true ? "1px solid var(--border-subtle)" : "none" }}> {icon} {editing ? ( <>
setEditVal(e.target.value)} autoFocus onKeyDown={(e) => { if (e.key === "Enter") saveEdit(item); if (e.key === "Escape") setEditId(null); }} style={{ width: "100%", height: 38 }} aria-label="الاسم الجديد" />
) : ( <>
{item.name}
0 ? "var(--text-muted)" : "var(--text-faint)" }}>{countLabel(used)}
)}
); })} {items.length === 0 &&
لا عناصر بعد — أضف أول عنصر.
}
); } window.WarehousesScreen = WarehousesScreen;