"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; const EVENT_LABELS: Record = { GOAL: { label: "گل", icon: "⚽", color: "bg-green-100 text-green-700" }, ASSIST: { label: "پاس گل", icon: "🎯", color: "bg-blue-100 text-blue-700" }, YELLOW_CARD: { label: "کارت زرد", icon: "🟨", color: "bg-yellow-100 text-yellow-700" }, RED_CARD: { label: "کارت قرمز", icon: "🟥", color: "bg-red-100 text-red-700" }, SECOND_YELLOW: { label: "کارت زرد دوم", icon: "🟨🟥", color: "bg-orange-100 text-orange-700" }, SUBSTITUTION_IN: { label: "ورود تعویضی", icon: "🔄↑", color: "bg-teal-100 text-teal-700" }, SUBSTITUTION_OUT: { label: "خروج تعویضی", icon: "🔄↓", color: "bg-gray-100 text-gray-600" }, INJURY_NO_SUB: { label: "مصدومیت بدون تعویض", icon: "🤕", color: "bg-red-50 text-red-500" }, CLEAN_SHEET: { label: "کلین‌شیت", icon: "🧤", color: "bg-green-100 text-green-700" }, PENALTY_SAVED: { label: "پنالتی گرفته", icon: "🛡️", color: "bg-purple-100 text-purple-700" }, PENALTY_MISSED: { label: "پنالتی از دست داده", icon: "❌", color: "bg-red-100 text-red-600" }, OWN_GOAL: { label: "گل به خودی", icon: "😬", color: "bg-orange-100 text-orange-700" }, EXTRA_TIME_BONUS: { label: "وقت اضافه", icon: "⏱️", color: "bg-indigo-100 text-indigo-700" }, MOTM: { label: "بازیکن برتر", icon: "🌟", color: "bg-yellow-100 text-yellow-700" }, }; export default function MatchEventManager({ match, roundId }: { match: any; roundId: string }) { const router = useRouter(); const [tab, setTab] = useState<"events" | "lineup" | "score">("events"); const [eventForm, setEventForm] = useState({ playerId: "", type: "GOAL", minute: "", extraInfo: "" }); const [score, setScore] = useState({ homeScore: match.homeScore ?? 0, awayScore: match.awayScore ?? 0, status: match.status }); const [loading, setLoading] = useState(false); const [msg, setMsg] = useState(""); const allPlayers = [...match.homeTeam.players, ...match.awayTeam.players]; async function addEvent() { if (!eventForm.playerId || !eventForm.type) return; setLoading(true); const res = await fetch(`/api/admin/matches/${match.id}/events`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ ...eventForm, minute: eventForm.minute ? parseInt(eventForm.minute) : null }), }); if (res.ok) { router.refresh(); setMsg("رویداد ثبت شد"); setEventForm({ playerId: "", type: "GOAL", minute: "", extraInfo: "" }); } else { const d = await res.json(); setMsg(d.error); } setLoading(false); } async function deleteEvent(eventId: string) { await fetch(`/api/admin/matches/${match.id}/events/${eventId}`, { method: "DELETE" }); router.refresh(); } async function updateScore() { setLoading(true); await fetch(`/api/matches/${match.id}`, { method: "PUT", headers: { "Content-Type": "application/json" }, body: JSON.stringify(score), }); router.refresh(); setLoading(false); setMsg("نتیجه ذخیره شد"); } async function calcPoints() { setLoading(true); const res = await fetch(`/api/admin/matches/${match.id}/calc-points`, { method: "POST" }); const d = await res.json(); setMsg(res.ok ? "امتیازات محاسبه شد" : d.error); setLoading(false); router.refresh(); } return (
{/* پنل چپ - رویدادها */}
{/* تب‌ها */}
{[["events", "رویدادها"], ["lineup", "ترکیب تیم‌ها"], ["score", "نتیجه"]].map(([key, label]) => ( ))}
{msg &&
{msg}
} {tab === "events" && (
{/* فرم ثبت رویداد */}

ثبت رویداد جدید

setEventForm({ ...eventForm, minute: e.target.value })} placeholder="مثلاً 45" className="w-full border rounded-xl px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-green-400" />
setEventForm({ ...eventForm, extraInfo: e.target.value })} placeholder="مثلاً نام بازیکن تعویضی" className="w-full border rounded-xl px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-green-400" />
{/* لیست رویدادها */}
رویدادهای ثبت‌شده ({match.events.length})
{match.events.length === 0 ? (
رویدادی ثبت نشده
) : (
{match.events.map((ev: any) => { const evInfo = EVENT_LABELS[ev.type]; return (
{evInfo?.icon} {evInfo?.label} {ev.player.name} {ev.minute && {ev.minute}'} {ev.extraInfo && ({ev.extraInfo})}
); })}
)}
)} {tab === "lineup" && ( )} {tab === "score" && (

نتیجه بازی

{match.homeTeam.name}
setScore({ ...score, homeScore: parseInt(e.target.value) || 0 })} className="w-20 border-2 rounded-xl px-3 py-3 text-center text-2xl font-bold focus:outline-none focus:border-green-500" />
-
{match.awayTeam.name}
setScore({ ...score, awayScore: parseInt(e.target.value) || 0 })} className="w-20 border-2 rounded-xl px-3 py-3 text-center text-2xl font-bold focus:outline-none focus:border-green-500" />
)}
{/* پنل راست - آمار بازیکنان */}
آمار بازیکنان
{match.playerStats.map((s: any) => (
{s.player.name} {s.points} pts
{s.goals > 0 && ⚽ {s.goals}} {s.assists > 0 && 🎯 {s.assists}} {s.yellowCards > 0 && 🟨 {s.yellowCards}} {s.redCards > 0 && 🟥} {s.cleanSheet && 🧤} {s.minutesPlayed}'
))} {match.playerStats.length === 0 &&
امتیازی محاسبه نشده
}
); } function LineupTab({ match }: { match: any }) { const [homeLineup, setHomeLineup] = useState([]); const [awayLineup, setAwayLineup] = useState([]); const [homeFormation, setHomeFormation] = useState(match.homeTeam.defaultFormation ?? "4-3-3"); const [awayFormation, setAwayFormation] = useState(match.awayTeam.defaultFormation ?? "4-3-3"); const [loading, setLoading] = useState(false); const [msg, setMsg] = useState(""); async function saveLineup() { setLoading(true); await fetch(`/api/admin/matches/${match.id}/lineup`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify([ { countryId: match.homeTeamId, formation: homeFormation, playerIds: homeLineup }, { countryId: match.awayTeamId, formation: awayFormation, playerIds: awayLineup }, ]), }); setMsg("ترکیب ذخیره شد"); setLoading(false); } const togglePlayer = (id: string, team: "home" | "away") => { if (team === "home") setHomeLineup((prev) => prev.includes(id) ? prev.filter((x) => x !== id) : [...prev, id]); else setAwayLineup((prev) => prev.includes(id) ? prev.filter((x) => x !== id) : [...prev, id]); }; return (

ثبت ترکیب رسمی

{msg &&
{msg}
}
{[{ team: match.homeTeam, side: "home" as const, lineup: homeLineup, formation: homeFormation, setFormation: setHomeFormation }, { team: match.awayTeam, side: "away" as const, lineup: awayLineup, formation: awayFormation, setFormation: setAwayFormation } ].map(({ team, side, lineup, formation, setFormation }) => (
{team.flagUrl} {team.name}
setFormation(e.target.value)} placeholder="ترکیب مثلاً 4-3-3" className="w-full border rounded-lg px-3 py-1.5 text-sm mb-2 focus:outline-none focus:ring-2 focus:ring-green-400" />
{team.players.map((p: any) => ( ))}
{lineup.length} بازیکن انتخاب شده
))}
); }