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

<?= $this->section('title') ?>Grup akses 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">Grup akses</h1>
            <p class="mt-1 text-sm text-gray-500">Grup menentukan izin menu di panel. Contoh: <code class="rounded bg-gray-100 px-1 text-xs">hrd</code>, <code class="rounded bg-gray-100 px-1 text-xs">supervisor</code> (beberapa grup hanya melihat data sesuai kantor pegawai yang sedang login).</p>
        </div>
        <div class="flex flex-wrap gap-2">
            <a href="<?= site_url('admin/panel/users') ?>" 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-users mr-2 text-gray-500"></i> Pengguna
            </a>
            <a href="<?= site_url('admin/panel/groups/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-plus mr-2"></i> Tambah grup
            </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">Daftar grup</h2>
            <p class="text-xs text-gray-500">Grup <strong>webmaster</strong> tidak bisa dihapus. Tombol <strong>Hapus</strong> nonaktif jika grup masih dipakai pengguna — pindahkan pengguna ke grup lain dulu.</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">Nama</th>
                        <th class="px-4 py-3">Deskripsi</th>
                        <th class="px-4 py-3">Pengguna</th>
                        <th class="px-4 py-3 w-40">Tindakan</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-100">
                    <?php if (($rows ?? []) === []) : ?>
                        <tr>
                            <td colspan="5" class="px-4 py-10 text-center text-gray-500">Tidak ada grup atau gagal memuat.</td>
                        </tr>
                    <?php else : ?>
                        <?php foreach ($rows as $r) : ?>
                            <?php
                            $gid   = (int) ($r['id'] ?? 0);
                            $gname = strtolower((string) ($r['name'] ?? ''));
                            $isWm  = $gname === 'webmaster';
                            $uc    = (int) ($r['user_count'] ?? 0);
                            ?>
                            <tr class="hover:bg-gray-50/80">
                                <td class="whitespace-nowrap px-4 py-2 font-mono text-xs text-gray-600"><?= $gid ?></td>
                                <td class="whitespace-nowrap px-4 py-2 font-medium text-gray-900"><?= esc((string) ($r['name'] ?? '')) ?></td>
                                <td class="px-4 py-2 text-gray-600"><?= esc((string) ($r['description'] ?? '')) ?></td>
                                <td class="px-4 py-2 text-gray-600"><?= $uc ?></td>
                                <td class="px-4 py-2">
                                    <div class="flex flex-wrap gap-1">
                                        <a href="<?= site_url('admin/panel/groups/edit/' . $gid) ?>" class="inline-flex 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>
                                        <?php if (! $isWm) : ?>
                                            <form method="post" action="<?= site_url('admin/panel/groups/delete/' . $gid) ?>" class="inline" onsubmit="<?= $uc > 0 ? 'return false;' : "return confirm('Hapus grup ini?');" ?>">
                                                <?= csrf_field() ?>
                                                <button
                                                    type="submit"
                                                    class="inline-flex rounded-lg border px-2 py-1 text-xs font-semibold shadow-sm <?= $uc > 0 ? 'cursor-not-allowed border-gray-200 bg-gray-100 text-gray-400' : 'border-red-200 bg-red-50 text-red-800 hover:bg-red-100' ?>"
                                                    <?= $uc > 0 ? 'disabled title="Grup masih dipakai ' . $uc . ' pengguna. Ubah pengguna lewat menu Pengguna → hapus dari grup ini dulu."' : 'title="Hapus grup ini"' ?>
                                                >Hapus</button>
                                            </form>
                                        <?php else : ?>
                                            <span class="text-xs text-gray-400" title="Grup sistem">—</span>
                                        <?php endif ?>
                                    </div>
                                </td>
                            </tr>
                        <?php endforeach ?>
                    <?php endif ?>
                </tbody>
            </table>
        </div>
    </section>
</div>
<?= $this->endSection() ?>
