@model IEnumerable<UserDto>
@{
    ViewData["Title"] = "User Management";
    var roles = ViewBag.Roles as List<string> ?? new List<string>();
    var selectedRole = ViewBag.SelectedRole as string;
    var selectedIsActive = ViewBag.SelectedIsActive as bool?;
}

<h2 class="page-header">
    <i class="bi bi-person-gear me-2"></i>User Management
</h2>

<div class="filter-bar">
    <form method="get" class="row g-2 align-items-end">
        <div class="col-auto">
            <label class="form-label mb-1">Role</label>
            <select name="role" class="form-select form-select-sm" style="min-width: 150px;">
                <option value="">All Roles</option>
                @foreach (var r in roles)
                {
                    <option value="@r" selected="@(r == selectedRole)">@r</option>
                }
            </select>
        </div>
        <div class="col-auto">
            <label class="form-label mb-1">Status</label>
            <select name="isActive" class="form-select form-select-sm" style="min-width: 130px;">
                <option value="">All</option>
                <option value="true" selected="@(selectedIsActive == true)">Active</option>
                <option value="false" selected="@(selectedIsActive == false)">Inactive</option>
            </select>
        </div>
        <div class="col-auto">
            <button type="submit" class="btn btn-sm btn-primary">
                <i class="bi bi-funnel me-1"></i>Filter
            </button>
            <a asp-action="Index" class="btn btn-sm btn-outline-secondary ms-1">Clear</a>
        </div>
        <div class="col-auto ms-auto">
            <a asp-action="Create" class="btn btn-sm btn-accent">
                <i class="bi bi-plus-lg me-1"></i>Add User
            </a>
        </div>
    </form>
</div>

<div class="table-responsive">
    <table class="table table-hover">
        <thead>
            <tr>
                <th>Name</th>
                <th>Email</th>
                <th>Role</th>
                <th>School</th>
                <th>Status</th>
                <th style="width: 160px;">Actions</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var user in Model)
            {
                <tr>
                    <td>@user.FullName</td>
                    <td>@user.Email</td>
                    <td><span class="badge bg-secondary">@user.Role</span></td>
                    <td>@(user.SchoolName ?? "—")</td>
                    <td>
                        @if (user.IsActive)
                        {
                            <span class="badge bg-success">Active</span>
                        }
                        else
                        {
                            <span class="badge bg-danger">Inactive</span>
                        }
                    </td>
                    <td>
                        <a asp-action="Edit" asp-route-id="@user.Id" class="btn btn-sm btn-outline-primary" title="Edit">
                            <i class="bi bi-pencil"></i>
                        </a>
                        <form asp-action="ToggleActive" asp-route-id="@user.Id" method="post" class="d-inline">
                            @Html.AntiForgeryToken()
                            @if (user.IsActive)
                            {
                                <button type="submit" class="btn btn-sm btn-outline-warning" title="Deactivate">
                                    <i class="bi bi-person-slash"></i>
                                </button>
                            }
                            else
                            {
                                <button type="submit" class="btn btn-sm btn-outline-success" title="Activate">
                                    <i class="bi bi-person-check"></i>
                                </button>
                            }
                        </form>
                    </td>
                </tr>
            }
            @if (!Model.Any())
            {
                <tr>
                    <td colspan="6" class="text-center text-muted py-3">No users found.</td>
                </tr>
            }
        </tbody>
    </table>
</div>
