<?php $pageTitle = 'Manage Users'; include __DIR__ . '/../shared/header.php'; ?>

<div class="page-header">
  <div>
    <div class="page-title"><i class="bi bi-people me-2"></i>Users</div>
    <div class="page-subtitle">Manage system users and laboratory assignments</div>
  </div>
  <a href="<?= APP_URL ?>/index.php?page=users&action=create" class="btn btn-primary">
    <i class="bi bi-plus-circle me-1"></i>Add User
  </a>
</div>

<div class="card">
  <div class="card-body p-0">
    <div class="table-responsive">
      <table class="table table-hover mb-0">
        <thead class="table-light">
          <tr><th>Name</th><th>Email</th><th>Phone</th><th>Role</th><th>Assigned Labs</th><th>Status</th><th>Joined</th><th>Actions</th></tr>
        </thead>
        <tbody>
        <?php foreach ($users as $u): ?>
          <tr>
            <td class="fw-semibold"><?= e($u['officer_name']) ?></td>
            <td><?= e($u['email']) ?></td>
            <td><?= e($u['phone_number'] ?? '—') ?></td>
            <td><span class="badge bg-secondary"><?= e(ucfirst(str_replace('_',' ',$u['role']))) ?></span></td>
            <td><small class="text-muted"><?= e($u['labs'] ?? '—') ?></small></td>
            <td>
              <?php if ($u['is_active']): ?>
                <span class="badge bg-success">Active</span>
              <?php else: ?>
                <span class="badge bg-danger">Inactive</span>
              <?php endif; ?>
            </td>
            <td><small><?= date('d M Y', strtotime($u['date_created'])) ?></small></td>
            <td>
              <a href="<?= APP_URL ?>/index.php?page=users&action=edit&id=<?= $u['user_id'] ?>" class="btn btn-sm btn-outline-primary me-1"><i class="bi bi-pencil"></i></a>
              <?php if ($u['user_id'] !== (int)$_SESSION['user_id']): ?>
              <a href="<?= APP_URL ?>/index.php?page=users&action=delete&id=<?= $u['user_id'] ?>"
                 class="btn btn-sm btn-outline-danger" data-confirm="Delete this user?"><i class="bi bi-trash"></i></a>
              <?php endif; ?>
            </td>
          </tr>
        <?php endforeach; ?>
        <?php if (empty($users)): ?>
          <tr><td colspan="8" class="text-center text-muted py-4">No users found.</td></tr>
        <?php endif; ?>
        </tbody>
      </table>
    </div>
  </div>
</div>

<?php include __DIR__ . '/../shared/footer.php'; ?>
