"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; type Player = { id: string; name: string; position: string; }; type Country = { id: string; name: string; code: string; flagUrl: string | null; defaultFormation: string; defaultLineupPlayerIds: string[]; players: Player[]; }; type Lineup = { id: string; countryId: string; formation: string; playerIds: string[]; }; type Match = { id: string; homeTeam: Country; awayTeam: Country; lineups: Lineup[]; }; const FORMATIONS: Record = { "4-3-3": { def: 4, mid: 3, fwd: 3 }, "4-4-2": { def: 4, mid: 4, fwd: 2 }, "4-5-1": { def: 4, mid: 5, fwd: 1 }, "3-5-2": { def: 3, mid: 5, fwd: 2 }, "3-4-3": { def: 3, mid: 4, fwd: 3 }, "5-3-2": { def: 5, mid: 3, fwd: 2 }, "5-4-1": { def: 5, mid: 4, fwd: 1 }, }; export default function MatchLineupManager({ match }: { match: Match }) { const router = useRouter(); const homeLineup = match.lineups.find((l) => l.countryId === match.homeTeam.id); const awayLineup = match.lineups.find((l) => l.countryId === match.awayTeam.id); const [homeFormation, setHomeFormation] = useState(homeLineup?.formation ?? match.homeTeam.defaultFormation); const [homePlayerIds, setHomePlayerIds] = useState(homeLineup?.playerIds ?? match.homeTeam.defaultLineupPlayerIds); const [awayFormation, setAwayFormation] = useState(awayLineup?.formation ?? match.awayTeam.defaultFormation); const [awayPlayerIds, setAwayPlayerIds] = useState(awayLineup?.playerIds ?? match.awayTeam.defaultLineupPlayerIds); const [loading, setLoading] = useState(false); const [msg, setMsg] = useState<{ text: string; type: "error" | "success" } | null>(null); function loadDefaultLineup(team: "home" | "away") { if (team === "home") { setHomeFormation(match.homeTeam.defaultFormation); setHomePlayerIds(match.homeTeam.defaultLineupPlayerIds); setMsg({ text: "ترکیب پیش‌فرض میزبان بارگذاری شد", type: "success" }); } else { setAwayFormation(match.awayTeam.defaultFormation); setAwayPlayerIds(match.awayTeam.defaultLineupPlayerIds); setMsg({ text: "ترکیب پیش‌فرض میهمان بارگذاری شد", type: "success" }); } setTimeout(() => setMsg(null), 3000); } async function handleSave() { // چک کنیم هر دو تیم 11 نفر داشته باشن if (homePlayerIds.length !== 11 || awayPlayerIds.length !== 11) { setMsg({ text: "هر تیم باید 11 بازیکن داشته باشد", type: "error" }); return; } setLoading(true); const res = await fetch(`/api/admin/matches/${match.id}/lineup`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify([ { countryId: match.homeTeam.id, formation: homeFormation, playerIds: homePlayerIds }, { countryId: match.awayTeam.id, formation: awayFormation, playerIds: awayPlayerIds }, ]), }); if (res.ok) { setMsg({ text: "ترکیب‌ها ذخیره شد", type: "success" }); router.refresh(); } else { const data = await res.json(); setMsg({ text: data.error || "خطا در ذخیره", type: "error" }); } setLoading(false); } return (

ترکیب تیم‌ها

{msg && (
{msg.text}
)}
{/* تیم میزبان */} loadDefaultLineup("home")} /> {/* تیم میهمان */} loadDefaultLineup("away")} />
); } function TeamLineupEditor({ team, formation, selectedPlayerIds, onFormationChange, onPlayersChange, onLoadDefault }: { team: Country; formation: string; selectedPlayerIds: string[]; onFormationChange: (f: string) => void; onPlayersChange: (ids: string[]) => void; onLoadDefault: () => void; }) { const fmt = FORMATIONS[formation] ?? FORMATIONS["4-3-3"]; const gkPlayers = team.players.filter((p) => p.position === "GK"); const defPlayers = team.players.filter((p) => p.position === "DEF"); const midPlayers = team.players.filter((p) => p.position === "MID"); const fwdPlayers = team.players.filter((p) => p.position === "FWD"); const selectedGk = selectedPlayerIds.filter((id) => gkPlayers.find((p) => p.id === id)); const selectedDef = selectedPlayerIds.filter((id) => defPlayers.find((p) => p.id === id)); const selectedMid = selectedPlayerIds.filter((id) => midPlayers.find((p) => p.id === id)); const selectedFwd = selectedPlayerIds.filter((id) => fwdPlayers.find((p) => p.id === id)); function togglePlayer(playerId: string, position: string) { if (selectedPlayerIds.includes(playerId)) { onPlayersChange(selectedPlayerIds.filter((id) => id !== playerId)); } else { const posPlayers = selectedPlayerIds.filter((id) => { const p = team.players.find((pl) => pl.id === id); return p?.position === position; }); let maxCount = 1; if (position === "DEF") maxCount = fmt.def; else if (position === "MID") maxCount = fmt.mid; else if (position === "FWD") maxCount = fmt.fwd; if (posPlayers.length >= maxCount) return; onPlayersChange([...selectedPlayerIds, playerId]); } } return (
{team.flagUrl}

{team.name}

{/* انتخاب فرمیشن */}
{/* نمایش تعداد */}
انتخاب شده: {selectedPlayerIds.length}/11 · GK: {selectedGk.length}/1 · DEF: {selectedDef.length}/{fmt.def} · MID: {selectedMid.length}/{fmt.mid} · FWD: {selectedFwd.length}/{fmt.fwd}
{/* لیست بازیکنان */}
{["GK", "DEF", "MID", "FWD"].map((pos) => { const posList = team.players.filter((p) => p.position === pos); return (
{pos}
{posList.map((p) => ( ))}
); })}
); }