<div class="page-header">
    <div>
        <a href="<?= APP_URL ?>/dashboard" class="btn btn-ghost btn-sm mb-4" style="padding-left:0;">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><line x1="19" y1="12" x2="5" y2="12"></line><polyline points="12 19 5 12 12 5"></polyline></svg>
            Back to Dashboard
        </a>
        <h1>Users & Roles</h1>
        <div class="page-subtitle">Manage your team members and their permission levels.</div>
    </div>
    <div class="flex gap-2">
        <?php if (!PlanGuard::checkLimit('users')['allowed']): ?>
            <button class="btn btn-primary" onclick="alert('User limit reached for your current plan. Please upgrade.')">Invite Member</button>
        <?php else: ?>
            <a href="<?= APP_URL ?>/users/create" class="btn btn-primary">
                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><line x1="12" y1="5" x2="12" y2="19"></line><line x1="5" y1="12" x2="19" y2="12"></line></svg>
                Invite Member
            </a>
        <?php endif; ?>
    </div>
</div>

<div class="card mb-6">
    <div class="table-wrapper">
        <table>
            <thead>
                <tr>
                    <th>User</th>
                    <th>Email</th>
                    <th>Role</th>
                    <th>Joined</th>
                    <th>Status</th>
                    <th style="width: 80px;"></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($users as $u): ?>
                <tr>
                    <td>
                        <div class="flex items-center gap-3">
                            <div class="bg-primary text-white rounded-full w-8 h-8 flex items-center justify-center font-bold">
                                <?= strtoupper(substr($u['name'], 0, 1)) ?>
                            </div>
                            <div class="font-bold"><?= e($u['name']) ?></div>
                        </div>
                    </td>
                    <td class="text-sm"><?= e($u['email']) ?></td>
                    <td>
                        <span class="badge badge-primary"><?= e($u['role_name'] ?? ucfirst(e($u['role']))) ?></span>
                    </td>
                    <td class="text-sm"><?= date('M j, Y', strtotime($u['created_at'])) ?></td>
                    <td>
                         <span class="badge <?= $u['is_active'] ? 'badge-success' : 'badge-muted' ?>">
                            <?= $u['is_active'] ? 'Active' : 'Inactive' ?>
                         </span>
                    </td>
                    <td class="text-right">
                        <a href="<?= APP_URL ?>/users/<?= $u['id'] ?>/edit" class="btn btn-ghost btn-icon" title="Edit">
                            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M11 4H4a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7"></path><path d="M18.5 2.5a2.121 2.121 0 0 1 3 3L12 15l-4 1 1-4 9.5-9.5z"></path></svg>
                        </a>
                        <form method="POST" action="<?= APP_URL ?>/users/<?= $u['id'] ?>/delete" onsubmit="return confirm('Are you sure you want to delete this user?');" style="display:inline;">
                            <?= Security::csrfField() ?>
                            <button type="submit" class="btn btn-ghost btn-icon text-danger" title="Delete">
                                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polyline points="3 6 5 6 21 6"></polyline><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"></path></svg>
                            </button>
                        </form>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
