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

<div class="d-flex justify-content-between align-items-center mb-3">
    <div>
        <h2>High Jump Scoring</h2>
        <p class="text-muted">@eventName - @levelName</p>
    </div>
    <div class="d-flex gap-2">
        <form asp-action="AddHeight" method="post" class="d-flex gap-1">
            <input type="hidden" name="TournamentEventLevelId" value="@telId" />
            <input type="number" step="0.01" name="Height" placeholder="Height (m)" class="form-control form-control-sm" style="width:120px" />
            <button type="submit" class="btn btn-sm btn-success">Add Height</button>
        </form>
        <form asp-action="CalculateResults" method="post">
            <input type="hidden" name="tournamentEventLevelId" value="@telId" />
            <button type="submit" class="btn btn-warning" onclick="return confirm('Calculate final results?')">Calculate Results</button>
        </form>
    </div>
</div>

<partial name="_Notification" />

<div class="card shadow-sm">
    <div class="card-body p-0 table-responsive">
        <table class="table table-bordered mb-0">
            <thead class="table-dark">
                <tr>
                    <th>Student</th>
                    <th>School</th>
                    @foreach (var height in Model.OrderBy(h => h.SortOrder))
                    {
                        <th class="text-center" style="min-width:80px">
                            @height.Height.ToString("0.00")m
                            <form asp-action="RemoveHeight" method="post" class="d-inline">
                                <input type="hidden" name="heightId" value="@height.HighJumpHeightId" />
                                <button type="submit" class="btn btn-link btn-sm text-danger p-0" onclick="return confirm('Remove?')">x</button>
                            </form>
                        </th>
                    }
                </tr>
            </thead>
            <tbody>
                @if (registrations != null)
                {
                    @foreach (var reg in registrations)
                    {
                        <tr>
                            <td>@reg.StudentName</td>
                            <td>@reg.SchoolName</td>
                            @foreach (var height in Model.OrderBy(h => h.SortOrder))
                            {
                                var attempt = height.Attempts.FirstOrDefault(a => a.EventRegistrationId == reg.EventRegistrationId);
                                <td class="text-center">
                                    @if (attempt != null)
                                    {
                                        @foreach (var a in new[] { attempt.Attempt1, attempt.Attempt2, attempt.Attempt3 })
                                        {
                                            if (a == HighJumpAttemptResult.Clear) { <span class="text-success fw-bold">O</span> }
                                            else if (a == HighJumpAttemptResult.Fail) { <span class="text-danger fw-bold">X</span> }
                                            else if (a == HighJumpAttemptResult.Pass) { <span class="text-muted">-</span> }
                                        }
                                        @if (attempt.IsEliminated) { <br/><span class="badge bg-danger">OUT</span> }
                                    }
                                    else
                                    {
                                        @for (int i = 1; i <= 3; i++)
                                        {
                                            <form asp-action="RecordAttempt" method="post" class="d-inline">
                                                <input type="hidden" name="HighJumpHeightId" value="@height.HighJumpHeightId" />
                                                <input type="hidden" name="EventRegistrationId" value="@reg.EventRegistrationId" />
                                                <input type="hidden" name="AttemptNumber" value="@i" />
                                                <select name="Result" onchange="this.form.submit()" class="form-select form-select-sm d-inline" style="width:50px">
                                                    <option value="">@i</option>
                                                    <option value="Clear">O</option>
                                                    <option value="Fail">X</option>
                                                    <option value="Pass">-</option>
                                                </select>
                                            </form>
                                        }
                                    }
                                </td>
                            }
                        </tr>
                    }
                }
            </tbody>
        </table>
    </div>
</div>
