<?php $title = 'AdminSB Pro - Users'; ?>

<div class="d-sm-flex align-items-center justify-content-between mb-4">
    <h1 class="h3 mb-0 text-gray-800">User Management</h1>
    <a href="<?= $baseUrl ?>/users/create" class="btn btn-primary btn-sm shadow-sm"><i class="fas fa-plus fa-sm text-white-50"></i> Add New User</a>
</div>

<div class="card shadow mb-4">
    <div class="card-header py-3">
        <h6 class="m-0 font-weight-bold text-primary">All Users (<?= $total ?>)</h6>
    </div>
    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-bordered" id="usersTable" width="100%" cellspacing="0">
                <thead>
                    <tr><th>ID</th><th>Name</th><th>Email</th><th>Role</th><th>Status</th><th>Created</th><th>Actions</th></tr>
                </thead>
                <tbody>
                    <?php foreach ($users as $user): ?>
                    <tr>
                        <td><?= $user->id ?></td>
                        <td><?= htmlspecialchars($user->name) ?></td>
                        <td><?= htmlspecialchars($user->email) ?></td>
                        <td>
                            <span class="badge badge-<?= $user->role === 'admin' ? 'primary' : ($user->role === 'editor' ? 'info' : 'secondary') ?>">
                                <?= ucfirst($user->role) ?>
                            </span>
                        </td>
                        <td>
                            <span class="badge badge-<?= $user->status === 'active' ? 'success' : 'danger' ?>">
                                <?= ucfirst($user->status) ?>
                            </span>
                        </td>
                        <td><?= date('M d, Y', strtotime($user->created_at)) ?></td>
                        <td class="table-actions">
                            <a href="<?= $baseUrl ?>/users/<?= $user->id ?>/edit" class="btn btn-sm btn-primary" title="Edit"><i class="fas fa-edit"></i></a>
                            <form method="POST" action="<?= $baseUrl ?>/users/<?= $user->id ?>/delete" style="display:inline" onsubmit="return confirm('Delete this user?')">
                                <button type="submit" class="btn btn-sm btn-danger" title="Delete"><i class="fas fa-trash"></i></button>
                            </form>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                    <?php if (count($users) === 0): ?>
                    <tr><td colspan="7" class="text-center">No users found.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
        <?php if ($lastPage > 1): ?>
        <nav>
            <ul class="pagination justify-content-center">
                <li class="page-item <?= $page <= 1 ? 'disabled' : '' ?>">
                    <a class="page-link" href="<?= $baseUrl ?>/users?page=<?= $page - 1 ?>&search=<?= $search ?>">Previous</a>
                </li>
                <?php for ($i = 1; $i <= $lastPage; $i++): ?>
                <li class="page-item <?= $i === $page ? 'active' : '' ?>">
                    <a class="page-link" href="<?= $baseUrl ?>/users?page=<?= $i ?>&search=<?= $search ?>"><?= $i ?></a>
                </li>
                <?php endfor; ?>
                <li class="page-item <?= $page >= $lastPage ? 'disabled' : '' ?>">
                    <a class="page-link" href="<?= $baseUrl ?>/users?page=<?= $page + 1 ?>&search=<?= $search ?>">Next</a>
                </li>
            </ul>
        </nav>
        <?php endif; ?>
    </div>
</div>
