@model IEnumerable<EventRegistrationDto>
@{
    ViewData["Title"] = "Registrations";
    var telId = ViewBag.TournamentEventLevelId as int?;
    var eventName = ViewBag.EventName as string;
    var levelName = ViewBag.LevelName as string;
}

<div class="d-flex justify-content-between align-items-center mb-3">
    <div>
        <h2>Registrations</h2>
        @if (eventName != null) { <p class="text-muted mb-0">@eventName - @levelName</p> }
    </div>
    @if (telId.HasValue)
    {
        <a asp-action="Register" asp-route-tournamentEventLevelId="@telId" class="btn btn-primary">Register Student</a>
    }
</div>

<partial name="_Notification" />

<table class="table table-striped table-hover">
    <thead class="table-dark">
        <tr>
            <th>Student</th>
            <th>School</th>
            <th>Event</th>
            <th>Level</th>
            <th>Registered</th>
            <th>Score</th>
            <th>Actions</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var r in Model)
        {
            <tr>
                <td>@(r.StudentName ?? r.RelayTeamName ?? "N/A")</td>
                <td>@r.SchoolName</td>
                <td>@r.EventName</td>
                <td>@r.EventLevelName</td>
                <td>@r.RegisteredAt.ToString("MMM d, yyyy HH:mm")</td>
                <td>
                    @if (r.Score != null)
                    {
                        <span class="badge bg-success">@r.Score.RawPerformance</span>
                        @if (r.Score.Placement.HasValue) { <span class="badge bg-warning text-dark">#@r.Score.Placement</span> }
                    }
                    else
                    {
                        <span class="text-muted">-</span>
                    }
                </td>
                <td>
                    <form asp-action="Unregister" method="post" class="d-inline">
                        <input type="hidden" name="eventRegistrationId" value="@r.EventRegistrationId" />
                        @if (telId.HasValue) { <input type="hidden" name="tournamentEventLevelId" value="@telId" /> }
                        <button type="submit" class="btn btn-sm btn-outline-danger" onclick="return confirm('Unregister?')">Remove</button>
                    </form>
                </td>
            </tr>
        }
    </tbody>
</table>
