<?php
/**
 * @var object $users
 * @var array $roles
 * @var array $branches
 * @var string $search
 * @var string $status
 * @var string $roleId
 */
?>
<div class="page-title mb-4">
    <h5><i class="fas fa-users me-2 text-gold"></i>Users</h5>
</div>

<div class="glass-card p-0">
    <div class="card-header">
        <div class="card-header-left">
            <i class="fas fa-user card-icon"></i>
            <h6 class="card-title">All Users</h6>
        </div>
        <div class="card-header-actions">
            <?php if (can('users_create')): ?>
                <a href="<?= url('users/create') ?>" class="btn btn-gold btn-sm">
                    <i class="fas fa-plus me-1"></i>Create User
                </a>
            <?php endif; ?>
        </div>
    </div>
    <div class="card-body">
        <form method="GET" class="row g-2 mb-3">
            <div class="col-md-5">
                <input type="text" name="search" class="form-control form-control-sm bg-transparent" 
                       placeholder="Search users..." value="<?= e($search) ?>">
            </div>
            <div class="col-md-3">
                <select name="role_id" class="form-select form-select-sm bg-transparent">
                    <option value="">All Roles</option>
                    <?php foreach ($roles as $role): ?>
                        <option value="<?= $role->id ?>" <?= $roleId == $role->id ? 'selected' : '' ?>>
                            <?= e($role->name) ?>
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-2">
                <select name="status" class="form-select form-select-sm bg-transparent">
                    <option value="">All Status</option>
                    <option value="active" <?= $status === 'active' ? 'selected' : '' ?>>Active</option>
                    <option value="inactive" <?= $status === 'inactive' ? 'selected' : '' ?>>Inactive</option>
                    <option value="suspended" <?= $status === 'suspended' ? 'selected' : '' ?>>Suspended</option>
                </select>
            </div>
            <div class="col-md-2">
                <button type="submit" class="btn btn-outline-gold btn-sm w-100">
                    <i class="fas fa-search me-1"></i>Filter
                </button>
            </div>
        </form>

        <div class="table-responsive">
            <table class="table table-hover align-middle mb-0">
                <thead>
                    <tr>
                        <th>User</th>
                        <th>Username</th>
                        <th>Email</th>
                        <th>Role</th>
                        <th>Branch</th>
                        <th class="text-center">Status</th>
                        <th class="text-end">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($users->data)): ?>
                    <tr>
                        <td colspan="7" class="text-center py-4 text-muted">No users found.</td>
                    </tr>
                    <?php else: ?>
                        <?php foreach ($users->data as $u): ?>
                        <tr>
                            <td>
                                <div class="d-flex align-items-center gap-2">
                                    <div class="rounded-circle d-flex align-items-center justify-content-center"
                                         style="width:36px;height:36px;background:var(--gold);color:var(--dark);font-weight:700;font-size:0.85rem;flex-shrink:0;">
                                        <?= strtoupper(substr($u->first_name, 0, 1) . substr($u->last_name, 0, 1)) ?>
                                    </div>
                                    <div>
                                        <strong><?= e($u->first_name . ' ' . $u->last_name) ?></strong>
                                        <?php if ($u->is_superadmin): ?>
                                            <span class="badge bg-gold ms-1" style="font-size:0.65rem;">Super</span>
                                        <?php endif; ?>
                                    </div>
                                </div>
                            </td>
                            <td><?= e($u->username) ?></td>
                            <td><?= e($u->email) ?></td>
                            <td><?= e($u->role_name ?? '-') ?></td>
                            <td><?= e($u->branch_name ?? '-') ?></td>
                            <td class="text-center">
                                <span class="badge bg-<?= $u->status === 'active' ? 'success' : ($u->status === 'inactive' ? 'secondary' : 'danger') ?>">
                                    <?= e(ucfirst($u->status)) ?>
                                </span>
                            </td>
                            <td class="text-end">
                                <?php if (can('users_edit')): ?>
                                    <a href="<?= url('users/edit/' . $u->id) ?>" class="btn btn-sm btn-outline-gold me-1" title="Edit">
                                        <i class="fas fa-edit"></i>
                                    </a>
                                <?php endif; ?>
                                <?php if (can('users_delete') && !$u->is_superadmin): ?>
                                    <button type="button" class="btn btn-sm btn-outline-danger" 
                                            onclick="confirmDelete('<?= url('users/delete/' . $u->id) ?>')" title="Delete">
                                        <i class="fas fa-trash-alt"></i>
                                    </button>
                                <?php endif; ?>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>

        <?php if ($users->totalPages > 1): ?>
            <div class="mt-3">
                <nav>
                    <ul class="pagination pagination-sm justify-content-center mb-0">
                        <?php for ($p = 1; $p <= $users->totalPages; $p++): ?>
                            <li class="page-item <?= $p === $users->currentPage ? 'active' : '' ?>">
                                <a class="page-link" href="<?= url('users?page=' . $p) ?>"><?= $p ?></a>
                            </li>
                        <?php endfor; ?>
                    </ul>
                </nav>
            </div>
        <?php endif; ?>
    </div>
</div>
