<?= $this->extend('layouts/admin') ?>
<?= $this->section('content') ?>

<div class="d-flex justify-content-between align-items-center mb-4">
  <h1 class="section-title mb-0">Pengguna</h1>
  <div class="d-flex gap-2">
    <a href="<?= base_url('admin/users/import') ?>" class="btn btn-sm btn-outline-secondary">
      <i class="bi bi-filetype-csv me-1"></i>Import CSV
    </a>
    <a href="<?= base_url('admin/users/new') ?>" class="btn btn-sm btn-primary">
      <i class="bi bi-person-plus me-1"></i>Tambah Pengguna
    </a>
  </div>
</div>

<!-- Filter -->
<form method="get" action="<?= base_url('admin/users') ?>" class="card-stt p-3 mb-4">
  <div class="row g-2 align-items-end">
    <div class="col-md-4">
      <input type="text" name="q" class="form-control form-control-sm"
             placeholder="Cari nama, email, NIM..."
             value="<?= esc($filters['search'] ?? '') ?>">
    </div>
    <div class="col-md-2">
      <select name="role" class="form-select form-select-sm">
        <option value="">Semua Peran</option>
        <?php foreach (['super_admin'=>'Super Admin','admin'=>'Admin','lecturer'=>'Dosen','student'=>'Mahasiswa'] as $v => $l): ?>
          <option value="<?= $v ?>" <?= ($filters['role'] ?? '') === $v ? 'selected' : '' ?>><?= $l ?></option>
        <?php endforeach; ?>
      </select>
    </div>
    <div class="col-md-2">
      <select name="status" class="form-select form-select-sm">
        <option value="">Semua Status</option>
        <option value="active"   <?= ($filters['status'] ?? '') === 'active'   ? 'selected' : '' ?>>Aktif</option>
        <option value="inactive" <?= ($filters['status'] ?? '') === 'inactive' ? 'selected' : '' ?>>Tidak Aktif</option>
      </select>
    </div>
    <div class="col-auto d-flex gap-2">
      <button type="submit" class="btn btn-sm btn-primary">Filter</button>
      <a href="<?= base_url('admin/users') ?>" class="btn btn-sm btn-outline-secondary">Reset</a>
    </div>
  </div>
</form>

<div class="card-stt overflow-hidden">
  <table class="table table-stt mb-0">
    <thead>
      <tr><th>Nama</th><th>Email</th><th>NIM/NIDN</th><th>Peran</th><th>Status</th><th>Aktif Terakhir</th><th></th></tr>
    </thead>
    <tbody>
      <?php if (empty($users)): ?>
        <tr><td colspan="7" class="text-center text-muted py-4">Tidak ada pengguna.</td></tr>
      <?php else: ?>
        <?php foreach ($users as $u): ?>
          <tr>
            <td class="fw-semibold small"><?= esc($u['name']) ?></td>
            <td class="small text-muted"><?= esc($u['email']) ?></td>
            <td class="small text-muted"><?= esc($u['identity_number'] ?? '—') ?></td>
            <td>
              <span class="badge bg-secondary"><?= esc(ucfirst(str_replace('_',' ',$u['role']))) ?></span>
            </td>
            <td>
              <span class="badge <?= $u['status'] === 'active' ? 'bg-success' : 'bg-danger' ?>">
                <?= $u['status'] === 'active' ? 'Aktif' : 'Tidak Aktif' ?>
              </span>
            </td>
            <td class="small text-muted">
              <?= $u['last_active'] ? date('d M Y', strtotime($u['last_active'])) : '—' ?>
            </td>
            <td class="d-flex gap-1">
              <a href="<?= base_url('admin/users/' . $u['user_id'] . '/edit') ?>"
                 class="btn btn-sm btn-outline-secondary"><i class="bi bi-pencil"></i></a>
              <form method="post" action="<?= base_url('admin/users/' . $u['user_id'] . '/toggle') ?>">
                <?= csrf_field() ?>
                <button type="submit" class="btn btn-sm <?= $u['status'] === 'active' ? 'btn-outline-warning' : 'btn-outline-success' ?>">
                  <i class="bi <?= $u['status'] === 'active' ? 'bi-pause-circle' : 'bi-play-circle' ?>"></i>
                </button>
              </form>
            </td>
          </tr>
        <?php endforeach; ?>
      <?php endif; ?>
    </tbody>
  </table>
</div>

<?= $this->endSection() ?>
