<div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:18px;">
    <div style="color:var(--color-text-muted);font-size:13.5px;"><i class="fa-solid fa-users"></i> Staff accounts &amp; login access</div>
    <?php if (Auth::can('users', 'add')): ?>
    <a href="<?= Router::url('admin/users/create') ?>" class="btn btn-primary"><i class="fa-solid fa-plus"></i> Add User</a>
    <?php endif; ?>
</div>

<div class="panel" style="padding:0;overflow-x:auto;">
<table class="data-table">
<thead>
<tr>
    <th>Name</th><th>Username</th><th>Role</th><th>Phone</th><th>Status</th><th>Last Login</th><th></th>
</tr>
</thead>
<tbody>
<?php foreach ($users as $u): ?>
<tr>
    <td><?= e($u['full_name']) ?><?= $u['is_protected'] ? ' <span class="badge badge-gray"><i class="fa-solid fa-shield-halved"></i> Protected</span>' : '' ?></td>
    <td><?= e($u['username']) ?></td>
    <td><span class="badge badge-gold"><?= e($u['role_name']) ?></span></td>
    <td><?= e($u['phone'] ?? '—') ?></td>
    <td><?= $u['is_active'] ? '<span class="badge badge-green"><i class="fa-solid fa-circle-check"></i> Active</span>' : '<span class="badge badge-red"><i class="fa-solid fa-circle-xmark"></i> Inactive</span>' ?></td>
    <td><?= $u['last_login'] ? date('d M Y, h:i A', strtotime($u['last_login'])) : '—' ?></td>
    <td style="white-space:nowrap;">
        <?php if (Auth::can('users', 'edit')): ?>
        <a href="<?= Router::url('admin/users/edit?id=' . $u['id']) ?>" class="btn btn-secondary" style="padding:5px 12px;font-size:12px;"><i class="fa-solid fa-pen"></i></a>
        <?php endif; ?>
        <?php if (Auth::can('users', 'delete') && !$u['is_protected'] && (int)$u['id'] !== Auth::id()): ?>
        <form method="POST" action="<?= Router::url('admin/users/delete') ?>" style="display:inline;" onsubmit="return confirm('Delete this user?');">
            <?= $csrf ?>
            <input type="hidden" name="id" value="<?= $u['id'] ?>">
            <button type="submit" class="btn btn-danger" style="padding:5px 12px;font-size:12px;"><i class="fa-solid fa-trash"></i></button>
        </form>
        <?php endif; ?>
    </td>
</tr>
<?php endforeach; ?>
<?php if (empty($users)): ?>
<tr><td colspan="7" style="text-align:center;color:var(--color-text-muted);padding:30px;">No users yet.</td></tr>
<?php endif; ?>
</tbody>
</table>
</div>
