<div class="d-flex flex-wrap gap-2 mb-3">
  <?php if (can('pengguna','create')): ?><a class="btn btn-sm btn-primary" href="<?= url('/pengguna/tambah') ?>"><i class="bi bi-person-plus me-1"></i>Tambah Pengguna</a><?php endif; ?>
  <a class="btn btn-sm btn-outline-primary" href="<?= url('/pengguna/peran') ?>"><i class="bi bi-shield-lock me-1"></i>Peran &amp; Hak Akses</a>
  <form class="ms-auto d-flex gap-2" method="get">
    <input type="search" class="form-control form-control-sm" name="cari" value="<?= attr($filters['cari'] ?? '') ?>" placeholder="Cari nama / username / email" style="min-width:220px">
    <select class="form-select form-select-sm" name="role_id" style="width:auto"><option value="">Semua Peran</option>
      <?php foreach ($roles as $r): ?><option value="<?= (int)$r['id'] ?>" <?= (($filters['role_id'] ?? '')==$r['id'])?'selected':'' ?>><?= e($r['name']) ?></option><?php endforeach; ?></select>
    <button class="btn btn-sm btn-light"><i class="bi bi-search"></i></button>
  </form>
</div>
<div class="card">
  <div class="card-header"><i class="bi bi-people me-1"></i>Daftar Pengguna <span class="text-body-secondary fw-normal">(<?= nf($p['total']) ?>)</span></div>
  <div class="table-wrap"><table class="table table-hover table-sticky mb-0">
    <thead><tr><th>Nama Pengguna</th><th>Nama</th><th>Email</th><th>Peran</th><th>Cakupan Data</th><th class="ctr">Terakhir Masuk</th>
      <th class="ctr">Status</th><th class="ctr">Aksi</th></tr></thead>
    <tbody>
    <?php if (!$p['data']): ?><tr><td colspan="8"><div class="empty-state"><i class="bi bi-people"></i>Tidak ada pengguna.</div></td></tr><?php endif; ?>
    <?php foreach ($p['data'] as $u): ?>
      <tr>
        <td class="fw-semibold"><?= e($u['username']) ?>
          <?php if ((int)$u['must_change_password'] === 1): ?><i class="bi bi-key-fill text-warning" data-bs-toggle="tooltip" title="Wajib ganti kata sandi"></i><?php endif; ?>
          <?php if (!empty($u['locked_until']) && strtotime($u['locked_until']) > time()): ?><i class="bi bi-lock-fill text-danger" data-bs-toggle="tooltip" title="Terkunci sampai <?= attr(tglWaktu($u['locked_until'])) ?>"></i><?php endif; ?></td>
        <td><?= e($u['name']) ?></td>
        <td class="small"><?= e($u['email']) ?></td>
        <td><?= badge($u['role_name'], 'primary') ?></td>
        <td class="small text-body-secondary">
          <?php
          $scopeMap = ['all'=>'Seluruh data','own_collection'=>'Invoice yang ditagihnya','own_sales'=>'Invoice sales-nya','own_area'=>'Area-nya'];
          $rr = array_values(array_filter($roles, fn($x)=>(int)$x['id']===(int)$u['role_id']));
          echo e($scopeMap[$rr[0]['data_scope'] ?? 'all'] ?? '-');
          if ($u['collection_officer_name']) echo '<br>Penagih: ' . e($u['collection_officer_name']);
          if ($u['salesperson_name']) echo '<br>Sales: ' . e($u['salesperson_name']);
          ?></td>
        <td class="ctr small"><?= tglWaktu($u['last_login_at']) ?><?php if ($u['last_login_ip']): ?><div class="text-body-tertiary"><?= e($u['last_login_ip']) ?></div><?php endif; ?></td>
        <td class="ctr"><?= (int)$u['is_active'] === 1 ? badge('Aktif','success') : badge('Nonaktif','secondary') ?></td>
        <td class="ctr nowrap">
          <?php if (can('pengguna','edit')): ?>
            <a class="btn btn-sm btn-light py-0 px-1" href="<?= url('/pengguna/'.(int)$u['id'].'/ubah') ?>" title="Ubah"><i class="bi bi-pencil"></i></a>
            <form method="post" action="<?= url('/pengguna/'.(int)$u['id'].'/aktifkan') ?>" class="d-inline"><?= csrf() ?>
              <button class="btn btn-sm btn-light py-0 px-1" title="<?= (int)$u['is_active']===1?'Nonaktifkan':'Aktifkan' ?>"
                data-konfirmasi="<?= (int)$u['is_active']===1?'Nonaktifkan':'Aktifkan' ?> akun <?= attr($u['username']) ?>?" data-konfirmasi-warna="warning">
                <i class="bi bi-<?= (int)$u['is_active']===1?'pause-circle':'play-circle' ?>"></i></button></form>
            <form method="post" action="<?= url('/pengguna/'.(int)$u['id'].'/reset-sandi') ?>" class="d-inline"><?= csrf() ?>
              <button class="btn btn-sm btn-light py-0 px-1 text-warning" title="Reset kata sandi"
                data-konfirmasi="Reset kata sandi <?= attr($u['username']) ?>? Sandi baru akan ditampilkan sekali saja." data-konfirmasi-warna="warning"><i class="bi bi-key"></i></button></form>
          <?php endif; ?>
          <?php if (can('pengguna','delete')): ?>
            <form method="post" action="<?= url('/pengguna/'.(int)$u['id'].'/hapus') ?>" class="d-inline"><?= csrf() ?>
              <button class="btn btn-sm btn-light py-0 px-1 text-danger" data-konfirmasi="Hapus pengguna <?= attr($u['username']) ?>?"><i class="bi bi-trash"></i></button></form>
          <?php endif; ?>
        </td>
      </tr>
    <?php endforeach; ?>
    </tbody></table></div>
  <?= \App\Core\View::partial('partials/pagination', ['p' => $p]) ?>
</div>
