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

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

<?= $this->section('content') ?>
<?php
$user = $user ?? null;
$isWm = false;
if (is_array($user)) {
    $gnames = $user['groups'] ?? [];
    $gnames = is_array($gnames) ? $gnames : [];
    foreach ($gnames as $gn) {
        if (strtolower((string) $gn) === 'webmaster') {
            $isWm = true;
            break;
        }
    }
}
?>
<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">Ubah pengguna admin</h1>
            <p class="mt-1 text-sm text-gray-500">ID <?= (int) ($id ?? 0) ?>. Password diubah lewat <strong>Reset password</strong>. Akun webmaster: grup tetap webmaster.</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 if (is_array($user)) : ?>
        <?php
        $groupChoices = 0;
        foreach ($groups as $g) {
            $gname = strtolower((string) ($g['name'] ?? ''));
            if ($isWm) {
                if ($gname === 'webmaster') {
                    $groupChoices++;
                }
            } elseif ($gname !== 'webmaster') {
                $groupChoices++;
            }
        }
        $gidCurrent = (int) ($user['group_id'] ?? 0);
        if ($isWm && $gidCurrent <= 0) {
            foreach ($groups as $gw) {
                if (strtolower((string) ($gw['name'] ?? '')) === 'webmaster') {
                    $gidCurrent = (int) ($gw['id'] ?? 0);
                    break;
                }
            }
        }
        $pegawaiKey = array_key_exists('id_pegawai', $user);
        $pidCurrent = $pegawaiKey ? (int) ($user['id_pegawai'] ?? 0) : 0;
        $actCurrent = (int) ($user['active'] ?? 1) === 1 ? '1' : '0';
        ?>
        <section class="rounded-2xl border border-gray-200 bg-white p-6 shadow-sm">
            <form method="post" action="<?= site_url('admin/panel/users/update/' . (int) ($user['id'] ?? 0)) ?>" class="mt-0 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', (string) ($user['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">Status *</label>
                    <select name="active" required class="mt-1 w-full rounded-lg border border-gray-300 px-3 py-2 text-sm shadow-sm">
                        <option value="1" <?= (string) old('active', $actCurrent) === '1' ? 'selected' : '' ?>>Aktif</option>
                        <option value="0" <?= (string) old('active', $actCurrent) === '0' ? 'selected' : '' ?>>Nonaktif</option>
                    </select>
                </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', (string) ($user['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', (string) ($user['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', (string) ($user['no_telepon'] ?? ''))) ?>" class="w-full rounded-lg border border-gray-300 px-3 py-2 text-sm shadow-sm">
                </div>
                <?php if ($pegawaiKey) : ?>
                    <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'] ?? '');
                                $sel = (string) old('id_pegawai', (string) ($pidCurrent > 0 ? $pidCurrent : '')) === (string) $pid;
                                ?>
                                <option value="<?= $pid ?>" <?= $sel ? 'selected' : '' ?>><?= esc($label) ?></option>
                            <?php endforeach ?>
                        </select>
                    </div>
                <?php endif ?>
                <div class="<?= $pegawaiKey ? '' : 'sm:col-span-2' ?>">
                    <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" <?= $isWm ? 'disabled' : '' ?>>
                        <?php foreach ($groups as $g) : ?>
                            <?php
                            $gid  = (int) ($g['id'] ?? 0);
                            $gname = strtolower((string) ($g['name'] ?? ''));
                            if ($isWm) {
                                if ($gname !== 'webmaster') {
                                    continue;
                                }
                            } elseif ($gname === 'webmaster') {
                                continue;
                            }
                            $sel = (string) old('group_id', (string) $gidCurrent) === (string) $gid;
                            ?>
                            <option value="<?= $gid ?>" <?= $sel ? 'selected' : '' ?>>
                                <?= esc((string) ($g['name'] ?? '')) ?><?= isset($g['description']) && (string) $g['description'] !== '' ? ' — ' . esc((string) $g['description']) : '' ?>
                            </option>
                        <?php endforeach ?>
                    </select>
                    <?php if ($isWm) : ?>
                        <input type="hidden" name="group_id" value="<?= $gidCurrent > 0 ? $gidCurrent : '' ?>">
                        <p class="mt-1 text-xs text-amber-800">Grup webmaster tidak bisa diganti dari sini.</p>
                    <?php endif ?>
                </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/reset/' . (int) ($user['id'] ?? 0)) ?>" class="inline-flex items-center rounded-lg border border-gray-200 bg-white px-5 py-2.5 text-sm font-medium text-gray-800 shadow-sm hover:bg-gray-50">Reset password</a>
                    <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>
    <?php endif ?>
</div>
<?= $this->endSection() ?>
