<?= $this->extend('layouts/main') ?>

<?= $this->section('content') ?>
<div class="bg-primary text-white py-4 mb-4" style="background: linear-gradient(135deg, #0d6efd 0%, #00d2ff 100%) !important;">
    <div class="container fade-in">
        <h2 class="fw-bold"><i class="fa-solid fa-users me-2"></i>User Management</h2>
        <a href="<?= base_url('admin') ?>" class="text-white text-decoration-none small"><i class="fa-solid fa-arrow-left me-1"></i> Back to Dashboard</a>
    </div>
</div>

<div class="container mb-5 fade-in">
    <?php if(session()->getFlashdata('error')): ?>
        <div class="alert alert-danger mb-4"><?= session()->getFlashdata('error') ?></div>
    <?php endif; ?>
    
    <div class="card border-0 shadow-sm rounded-4">
        <div class="card-body p-4">
            <div class="table-responsive">
                <table class="table table-hover align-middle">
                    <thead class="bg-light">
                        <tr>
                            <th>ID</th>
                            <th>Name</th>
                            <th>Email</th>
                            <th>Role</th>
                            <th>Status</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if(empty($users)): ?>
                        <tr><td colspan="6" class="text-center text-muted py-4">No users found. <a href="<?= base_url('setup') ?>">Did you setup the database?</a></td></tr>
                        <?php else: ?>
                            <?php foreach($users as $user): ?>
                            <tr>
                                <td><?= esc($user['id']) ?></td>
                                <td><div class="d-flex align-items-center"><div class="bg-primary text-white rounded-circle p-2 me-2" style="width: 35px; height: 35px; text-align: center; line-height: 20px;"><?= substr($user['name'], 0, 1) ?></div> <?= esc($user['name']) ?></div></td>
                                <td><?= esc($user['email']) ?></td>
                                <td><span class="badge bg-<?= $user['role'] == 'admin' ? 'danger' : ($user['role'] == 'agent' ? 'warning text-dark' : 'info') ?>"><?= strtoupper($user['role']) ?></span></td>
                                <td><span class="badge bg-<?= $user['status'] == 'active' ? 'success' : 'secondary' ?>"><?= ucfirst($user['status']) ?></span></td>
                                <td>
                                    <button class="btn btn-sm btn-outline-primary rounded-circle" title="Edit"><i class="fa-solid fa-pen"></i></button>
                                    <button class="btn btn-sm btn-outline-danger rounded-circle" title="Deactivate/Delete"><i class="fa-solid fa-trash"></i></button>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
<?= $this->endSection() ?>
