"use client"; import { useEffect, useState } from "react"; import { Trash2, Plus, Pencil, X } from "lucide-react"; import Shell from "@/components/Shell"; import Badge, { type BadgeTone } from "@/components/Badge"; import Empty from "@/components/Empty"; import { TableSkeleton } from "@/components/Skeleton"; import { api } from "@/lib/api"; import { toastError } from "@/lib/toast"; type Tournament = { id: number; title: string; description: string; entry_fee: number; prizes: number[]; prize_pool: number; status: string; players: number; starts_local: string; ends_local: string; }; const STATUS: Record = { active: { t: "در حال برگزاری", tone: "green" }, upcoming: { t: "به‌زودی", tone: "gold" }, ended: { t: "پایان‌یافته", tone: "gray" }, }; const EMPTY = { title: "", description: "", entry_fee: 0, prizes: "", starts_at: "", ends_at: "", }; // "2026-07-07 13:00" → "2026-07-07T13:00" (ورودیِ datetime-local) const toLocalInput = (s: string) => (s || "").replace(" ", "T"); export default function TournamentsPage() { const [list, setList] = useState([]); const [form, setForm] = useState({ ...EMPTY }); const [editingId, setEditingId] = useState(null); const [busy, setBusy] = useState(false); const [loading, setLoading] = useState(true); async function load() { setLoading(true); try { const r = await api.get<{ tournaments: Tournament[] }>("/tournaments"); setList(r.tournaments || []); } finally { setLoading(false); } } useEffect(() => { load(); }, []); function startEdit(t: Tournament) { setEditingId(t.id); setForm({ title: t.title, description: t.description || "", entry_fee: t.entry_fee, prizes: (t.prizes || []).join(","), starts_at: toLocalInput(t.starts_local), ends_at: toLocalInput(t.ends_local), }); window.scrollTo({ top: document.body.scrollHeight, behavior: "smooth" }); } function cancelEdit() { setEditingId(null); setForm({ ...EMPTY }); } async function submit(e: React.FormEvent) { e.preventDefault(); setBusy(true); try { const payload = { title: form.title, description: form.description, entry_fee: Number(form.entry_fee), prizes: form.prizes .split(",") .map((s) => parseInt(s.trim(), 10)) .filter((n) => !isNaN(n)), starts_at: form.starts_at, ends_at: form.ends_at, }; if (editingId) { await api.put("/tournaments", { id: editingId, ...payload }); } else { await api.post("/tournaments", payload); } cancelEdit(); await load(); } catch (e) { toastError((e as Error).message); } finally { setBusy(false); } } async function remove(id: number) { if (!confirm("حذف تورنومنت؟")) return; await api.del(`/tournaments?id=${id}`); if (editingId === id) cancelEdit(); await load(); } return ( {loading ? ( ) : list.length === 0 ? ( ) : (
{list.map((t) => { const s = STATUS[t.status] || STATUS.ended; return ( ); })}
# عنوان ورودی جوایز وضعیت بازیکنان شروع (تهران) پایان (تهران)
{t.id} {t.title} {t.entry_fee} {(t.prizes || []).join("، ")} {s.t} {t.players} {t.starts_local} {t.ends_local}
)}

{editingId ? ( <> ویرایش تورنومنت #{editingId} ) : ( <> افزودن تورنومنت )}

برای فعال‌شدنِ فوری، زمانِ شروع را کمی قبل از الان بگذارید. برد ۱۰۰ و شرکت ۲۵ امتیاز.

); }