import { useState, useEffect } from "react"; import { getStockMovements, stockIn, stockOut, getAllItems, } from "../api/index"; const REASON_LABELS = { purchase: "Закупка", order: "Продажа", writeoff: "Списание", correction: "Корректировка", }; export default function AdminStock() { const [movements, setMovements] = useState([]); const [items, setItems] = useState([]); const [loading, setLoading] = useState(true); // состояние форм const [inForm, setInForm] = useState({ item_id: "", quantity: "", comment: "", }); const [outForm, setOutForm] = useState({ item_id: "", quantity: "", comment: "", }); const [busy, setBusy] = useState(false); const loadAll = () => { Promise.all([getStockMovements(), getAllItems()]) .then(([movRes, itemsRes]) => { setMovements(movRes.data); setItems(itemsRes.data); }) .catch(() => { }) .finally(() => setLoading(false)); }; useEffect(() => { loadAll(); }, []); // приход const handleStockIn = async () => { if (!inForm.item_id || !inForm.quantity || Number(inForm.quantity) <= 0) { alert("Выберите товар и укажите количество"); return; } setBusy(true); try { await stockIn({ item_id: Number(inForm.item_id), quantity: Number(inForm.quantity), comment: inForm.comment, }); setInForm({ item_id: "", quantity: "", comment: "" }); loadAll(); // обновить историю и остатки } catch { alert("Не удалось оформить приход"); } finally { setBusy(false); } }; // уход const handleStockOut = async () => { if ( !outForm.item_id || !outForm.quantity || Number(outForm.quantity) <= 0 ) { alert("Выберите товар и укажите количество"); return; } setBusy(true); try { await stockOut({ item_id: Number(outForm.item_id), quantity: Number(outForm.quantity), comment: outForm.comment, }); setOutForm({ item_id: "", quantity: "", comment: "" }); loadAll(); } catch (err) { // бэк возвращает "недостаточно товара" при нехватке alert(err.response?.data?.error || "Не удалось оформить списание"); } finally { setBusy(false); } }; if (loading) return

Загрузка...

; // общий класс инпутов const inputClass = "bg-slate-800 border border-white/10 rounded px-3 py-2 text-white placeholder-white/40 focus:border-gold focus:outline-none"; return (
{/* === Формы === */}
{/* Приход */}

Приход товара

setInForm({ ...inForm, quantity: e.target.value }) } /> setInForm({ ...inForm, comment: e.target.value }) } />
{/* Уход */}

Списание товара

setOutForm({ ...outForm, quantity: e.target.value }) } /> setOutForm({ ...outForm, comment: e.target.value }) } />
{/* === История (твоя таблица) === */}

История движений

{movements.length === 0 ? (

Движений нет

) : (
{movements.map((m) => ( ))}
Дата Товар Тип Количество Причина Комментарий
{new Date(m.created_at).toLocaleDateString("ru-RU")} {m.item?.name} {m.type === "in" ? "Приход" : "Уход"} {m.quantity} {m.item?.unit} {REASON_LABELS[m.reason] || m.reason} {m.comment || "—"}
)}
); }