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

<?= $this->section('title') ?><?= $mode === 'create' ? 'Tambah' : 'Edit' ?> Pegawai<?= $this->endSection() ?>

<?php
/** @var string $mode */
/** @var array<string,mixed>|null $row */
/** @var array<string,mixed>|null $refs */
$r = $row ?? [];
$jab = $refs['jabatan'] ?? [];
$uk  = $refs['unit_kerja'] ?? [];
$gol = $refs['golongan'] ?? [];
$kan = $refs['kantor'] ?? [];
$jad = $refs['jadwal'] ?? [];
$ph  = (string) ($r['photo'] ?? '');
$phDisp = $ph !== '' && $ph !== '-';
?>

<?= $this->section('content') ?>
<div class="mx-auto max-w-3xl 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 tracking-tight text-gray-900"><?= $mode === 'create' ? 'Tambah pegawai' : 'Edit pegawai' ?></h1>
            <p class="mt-1 text-sm text-gray-500"><?= $mode === 'create' ? 'Isi data pegawai baru lalu simpan.' : 'Perbarui data pegawai lalu simpan.' ?></p>
        </div>
        <a href="<?= site_url('admin/pegawai') ?>" class="inline-flex items-center justify-center gap-2 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 text-xs text-gray-500"></i> Kembali
        </a>
    </div>

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

    <?php if ($refs === null) : ?>
        <div class="rounded-2xl border border-gray-200 bg-white p-6 shadow-sm">
            <?= view('layouts/partials/empty_state', [
                'icon'  => 'fa-triangle-exclamation',
                'title' => 'Referensi form tidak dimuat',
                'hint'  => 'Periksa koneksi API atau login ulang. Tanpa jabatan/unit/kantor, form tidak dapat digunakan.',
            ]) ?>
        </div>
    <?php else : ?>
        <?php
        $action = $mode === 'create' ? site_url('admin/pegawai/store') : site_url('admin/pegawai/update/' . (int) ($r['id_pegawai'] ?? 0));
        ?>
        <form action="<?= $action ?>" method="post" enctype="multipart/form-data" class="space-y-4 rounded-2xl border border-gray-200 bg-white p-6 shadow-sm">
            <?= csrf_field() ?>
            <?php if ($mode === 'edit') : ?>
                <input type="hidden" name="photo_existing" value="<?= esc(old('photo_existing', $ph), 'attr') ?>">
            <?php endif ?>
            <div class="grid gap-4 sm:grid-cols-2">
                <div class="sm:col-span-2">
                    <label class="block text-xs font-medium text-gray-600">NIP / NIK *</label>
                    <input name="nip" required value="<?= esc(old('nip', (string) ($r['nip'] ?? ''))) ?>" class="mt-1 w-full rounded-lg border border-gray-300 px-3 py-2 text-sm">
                </div>
                <div class="sm:col-span-2">
                    <label class="block text-xs font-medium text-gray-600">Nama lengkap *</label>
                    <input name="nama_lengkap" required value="<?= esc(old('nama_lengkap', (string) ($r['nama_lengkap'] ?? ''))) ?>" class="mt-1 w-full rounded-lg border border-gray-300 px-3 py-2 text-sm">
                </div>
                <div>
                    <label class="block text-xs font-medium text-gray-600">Jenis kelamin *</label>
                    <select name="jenis_kelamin" class="mt-1 w-full rounded-lg border border-gray-300 px-3 py-2 text-sm">
                        <?php foreach (['Pria', 'Wanita'] as $jk) : ?>
                            <option value="<?= esc($jk) ?>" <?= old('jenis_kelamin', (string) ($r['jenis_kelamin'] ?? 'Pria')) === $jk ? 'selected' : '' ?>><?= esc($jk) ?></option>
                        <?php endforeach ?>
                    </select>
                </div>
                <div>
                    <label class="block text-xs font-medium text-gray-600">Status kepegawaian *</label>
                    <select name="status_kepegawaian" class="mt-1 w-full rounded-lg border border-gray-300 px-3 py-2 text-sm">
                        <?php foreach (['Pegawai Tetap', 'Kontrak'] as $st) : ?>
                            <option value="<?= esc($st) ?>" <?= old('status_kepegawaian', (string) ($r['status_kepegawaian'] ?? 'Pegawai Tetap')) === $st ? 'selected' : '' ?>><?= esc($st) ?></option>
                        <?php endforeach ?>
                    </select>
                </div>
                <div>
                    <label class="block text-xs font-medium text-gray-600">Tempat lahir</label>
                    <input name="tempat_lahir" value="<?= esc(old('tempat_lahir', (string) ($r['tempat_lahir'] ?? ''))) ?>" class="mt-1 w-full rounded-lg border border-gray-300 px-3 py-2 text-sm">
                </div>
                <div>
                    <label class="block text-xs font-medium text-gray-600">Tanggal lahir</label>
                    <input type="date" name="tanggal_lahir" value="<?= esc(old('tanggal_lahir', ($r['tanggal_lahir'] ?? '') && ($r['tanggal_lahir'] ?? '') !== '0000-00-00' ? (string) $r['tanggal_lahir'] : '')) ?>" class="mt-1 w-full rounded-lg border border-gray-300 px-3 py-2 text-sm">
                </div>
                <div class="sm:col-span-2">
                    <label class="block text-xs font-medium text-gray-600">Email</label>
                    <input name="email" type="email" value="<?= esc(old('email', (string) ($r['email'] ?? ''))) ?>" class="mt-1 w-full rounded-lg border border-gray-300 px-3 py-2 text-sm">
                </div>
                <div>
                    <label class="block text-xs font-medium text-gray-600">Jabatan *</label>
                    <select name="jabatan" required class="mt-1 w-full rounded-lg border border-gray-300 px-3 py-2 text-sm">
                        <?php foreach ($jab as $j) : ?>
                            <option value="<?= (int) ($j['id_jabatan'] ?? 0) ?>" <?= (int) old('jabatan', (string) ($r['jabatan'] ?? '0')) === (int) ($j['id_jabatan'] ?? 0) ? 'selected' : '' ?>><?= esc((string) ($j['nama_jabatan'] ?? '')) ?></option>
                        <?php endforeach ?>
                    </select>
                </div>
                <div>
                    <label class="block text-xs font-medium text-gray-600">Unit kerja *</label>
                    <select name="unit_kerja" required class="mt-1 w-full rounded-lg border border-gray-300 px-3 py-2 text-sm">
                        <?php foreach ($uk as $u) : ?>
                            <option value="<?= (int) ($u['id_unit_kerja'] ?? 0) ?>" <?= (int) old('unit_kerja', (string) ($r['unit_kerja'] ?? '0')) === (int) ($u['id_unit_kerja'] ?? 0) ? 'selected' : '' ?>><?= esc((string) ($u['nama_unit_kerja'] ?? '')) ?></option>
                        <?php endforeach ?>
                    </select>
                </div>
                <div>
                    <label class="block text-xs font-medium text-gray-600">Golongan *</label>
                    <select name="golongan_pekerjaan" required class="mt-1 w-full rounded-lg border border-gray-300 px-3 py-2 text-sm">
                        <?php foreach ($gol as $g) : ?>
                            <option value="<?= (int) ($g['id_golongan'] ?? 0) ?>" <?= (int) old('golongan_pekerjaan', (string) ($r['golongan_pekerjaan'] ?? '0')) === (int) ($g['id_golongan'] ?? 0) ? 'selected' : '' ?>><?= esc((string) ($g['nama_golongan'] ?? '')) ?></option>
                        <?php endforeach ?>
                    </select>
                </div>
                <div>
                    <label class="block text-xs font-medium text-gray-600">Kantor *</label>
                    <select name="kantor" required class="mt-1 w-full rounded-lg border border-gray-300 px-3 py-2 text-sm">
                        <?php foreach ($kan as $k) : ?>
                            <option value="<?= (int) ($k['id_kantor'] ?? 0) ?>" <?= (int) old('kantor', (string) ($r['kantor'] ?? '0')) === (int) ($k['id_kantor'] ?? 0) ? 'selected' : '' ?>><?= esc((string) ($k['nama_kantor'] ?? '')) ?></option>
                        <?php endforeach ?>
                    </select>
                </div>
                <div>
                    <label class="block text-xs font-medium text-gray-600">Jadwal *</label>
                    <select name="jadwal" required class="mt-1 w-full rounded-lg border border-gray-300 px-3 py-2 text-sm">
                        <?php foreach ($jad as $jd) : ?>
                            <option value="<?= (int) ($jd['id_jadwal'] ?? 0) ?>" <?= (int) old('jadwal', (string) ($r['jadwal'] ?? '1')) === (int) ($jd['id_jadwal'] ?? 0) ? 'selected' : '' ?>><?= esc((string) ($jd['nama_jadwal'] ?? '')) ?></option>
                        <?php endforeach ?>
                    </select>
                </div>
                <div>
                    <label class="block text-xs font-medium text-gray-600">Tanggal bergabung *</label>
                    <input type="date" name="tanggal_bergabung" required value="<?= esc(old('tanggal_bergabung', ($r['tanggal_bergabung'] ?? '') && ($r['tanggal_bergabung'] ?? '') !== '0000-00-00' ? (string) $r['tanggal_bergabung'] : date('Y-m-d'))) ?>" class="mt-1 w-full rounded-lg border border-gray-300 px-3 py-2 text-sm">
                </div>
                <div>
                    <label class="block text-xs font-medium text-gray-600">Super akses</label>
                    <select name="super_akses" class="mt-1 w-full rounded-lg border border-gray-300 px-3 py-2 text-sm">
                        <?php foreach (['false', 'true'] as $sa) : ?>
                            <option value="<?= esc($sa) ?>" <?= old('super_akses', (string) ($r['super_akses'] ?? 'false')) === $sa ? 'selected' : '' ?>><?= esc($sa) ?></option>
                        <?php endforeach ?>
                    </select>
                </div>
                <div>
                    <label class="block text-xs font-medium text-gray-600">Username *</label>
                    <input name="username" required value="<?= esc(old('username', (string) ($r['username'] ?? ''))) ?>" class="mt-1 w-full rounded-lg border border-gray-300 px-3 py-2 text-sm" autocomplete="username">
                </div>
                <div class="sm:col-span-2">
                    <label class="block text-xs font-medium text-gray-600">Password <?= $mode === 'edit' ? '(kosongkan jika tidak diubah)' : '(opsional, default = MD5(NIP))' ?></label>
                    <input type="password" name="password" value="<?= esc(old('password', '')) ?>" class="mt-1 w-full rounded-lg border border-gray-300 px-3 py-2 text-sm" autocomplete="new-password">
                </div>
                <div class="sm:col-span-2 rounded-xl border border-gray-100 bg-gray-50/80 p-4">
                    <p class="text-xs font-semibold uppercase tracking-wide text-gray-500">Foto profil</p>
                    <p class="mt-1 text-xs text-gray-500">Unggah gambar (maks. 2 MB) atau isi nama file yang sudah ada di folder <code class="rounded bg-white px-1">assets/uploads/pengguna/</code>. Unggah file menggantikan isian nama file.</p>
                    <?php if ($phDisp) : ?>
                        <div class="mt-3 flex items-center gap-4">
                            <img src="<?= esc(base_url('assets/uploads/pengguna/' . rawurlencode(basename(str_replace('\\', '/', $ph))))) ?>" alt="Foto pegawai" class="h-20 w-20 shrink-0 rounded-lg border border-gray-200 object-cover shadow-sm">
                            <span class="text-xs text-gray-600">File saat ini: <strong class="font-mono text-gray-800"><?= esc(basename(str_replace('\\', '/', $ph))) ?></strong></span>
                        </div>
                    <?php endif ?>
                    <div class="mt-3 grid gap-3 sm:grid-cols-2">
                        <div class="sm:col-span-2">
                            <label class="block text-xs font-medium text-gray-600">Unggah foto</label>
                            <input type="file" name="photo_file" accept="image/jpeg,image/png,image/gif,image/webp" class="mt-1 block w-full text-sm text-gray-600 file:mr-3 file:rounded-lg file:border-0 file:bg-blue-50 file:px-3 file:py-2 file:text-sm file:font-semibold file:text-blue-700 hover:file:bg-blue-100">
                        </div>
                        <div class="sm:col-span-2">
                            <label class="block text-xs font-medium text-gray-600">Nama file di server (opsional)</label>
                            <input type="text" name="photo" value="<?= esc(old('photo', $ph)) ?>" class="mt-1 w-full rounded-lg border border-gray-300 px-3 py-2 text-sm font-mono" placeholder="contoh: 67a1b2c3d4e5f-photo.jpg" autocomplete="off">
                        </div>
                    </div>
                </div>
            </div>
            <div class="flex flex-col-reverse gap-2 border-t border-gray-100 pt-4 sm:flex-row sm:justify-end">
                <a href="<?= site_url('admin/pegawai') ?>" 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">Batal</a>
                <button type="submit" class="inline-flex items-center justify-center rounded-lg bg-blue-600 px-4 py-2.5 text-sm font-semibold text-white shadow-sm hover:bg-blue-700">Simpan</button>
            </div>
        </form>
    <?php endif ?>
</div>
<?= $this->endSection() ?>
