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

<?= $this->section('title') ?>
Edit User
<?= $this->endSection() ?>

<?= $this->section('page_title') ?>
Update Informasi User
<?= $this->endSection() ?>

<?= $this->section('content') ?>
<div class="row">
    <div class="col-lg-6 mx-auto">
        <form action="<?= base_url('admin/user/processEdit/' . $user['id']) ?>" method="POST">

            <div class="glass-card mb-4">
                <h5 class="mb-4 text-primary"><i class="bi bi-pencil-square me-2"></i> Identitas & Akun</h5>

                <?php if (session()->getFlashdata('errors')): ?>
                    <div class="alert alert-danger rounded-pill px-4" role="alert">
                        <ul class="mb-0">
                            <?php foreach (session()->getFlashdata('errors') as $error): ?>
                                <li><?= $error ?></li>
                            <?php endforeach; ?>
                        </ul>
                    </div>
                <?php endif; ?>

                <div class="mb-3">
                    <label class="form-label small fw-bold">NAMA LENGKAP</label>
                    <input type="text" class="form-control rounded-pill px-4" name="name" required
                        value="<?= old('name', $user['name']) ?>" placeholder="Nama lengkap user">
                </div>

                <div class="mb-3">
                    <label class="form-label small fw-bold">NIP (Opsional)</label>
                    <input type="text" class="form-control rounded-pill px-4" name="nip"
                        value="<?= old('nip', $user['nip']) ?>" placeholder="NIP jika ASN">
                    <small class="text-muted">Jika diisi, user akan terknoneksi ke data profil pegawai dengan NIP yang
                        sama.</small>
                </div>

                <div class="mb-3">
                    <label class="form-label small fw-bold">USERNAME</label>
                    <input type="text" class="form-control rounded-pill px-4" name="username" required
                        value="<?= old('username', $user['username']) ?>" placeholder="Username login">
                </div>

                <div class="mb-3">
                    <label class="form-label small fw-bold">EMAIL</label>
                    <input type="email" class="form-control rounded-pill px-4" name="email"
                        value="<?= old('email', $user['email']) ?>" placeholder="user@domain.com">
                </div>

                <div class="mb-3">
                    <label class="form-label small fw-bold">NOMOR WHATSAPP (Notifikasi)</label>
                    <input type="text" class="form-control rounded-pill px-4" name="no_hp"
                        value="<?= old('no_hp', $user['no_hp']) ?>" placeholder="628xxxxxxx">
                </div>

                <div class="mb-3 border-top pt-3 mt-4">
                    <label class="form-label small fw-bold">PASSWORD BARU (Kosongkan jika tak diubah)</label>
                    <input type="password" class="form-control rounded-pill px-4" name="password"
                        placeholder="Min 5 karakter">
                </div>

                <div class="mb-3 mt-4">
                    <label class="form-label small fw-bold">ROLE / PERAN</label>
                    <select class="form-select rounded-pill px-4" name="role_id" required>
                        <option value="">Pilih Role...</option>
                        <?php foreach ($roles as $role): ?>
                            <option value="<?= $role['id'] ?>" <?= old('role_id', $user['role_id']) == $role['id'] ? 'selected' : '' ?>><?= $role['name'] ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>

                <div class="mb-3 mt-4">
                    <div class="form-check form-switch mt-2">
                        <input class="form-check-input" type="checkbox" name="is_active" value="1" id="is_active"
                            <?= old('is_active', $user['is_active']) ? 'checked' : '' ?>>
                        <label class="form-check-label fw-bold small" for="is_active">AKTIF (Bisa Login)</label>
                    </div>
                </div>
            </div>

            <div class="text-end mb-5">
                <a href="<?= base_url('admin/user') ?>" class="btn btn-light rounded-pill px-4 me-2">BATAL</a>
                <button type="submit" class="btn btn-primary rounded-pill px-5 shadow py-2 fw-bold">SIMPAN
                    PERUBAHAN</button>
            </div>
        </form>
    </div>
</div>
<?= $this->endSection() ?>