@model IEnumerable<RoundDto>
@{
    ViewData["Title"] = "Track Event - Rounds";
    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>Track Event Scoring</h2>
        <p class="text-muted">@eventName - @levelName</p>
    </div>
</div>

<partial name="_Notification" />

<div class="card shadow-sm mb-3">
    <div class="card-header bg-dark text-white d-flex justify-content-between align-items-center">
        <h5 class="mb-0">Rounds</h5>
        <form asp-action="CreateRound" method="post" class="d-flex gap-2">
            <input type="hidden" name="TournamentEventLevelId" value="@telId" />
            <select name="RoundType" class="form-select form-select-sm" style="width:120px">
                <option value="Heats">Heats</option>
                <option value="SemiFinal">Semi-Final</option>
                <option value="Final">Final</option>
            </select>
            <input type="number" name="RoundOrder" value="@(Model.Count() + 1)" class="form-control form-control-sm" style="width:60px" />
            <input type="number" name="AdvanceTopN" value="3" class="form-control form-control-sm" style="width:60px" placeholder="Top N" />
            <input type="number" name="AdvanceFastestLosers" value="2" class="form-control form-control-sm" style="width:60px" placeholder="FL" />
            <button type="submit" class="btn btn-sm btn-success">Add Round</button>
        </form>
    </div>
    <div class="card-body p-0">
        <table class="table table-hover mb-0">
            <thead><tr><th>Round</th><th>Type</th><th>Status</th><th>Heats</th><th>Advance</th><th>Actions</th></tr></thead>
            <tbody>
                @foreach (var r in Model.OrderBy(r => r.RoundOrder))
                {
                    <tr>
                        <td><strong>Round @r.RoundOrder</strong></td>
                        <td><span class="badge bg-info">@r.RoundType</span></td>
                        <td><span class="badge @(r.Status == RoundStatus.Completed ? "bg-success" : r.Status == RoundStatus.InProgress ? "bg-primary" : "bg-secondary")">@r.Status</span></td>
                        <td>@r.Heats.Count heats</td>
                        <td>Top @(r.AdvanceTopN ?? 0) + @(r.AdvanceFastestLosers ?? 0) FL</td>
                        <td>
                            <a asp-action="ManageRound" asp-route-roundId="@r.RoundId" class="btn btn-sm btn-primary">Manage</a>
                            @if (r.Heats.Count == 0)
                            {
                                <form asp-action="SeedHeats" method="post" class="d-inline">
                                    <input type="hidden" name="roundId" value="@r.RoundId" />
                                    <input type="hidden" name="method" value="Random" />
                                    <button type="submit" class="btn btn-sm btn-outline-success">Seed Heats</button>
                                </form>
                            }
                        </td>
                    </tr>
                }
            </tbody>
        </table>
    </div>
</div>
