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

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

<?= $this->section('content') ?>
<div class="space-y-6">
    <div class="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
        <div>
            <h1 class="text-2xl font-semibold text-gray-900">Tambah pengguna admin</h1>
            <p class="mt-1 text-sm text-gray-500">Grup webmaster tidak bisa dipilih di sini. Akun bertipe pegawai menghubungkan login admin ke data pegawai tertentu.</p>
        </div>
        <a href="<?= site_url('admin/panel/users') ?>" class="inline-flex items-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-arrow-left mr-2 text-gray-500"></i> Kembali ke daftar
        </a>
    </div>

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

    <?php
    $groupChoices = 0;
    foreach ($groups as $g) {
        if (strtolower((string) ($g['name'] ?? '')) !== 'webmaster') {
            $groupChoices++;
        }
    }
    ?>
    <?php if ($groupChoices === 0) : ?>
        <div class="rounded-2xl border border-amber-200 bg-amber-50 p-4 text-sm text-amber-900">
            Tidak ada grup yang bisa dipilih (semua grup mungkin webmaster, atau belum ada grup). Tambah grup lewat <a href="<?= site_url('admin/panel/groups/create') ?>" class="font-medium text-amber-950 underline">Tambah grup</a> atau hubungi webmaster.
        </div>
    <?php endif ?>

    <section class="rounded-2xl border border-gray-200 bg-white p-6 shadow-sm">
        <h2 class="text-base font-semibold text-gray-900">Form pengguna</h2>
        <form method="post" action="<?= site_url('admin/panel/users/store') ?>" class="mt-6 grid gap-4 sm:grid-cols-2">
            <?= csrf_field() ?>
            <div>
                <label class="mb-1 block text-xs font-medium text-gray-600">Username *</label>
                <input name="username" value="<?= esc(old('username', '')) ?>" required autocomplete="username" class="w-full rounded-lg border border-gray-300 px-3 py-2 text-sm shadow-sm">
            </div>
            <div>
                <label class="mb-1 block text-xs font-medium text-gray-600">Password *</label>
                <input type="password" name="password" required autocomplete="new-password" minlength="6" class="w-full rounded-lg border border-gray-300 px-3 py-2 text-sm shadow-sm">
            </div>
            <div>
                <label class="mb-1 block text-xs font-medium text-gray-600">Nama lengkap *</label>
                <input name="nama_lengkap" value="<?= esc(old('nama_lengkap', '')) ?>" required class="w-full rounded-lg border border-gray-300 px-3 py-2 text-sm shadow-sm">
            </div>
            <div>
                <label class="mb-1 block text-xs font-medium text-gray-600">Email</label>
                <input type="email" name="email" value="<?= esc(old('email', '')) ?>" class="w-full rounded-lg border border-gray-300 px-3 py-2 text-sm shadow-sm">
            </div>
            <div>
                <label class="mb-1 block text-xs font-medium text-gray-600">No. telepon</label>
                <input name="no_telepon" value="<?= esc(old('no_telepon', '')) ?>" class="w-full rounded-lg border border-gray-300 px-3 py-2 text-sm shadow-sm">
            </div>
            <div class="sm:col-span-2">
                <label class="mb-1 block text-xs font-medium text-gray-600">Pegawai untuk token API (opsional)</label>
                <select name="id_pegawai" class="mt-1 w-full rounded-lg border border-gray-300 px-3 py-2 text-sm shadow-sm">
                    <option value="">— Default (urutan .env / super_akses) —</option>
                    <?php foreach ($pegawai_rows ?? [] as $p) : ?>
                        <?php
                        $pid = (int) ($p['id_pegawai'] ?? 0);
                        if ($pid <= 0) {
                            continue;
                        }
                        $label = trim((string) ($p['nama_lengkap'] ?? '')) . ' — NIP ' . (string) ($p['nip'] ?? '');
                        ?>
                        <option value="<?= $pid ?>" <?= (string) old('id_pegawai') === (string) $pid ? 'selected' : '' ?>><?= esc($label) ?></option>
                    <?php endforeach ?>
                </select>
                <p class="mt-1 text-xs text-gray-500">Kosongkan jika satu akun admin boleh memakai aturan fallback lama. Isi agar login panel memakai pegawai ini sebagai proxy.</p>
            </div>
            <div>
                <label class="mb-1 block text-xs font-medium text-gray-600">Grup *</label>
                <select name="group_id" required class="mt-1 w-full rounded-lg border border-gray-300 px-3 py-2 text-sm shadow-sm">
                    <option value="">— Pilih grup —</option>
                    <?php foreach ($groups as $g) : ?>
                        <?php
                        $gid  = (int) ($g['id'] ?? 0);
                        $gname = strtolower((string) ($g['name'] ?? ''));
                        if ($gname === 'webmaster') {
                            continue;
                        }
                        ?>
                        <option value="<?= $gid ?>" <?= (string) old('group_id') === (string) $gid ? 'selected' : '' ?>>
                            <?= esc((string) ($g['name'] ?? '')) ?><?= isset($g['description']) && (string) $g['description'] !== '' ? ' — ' . esc((string) $g['description']) : '' ?>
                        </option>
                    <?php endforeach ?>
                </select>
            </div>
            <div class="sm:col-span-2 flex flex-wrap gap-3 pt-2">
                <button type="submit" <?= $groupChoices === 0 ? 'disabled' : '' ?> class="rounded-lg bg-blue-600 px-5 py-2.5 text-sm font-semibold text-white shadow-sm hover:bg-blue-700 disabled:cursor-not-allowed disabled:opacity-50">Simpan</button>
                <a href="<?= site_url('admin/panel/users') ?>" class="inline-flex items-center rounded-lg border border-gray-200 px-5 py-2.5 text-sm font-medium text-gray-800 hover:bg-gray-50">Batal</a>
            </div>
        </form>
    </section>
</div>
<?= $this->endSection() ?>
