"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; import PersianDateField from "@/components/PersianDateField"; type Country = { id: string; name: string }; type Round = { id: string; name: string; number: number }; export default function MatchForm({ countries, rounds, initial, matchId, }: { countries: Country[]; rounds: Round[]; initial?: any; matchId?: string; }) { const router = useRouter(); const [form, setForm] = useState({ homeTeamId: initial?.homeTeamId ?? "", awayTeamId: initial?.awayTeamId ?? "", stage: initial?.stage ?? "GROUP", status: initial?.status ?? "SCHEDULED", matchDate: initial?.matchDate ?? "", homeScore: initial?.homeScore ?? "", awayScore: initial?.awayScore ?? "", roundId: initial?.roundId ?? "", }); const [loading, setLoading] = useState(false); const [error, setError] = useState(""); async function handleSubmit(e: React.FormEvent) { e.preventDefault(); if (!form.matchDate) { setError("تاریخ و ساعت بازی را انتخاب کنید."); return; } setLoading(true); const payload = { ...form, homeScore: form.homeScore !== "" ? parseInt(String(form.homeScore), 10) : null, awayScore: form.awayScore !== "" ? parseInt(String(form.awayScore), 10) : null, roundId: form.roundId || null, }; const res = await fetch(matchId ? `/api/matches/${matchId}` : "/api/matches", { method: matchId ? "PUT" : "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(payload), }); if (res.ok) { router.push("/admin/matches"); router.refresh(); } else { const d = await res.json(); setError(d.error ?? "خطا"); } setLoading(false); } const stages = [ { value: "GROUP", label: "مرحله گروهی" }, { value: "ROUND_OF_16", label: "یکهشتم نهایی" }, { value: "QUARTER_FINAL", label: "یکچهارم نهایی" }, { value: "SEMI_FINAL", label: "نیمهنهایی" }, { value: "THIRD_PLACE", label: "ردهبندی" }, { value: "FINAL", label: "فینال" }, ]; return (
); }