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

<?= $this->section('title') ?>Reset password 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">Reset password</h1>
            <p class="mt-1 text-sm text-gray-500">Atur ulang kata sandi untuk pengguna ID <span class="font-mono font-semibold text-gray-800"><?= (int) $id ?></span>.</p>
        </div>
        <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.5 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
        </a>
    </div>

    <?php $flashErr = session()->getFlashdata('error'); ?>
    <?php if (is_string($flashErr) && $flashErr !== '') : ?>
        <?= view('layouts/partials/admin_alert', ['errors' => [$flashErr]]) ?>
    <?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">Password baru</h2>
        <form method="post" action="<?= site_url('admin/panel/users/reset_password/' . (int) $id) ?>" class="mt-6 max-w-md space-y-4" id="form-reset-admin">
            <?= csrf_field() ?>
            <div>
                <label class="mb-1 block text-xs font-medium text-gray-600">Password baru *</label>
                <input type="password" id="pw1" name="password" required minlength="6" autocomplete="new-password" 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">Ulangi password *</label>
                <input type="password" id="pw2" required minlength="6" autocomplete="new-password" class="w-full rounded-lg border border-gray-300 px-3 py-2 text-sm shadow-sm">
            </div>
            <div class="flex flex-wrap gap-3 pt-2">
                <button type="submit" class="rounded-lg bg-blue-600 px-5 py-2.5 text-sm font-semibold text-white shadow-sm hover:bg-blue-700">Simpan password</button>
            </div>
        </form>
    </section>
</div>
<script>
(function () {
    var f = document.getElementById('form-reset-admin');
    if (!f) return;
    f.addEventListener('submit', function (e) {
        var a = document.getElementById('pw1');
        var b = document.getElementById('pw2');
        if (!a || !b || a.value !== b.value) {
            e.preventDefault();
            alert('Kedua password harus sama.');
        }
    });
})();
</script>
<?= $this->endSection() ?>
