@model IEnumerable<StudentDto>
@{
    ViewData["Title"] = "Students";
    var schools = ViewBag.Schools as IEnumerable<SchoolDto>;
    var selectedSchool = ViewBag.SelectedSchool as int?;
    var search = ViewBag.Search as string;
}

<div class="d-flex justify-content-between align-items-center mb-3">
    <h2>Students</h2>
    <a asp-action="Create" class="btn btn-primary">Add Student</a>
</div>

<form method="get" class="row g-2 mb-3">
    <div class="col-auto">
        <select name="schoolId" class="form-select form-select-sm">
            <option value="">All Schools</option>
            @if (schools != null) { @foreach (var s in schools) { <option value="@s.SchoolId" selected="@(selectedSchool == s.SchoolId)">@s.Name</option> } }
        </select>
    </div>
    <div class="col-auto">
        <input type="text" name="search" value="@search" class="form-control form-control-sm" placeholder="Search name..." />
    </div>
    <div class="col-auto">
        <button type="submit" class="btn btn-sm btn-outline-primary">Search</button>
    </div>
</form>

<partial name="_Notification" />

<table class="table table-striped table-hover">
    <thead class="table-dark">
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>DOB</th>
            <th>Sex</th>
            <th>School</th>
            <th>Actions</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var s in Model)
        {
            <tr>
                <td>@s.ExistingStudentId</td>
                <td>@s.FullName</td>
                <td>@s.DateOfBirth.ToString("yyyy-MM-dd")</td>
                <td><span class="badge @(s.Sex == Sex.Male ? "bg-primary" : "bg-danger")">@s.Sex</span></td>
                <td>@s.SchoolName</td>
                <td>
                    <a asp-action="Edit" asp-route-id="@s.StudentId" class="btn btn-sm btn-outline-primary">Edit</a>
                    <a asp-controller="Registration" asp-action="ByStudent" asp-route-studentId="@s.StudentId" class="btn btn-sm btn-outline-info">Registrations</a>
                </td>
            </tr>
        }
    </tbody>
</table>

<partial name="_Pagination" />
