<?= $this->extend('layouts/main') ?>

<?= $this->section('title') ?>Pengguna admin<?= $this->endSection() ?>

<?= $this->section('content') ?>
<div class="space-y-6">
    <div class="flex flex-col justify-between gap-3 sm:flex-row sm:items-center">
        <div>
            <h1 class="text-2xl font-semibold text-gray-900">Daftar pengguna admin</h1>
            <p class="mt-1 text-sm text-gray-500">Daftar akun yang dapat masuk ke panel admin.</p>
        </div>
        <div class="flex flex-wrap gap-2">
            <a href="<?= site_url('admin/panel/groups') ?>" class="inline-flex items-center justify-center rounded-lg border border-gray-200 bg-white px-4 py-2 text-sm font-medium text-gray-800 shadow-sm hover:bg-gray-50">
                <i class="fa-solid fa-layer-group mr-2 text-gray-500"></i> Kelola grup
            </a>
            <a href="<?= site_url('admin/panel/users/create') ?>" class="inline-flex items-center justify-center rounded-lg bg-blue-600 px-4 py-2 text-sm font-semibold text-white shadow-sm hover:bg-blue-700">
                <i class="fa-solid fa-user-plus mr-2"></i> Tambah pengguna
            </a>
        </div>
    </div>

    <?= view('layouts/partials/admin_alert', ['errors' => $errors ?? []]) ?>

    <section class="overflow-hidden rounded-2xl border border-gray-200 bg-white shadow-sm">
        <div class="border-b border-gray-200 px-5 py-4">
            <h2 class="text-base font-semibold text-gray-900">Pengguna</h2>
            <p class="text-xs text-gray-500">Reset password untuk akun yang sudah ada. Pembuatan akun tidak mengizinkan grup webmaster.</p>
        </div>
        <div class="overflow-x-auto">
            <table class="min-w-full divide-y divide-gray-200 text-sm">
                <thead class="border-y border-gray-200 bg-gray-50 text-left text-xs font-semibold uppercase tracking-wide text-gray-500">
                    <tr>
                        <th class="px-4 py-3">ID</th>
                        <th class="px-4 py-3">Username</th>
                        <th class="px-4 py-3">Nama</th>
                        <th class="px-4 py-3">Email</th>
                        <th class="px-4 py-3">Status</th>
                        <th class="px-4 py-3">Grup</th>
                        <th class="px-4 py-3">Pegawai (API)</th>
                        <th class="px-4 py-3 w-52">Tindakan</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-100">
                    <?php if ($rows === []) : ?>
                        <tr>
                            <td colspan="8" class="px-4 py-10 text-center text-gray-500">Tidak ada pengguna atau tabel belum tersedia.</td>
                        </tr>
                    <?php else : ?>
                        <?php foreach ($rows as $r) : ?>
                            <?php
                            $uid   = (int) ($r['id'] ?? 0);
                            $act   = (int) ($r['active'] ?? 0) === 1;
                            $grups = $r['groups'] ?? [];
                            $glist = is_array($grups) ? $grups : [];
                            ?>
                            <tr class="hover:bg-gray-50/80">
                                <td class="whitespace-nowrap px-4 py-2 font-mono text-xs text-gray-600"><?= $uid ?></td>
                                <td class="whitespace-nowrap px-4 py-2 font-medium text-gray-900"><?= esc((string) ($r['username'] ?? '')) ?></td>
                                <td class="px-4 py-2"><?= esc((string) ($r['nama_lengkap'] ?? '')) ?></td>
                                <td class="px-4 py-2 text-gray-600"><?= esc((string) ($r['email'] ?? '')) ?></td>
                                <td class="px-4 py-2">
                                    <?php if ($act) : ?>
                                        <span class="inline-flex rounded-full bg-emerald-50 px-2 py-0.5 text-xs font-semibold text-emerald-800">Aktif</span>
                                    <?php else : ?>
                                        <span class="inline-flex rounded-full bg-gray-100 px-2 py-0.5 text-xs font-semibold text-gray-600">Nonaktif</span>
                                    <?php endif ?>
                                </td>
                                <td class="px-4 py-2">
                                    <div class="flex flex-wrap gap-1">
                                        <?php foreach ($glist as $gn) : ?>
                                            <span class="inline-flex rounded-md bg-sky-50 px-2 py-0.5 text-xs font-medium text-sky-800"><?= esc((string) $gn) ?></span>
                                        <?php endforeach ?>
                                        <?php if ($glist === []) : ?>
                                            <span class="text-xs text-gray-400">—</span>
                                        <?php endif ?>
                                    </div>
                                </td>
                                <td class="max-w-xs px-4 py-2 text-xs text-gray-700">
                                    <?php
                                    $pp = $r['pegawai_proxy'] ?? null;
                                    ?>
                                    <?php if (is_string($pp) && $pp !== '') : ?>
                                        <?= esc($pp) ?>
                                    <?php else : ?>
                                        <span class="text-gray-400">Default</span>
                                    <?php endif ?>
                                </td>
                                <td class="px-4 py-2">
                                    <div class="flex flex-col gap-1 sm:flex-row sm:flex-wrap">
                                        <a href="<?= site_url('admin/panel/users/edit/' . $uid) ?>" class="inline-flex justify-center rounded-lg border border-gray-200 bg-white px-2 py-1 text-xs font-semibold text-gray-800 shadow-sm hover:bg-gray-50">Ubah</a>
                                        <a href="<?= site_url('admin/panel/users/reset/' . $uid) ?>" class="inline-flex justify-center rounded-lg border border-gray-200 bg-white px-2 py-1 text-xs font-semibold text-gray-800 shadow-sm hover:bg-gray-50">Reset password</a>
                                    </div>
                                </td>
                            </tr>
                        <?php endforeach ?>
                    <?php endif ?>
                </tbody>
            </table>
        </div>
    </section>
</div>
<?= $this->endSection() ?>
