"use client"; import { useEffect, useRef, useState } from "react"; import { Upload, Loader2 } from "lucide-react"; import Shell from "@/components/Shell"; import EditableTable, { Col } from "@/components/EditableTable"; import Thumb from "@/components/Thumb"; import { api, getAuth, ASSET_BASE } from "@/lib/api"; import { toastError, toastSuccess } from "@/lib/toast"; type Row = Record; const COLS: Col[] = [ { key: "id", label: "شناسه" }, { key: "title", label: "عنوان" }, { key: "price_coins", label: "قیمت (سکه)", type: "number" }, { key: "vip", label: "VIP رایگان", type: "bool" }, { key: "sort", label: "ترتیب", type: "number" }, ]; const API = process.env.NEXT_PUBLIC_API_URL || "https://hakem.approagency.ir/admin/api"; export default function CarpetsPage() { const [rows, setRows] = useState([]); const [ver, setVer] = useState(1); // برای تازه‌سازیِ کشِ تصویر پس از آپلود async function load() { const r = await api.get<{ carpets: Row[] }>("/carpets"); setRows(r.carpets || []); setVer((v) => v + 1); } useEffect(() => { load(); }, []); return (

هر فرش به‌جای میزِ بازی استفاده می‌شود. پس از ساختِ ردیف، تصویرِ jpg آپلود کنید. شناسهٔ classic یعنی میزِ پیش‌فرضِ بدونِ تصویر.

{ await api.post("/carpets", row); await load(); }} onDelete={async (id) => { if (!confirm("حذف فرش؟")) return; await api.del(`/carpets?id=${encodeURIComponent(id)}`); await load(); }} preview={(row) => String(row.id) === "classic" ? ( پیش‌فرض ) : ( ) } extra={(row) => } />
); } function UploadBtn({ id, onDone }: { id: string; onDone: () => void }) { const ref = useRef(null); const [busy, setBusy] = useState(false); async function upload(file: File) { setBusy(true); try { const fd = new FormData(); fd.append("image", file); const auth = getAuth(); const res = await fetch(`${API}/carpets/${encodeURIComponent(id)}/upload`, { method: "POST", headers: auth ? { Authorization: `Basic ${auth}` } : {}, body: fd, }); if (!res.ok) throw new Error("آپلود ناموفق بود"); toastSuccess("تصویر آپلود شد"); onDone(); } catch (e) { toastError((e as Error).message); } finally { setBusy(false); } } return ( <> { const f = e.target.files?.[0]; if (f) upload(f); e.target.value = ""; }} /> ); }