@model IEnumerable<EventRegistrationDto>
@{
    ViewData["Title"] = "Field Event Scoring";
    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>Field Event Scoring</h2>
        <p class="text-muted">@eventName - @levelName</p>
    </div>
    <div class="d-flex gap-2">
        <form asp-action="CalculateScores" method="post">
            <input type="hidden" name="tournamentEventLevelId" value="@telId" />
            <button type="submit" class="btn btn-warning">Calculate Points</button>
        </form>
        <form asp-action="CalculatePlacements" method="post">
            <input type="hidden" name="tournamentEventLevelId" value="@telId" />
            <button type="submit" class="btn btn-success">Calculate Placements</button>
        </form>
    </div>
</div>

<partial name="_Notification" />

<div class="card shadow-sm">
    <div class="card-body p-0">
        <table class="table table-hover mb-0">
            <thead class="table-dark">
                <tr>
                    <th>#</th>
                    <th>Student</th>
                    <th>School</th>
                    <th>Performance</th>
                    <th>Points</th>
                    <th>Placement</th>
                    <th>Pts</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
                @{ int idx = 1; }
                @foreach (var r in Model.OrderBy(r => r.Score?.Placement ?? int.MaxValue))
                {
                    <tr>
                        <td>@idx</td>
                        <td>@r.StudentName</td>
                        <td>@r.SchoolName</td>
                        <td>
                            <form asp-action="RecordScore" method="post" class="d-flex gap-1">
                                <input type="hidden" name="EventRegistrationId" value="@r.EventRegistrationId" />
                                <input type="number" step="0.01" name="RawPerformance" value="@r.Score?.RawPerformance" class="form-control form-control-sm" style="width:100px" />
                                <button type="submit" class="btn btn-sm btn-outline-primary">Save</button>
                            </form>
                        </td>
                        <td>@(r.Score?.CalculatedPoints ?? 0)</td>
                        <td>
                            @if (r.Score?.Placement != null)
                            {
                                <span class="badge bg-warning text-dark">#@r.Score.Placement</span>
                            }
                        </td>
                        <td>@(r.Score?.PlacementPoints ?? 0)</td>
                        <td></td>
                    </tr>
                    idx++;
                }
            </tbody>
        </table>
    </div>
</div>
