<?= $this->extend('layout/default') ?>
<?= $this->section('content') ?>
<div class="container-fluid px-4">
    <h1 class="mt-4">Edit Personil</h1>
    <div class="card mb-4 col-lg-8">
        <div class="card-body shadow">
            <form action="<?= base_url('admin/struktur/update') ?>" method="post" enctype="multipart/form-data">
                <?= csrf_field() ?>
                <input type="hidden" name="id_struktur" value="<?= $personil['id_struktur'] ?>">
                <input type="hidden" name="fotoLama" value="<?= $personil['foto'] ?>">
                
                <div class="mb-3">
                    <label class="form-label">Nama Lengkap</label>
                    <input type="text" name="nama" class="form-control" value="<?= $personil['nama'] ?>" required>
                </div>
                <div class="mb-3">
                    <label class="form-label">Jabatan</label>
                    <input type="text" name="jabatan" class="form-control" value="<?= $personil['jabatan'] ?>" required>
                </div>
                <div class="row">
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Level</label>
                        <select name="level" class="form-select">
                            <option value="1" <?= ($personil['level'] == '1') ? 'selected' : '' ?>>Level 1</option>
                            <option value="2" <?= ($personil['level'] == '2') ? 'selected' : '' ?>>Level 2</option>
                            <option value="3" <?= ($personil['level'] == '3') ? 'selected' : '' ?>>Level 3</option>
                            <option value="4" <?= ($personil['level'] == '4') ? 'selected' : '' ?>>Level 4 (Staf)</option>
                        </select>
                    </div>
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Pilih Atasan (Hanya untuk Staf)</label>
                        <select name="parent_id" class="form-select">
                            <option value="0">-- Tanpa Atasan (Pimpinan) --</option>
                            <?php 
                            $db = \Config\Database::connect();
                            $wakaseks = $db->table('struktur')->where('level', '3')->get()->getResultArray();
                            foreach ($wakaseks as $w) : ?>
                                <option value="<?= $w['id_struktur'] ?>" <?= ($personil['parent_id'] == $w['id_struktur']) ? 'selected' : '' ?>>
                                    <?= $w['jabatan'] ?> (<?= $w['nama'] ?>)
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Urutan</label>
                        <input type="number" name="urutan" class="form-control" value="<?= $personil['urutan'] ?>">
                    </div>
                </div>
                <div class="mb-3 text-center bg-light p-3 rounded">
                    <label class="form-label d-block">Foto Saat Ini</label>
                    <img src="<?= base_url('assets/img/team/' . $personil['foto']) ?>" width="120" class="img-thumbnail rounded-circle mb-2 shadow-sm">
                    <input type="file" name="foto" class="form-control mt-2">
                </div>
                <button type="submit" class="btn btn-warning">Update</button>
                <a href="<?= base_url('admin/struktur') ?>" class="btn btn-secondary">Batal</a>
            </form>
        </div>
    </div>
</div>
<?= $this->endSection() ?>