<div class="card">
  <div style="display:flex; justify-content:space-between; align-items:center; margin-bottom:16px; flex-wrap:wrap; gap:8px;">
    <span style="font-weight:600; color:#555;">Total: <strong><?= count($users) ?></strong> akun</span>
    <a href="<?= site_url('account/create') ?>" class="btn btn-primary">+ Tambah Akun</a>
  </div>

  <div class="table-responsive">
    <table id="tblAccount" class="dt-table" data-nosort="5,6" data-order='[[2,"asc"]]' data-pglength="10">
      <thead>
        <tr>
          <th>#</th>
          <th>Nama</th>
          <th>Username</th>
          <th>Role</th>
          <th>Status</th>
          <th>Login Terakhir</th>
          <th style="text-align:center;">Aksi</th>
        </tr>
      </thead>
      <tbody>
        <?php if (empty($users)): ?>
          <tr><td colspan="7" style="text-align:center;color:#999;padding:30px;">Belum ada akun.</td></tr>
        <?php else: foreach ($users as $i => $u): ?>
          <tr>
            <td data-label="#"><?= $i+1 ?></td>
            <td data-label="Nama">
              <strong><?= htmlspecialchars($u->name) ?></strong>
              <?php if ($u->id == $this->session->userdata('user_id')): ?>
                <span class="badge badge-primary" style="margin-left:4px;">Anda</span>
              <?php endif; ?>
            </td>
            <td data-label="Username">
              <code style="background:#f0f0f0;padding:2px 6px;border-radius:4px;font-size:12px;">
                <?= htmlspecialchars($u->username) ?>
              </code>
            </td>
            <td data-label="Role">
              <?php if ($u->role === 'admin'): ?>
                <span class="badge badge-primary">👑 Admin</span>
              <?php else: ?>
                <span class="badge badge-success">👤 User</span>
              <?php endif; ?>
            </td>
            <td data-label="Status">
              <?php if ($u->is_active): ?>
                <span class="badge badge-success">✓ Aktif</span>
              <?php else: ?>
                <span class="badge" style="background:#FFEBEE;color:#C62828;">✗ Nonaktif</span>
              <?php endif; ?>
            </td>
            <td data-label="Login" style="color:#888;font-size:12px;">
              <?= $u->last_login ? date('d/m/Y H:i', strtotime($u->last_login)) : '—' ?>
            </td>
            <td data-label="Aksi" class="td-actions" style="white-space:nowrap;text-align:center;">
              <a href="<?= site_url('account/edit/'.$u->id) ?>"
                 class="btn btn-warning btn-sm">✏️ Edit</a>
              <?php if ($u->id != $this->session->userdata('user_id')): ?>
                <a href="<?= site_url('account/delete/'.$u->id) ?>"
                   class="btn btn-danger btn-sm"
                   onclick="return confirm('Hapus akun <?= htmlspecialchars($u->name) ?>?')">🗑</a>
              <?php endif; ?>
            </td>
          </tr>
        <?php endforeach; endif; ?>
      </tbody>
    </table>
  </div>
</div>
