@model RoundDto
@{
    ViewData["Title"] = $"Round {Model.RoundOrder} - {Model.RoundType}";
    var telId = ViewBag.TournamentEventLevelId as int?;
}

<div class="d-flex justify-content-between align-items-center mb-3">
    <div>
        <h2>Round @Model.RoundOrder - @Model.RoundType</h2>
        <span class="badge @(Model.Status == RoundStatus.Completed ? "bg-success" : "bg-primary")">@Model.Status</span>
    </div>
    <div class="d-flex gap-2">
        @if (Model.Status != RoundStatus.Completed)
        {
            <form asp-action="CalculateAdvancement" method="post">
                <input type="hidden" name="roundId" value="@Model.RoundId" />
                <button type="submit" class="btn btn-warning" onclick="return confirm('Calculate advancement?')">Calculate Advancement</button>
            </form>
            @if (ViewBag.HasNextRound == true)
            {
                <form asp-action="PopulateNextRound" method="post">
                    <input type="hidden" name="roundId" value="@Model.RoundId" />
                    <button type="submit" class="btn btn-info" onclick="return confirm('Move the advancing athletes into the next round?')">Send Advancers to Next Round</button>
                </form>
            }
            <form asp-action="CompleteRound" method="post">
                <input type="hidden" name="roundId" value="@Model.RoundId" />
                <button type="submit" class="btn btn-success" onclick="return confirm('Complete this round?')">Complete Round</button>
            </form>
        }
        <a asp-action="Index" asp-route-tournamentEventLevelId="@Model.TournamentEventLevelId" class="btn btn-secondary">Back</a>
    </div>
</div>

<partial name="_Notification" />

@if (!Model.Heats.Any())
{
    <div class="alert alert-info">No heats yet. Go back and seed heats for this round.</div>
}

<ul class="nav nav-tabs mb-3" role="tablist">
    @for (int i = 0; i < Model.Heats.Count; i++)
    {
        var heat = Model.Heats[i];
        <li class="nav-item">
            <button class="nav-link @(i == 0 ? "active" : "")" data-bs-toggle="tab" data-bs-target="#heat-@heat.HeatId" type="button">
                Heat @heat.HeatNumber
                <span class="badge @(heat.Status == HeatStatus.Completed ? "bg-success" : "bg-secondary") ms-1">@heat.Status</span>
            </button>
        </li>
    }
</ul>

<div class="tab-content">
    @for (int i = 0; i < Model.Heats.Count; i++)
    {
        var heat = Model.Heats[i];
        <div class="tab-pane @(i == 0 ? "show active" : "")" id="heat-@heat.HeatId">
            <div class="card shadow-sm">
                <div class="card-body">
                    <form asp-action="SaveTimes" method="post">
                        <input type="hidden" name="heatId" value="@heat.HeatId" />
                        <input type="hidden" name="roundId" value="@Model.RoundId" />
                        <table class="table table-hover">
                            <thead>
                                <tr>
                                    <th>Lane</th>
                                    <th>Student</th>
                                    <th>School</th>
                                    <th>Time (s)</th>
                                    <th>DNS</th>
                                    <th>DNF</th>
                                    <th>DQ</th>
                                    <th>Status</th>
                                </tr>
                            </thead>
                            <tbody>
                                @for (int j = 0; j < heat.HeatLanes.Count; j++)
                                {
                                    var lane = heat.HeatLanes[j];
                                    var rowClass = lane.IsAdvanced
                                        ? (lane.AdvanceReason == AdvanceReason.TopN ? "table-success" : "table-info")
                                        : (lane.IsDNS || lane.IsDNF || lane.IsDQ ? "table-secondary" : "");
                                    <tr class="@rowClass">
                                        <td><strong>@lane.LaneNumber</strong></td>
                                        <td>@lane.StudentName</td>
                                        <td>@lane.SchoolName</td>
                                        <td>
                                            <input type="hidden" name="lanes[@j].HeatLaneId" value="@lane.HeatLaneId" />
                                            <input type="number" step="0.01" name="lanes[@j].Time" value="@lane.Time" class="form-control form-control-sm" style="width:100px" />
                                        </td>
                                        <td><input type="checkbox" name="lanes[@j].IsDNS" value="true" checked="@lane.IsDNS" class="form-check-input" /></td>
                                        <td><input type="checkbox" name="lanes[@j].IsDNF" value="true" checked="@lane.IsDNF" class="form-check-input" /></td>
                                        <td><input type="checkbox" name="lanes[@j].IsDQ" value="true" checked="@lane.IsDQ" class="form-check-input" /></td>
                                        <td>
                                            @if (lane.IsAdvanced)
                                            {
                                                <span class="badge @(lane.AdvanceReason == AdvanceReason.TopN ? "bg-success" : "bg-info")">@lane.AdvanceReason</span>
                                            }
                                        </td>
                                    </tr>
                                }
                            </tbody>
                        </table>
                        <div class="d-flex gap-2">
                            <button type="submit" class="btn btn-primary">Save Times</button>
                            @if (heat.Status != HeatStatus.Completed)
                            {
                                <a href="javascript:void(0)" onclick="document.getElementById('complete-heat-@heat.HeatId').submit()" class="btn btn-success">Complete Heat</a>
                            }
                        </div>
                    </form>
                    <form id="complete-heat-@heat.HeatId" asp-action="CompleteHeat" method="post" style="display:none">
                        <input type="hidden" name="heatId" value="@heat.HeatId" />
                    </form>
                </div>
            </div>
        </div>
    }
</div>
