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

<div class="row justify-content-center">
<div class="col-lg-8">
<div class="card">
    <div class="card-header d-flex align-items-center justify-content-between">
        <div class="d-flex align-items-center gap-2">
            <i class="bi bi-pencil-square text-primary"></i>
            <span class="fw-semibold">Edit Data Guru</span>
        </div>
        <a href="/teacher" class="btn btn-sm btn-outline-secondary">
            <i class="bi bi-arrow-left me-1"></i>Kembali
        </a>
    </div>
    <div class="card-body">

        <?php if (session()->getFlashdata('errors')): ?>
        <div class="alert alert-danger py-2">
            <ul class="mb-0 ps-3 small">
                <?php foreach (session()->getFlashdata('errors') as $err): ?>
                <li><?= esc($err) ?></li>
                <?php endforeach; ?>
            </ul>
        </div>
        <?php endif; ?>

        <!-- Info akun login -->
        <?php if ($teacher['user_id']): ?>
        <div class="alert alert-success py-2 small mb-3 d-flex align-items-center gap-2">
            <i class="bi bi-check-circle-fill text-success"></i>
            <span>Guru ini sudah punya akun login dengan email <strong><?= esc($teacher['login_email'] ?? '—') ?></strong></span>
        </div>
        <?php else: ?>
        <div class="alert alert-warning py-2 small mb-3 d-flex align-items-center gap-2">
            <i class="bi bi-exclamation-triangle-fill text-warning"></i>
            <span>Guru ini <strong>belum punya akun login</strong>. Centang di bawah untuk membuatkan.</span>
        </div>
        <?php endif; ?>

        <form action="/teacher/update/<?= $teacher['id'] ?>" method="post">
            <?= csrf_field() ?>

            <div class="row g-3">

                <div class="col-md-8">
                    <label class="form-label">Nama Lengkap <span class="text-danger">*</span></label>
                    <input type="text" name="name" class="form-control"
                        value="<?= old('name', $teacher['name']) ?>" required>
                </div>
                <div class="col-md-4">
                    <label class="form-label">Jenis Kelamin <span class="text-danger">*</span></label>
                    <select name="gender" class="form-select" required>
                        <option value="L" <?= old('gender', $teacher['gender']) == 'L' ? 'selected' : '' ?>>Laki-laki</option>
                        <option value="P" <?= old('gender', $teacher['gender']) == 'P' ? 'selected' : '' ?>>Perempuan</option>
                    </select>
                </div>

                <div class="col-md-4">
                    <label class="form-label">NIP</label>
                    <input type="text" name="nip" class="form-control"
                        value="<?= old('nip', $teacher['nip']) ?>" id="nipInput">
                    <div class="form-text small text-muted">Wajib diisi jika status PNS / PPPK</div>
                </div>
                <div class="col-md-4">
                    <label class="form-label">PEG ID / NPK / NUPTK</label>
                    <input type="text" name="peg_id" class="form-control"
                        value="<?= old('peg_id', $teacher['peg_id'] ?? '') ?>"
                        id="pegIdInput" inputmode="numeric">
                    <div class="form-text small text-muted">Wajib diisi jika non-PNS/PPPK (hanya angka)</div>
                </div>
                <div class="col-md-2">
                    <label class="form-label">Kode aSc</label>
                    <input type="text" name="short_code" class="form-control"
                        value="<?= old('short_code', $teacher['short_code']) ?>"
                        placeholder="01, 02..."
                        style="text-transform:uppercase"
                        maxlength="10">
                    <div class="form-text small text-muted">Opsional</div>
                </div>
                <div class="col-md-2">
                    <label class="form-label">No. HP</label>
                    <input type="text" name="phone" class="form-control"
                        value="<?= old('phone', $teacher['phone']) ?>">
                </div>

                <div class="col-md-10">
                    <label class="form-label">Email</label>
                    <input type="email" name="email" class="form-control"
                        value="<?= old('email', $teacher['email']) ?>">
                </div>

                <!-- Field Tambahan Kepegawaian -->
                <div class="col-12">
                    <hr class="my-1">
                    <h6 class="fw-semibold mb-2"><i class="bi bi-briefcase me-1 text-primary"></i>Detail Kepegawaian (Kebutuhan Surat/SPPD)</h6>
                </div>

                <div class="col-md-4">
                    <label class="form-label">Pangkat / Golongan</label>
                    <input type="text" name="golongan" list="golonganList" class="form-control"
                        value="<?= old('golongan', $teacher['golongan'] ?? '') ?>" placeholder="Contoh: Penata Muda, III/a">
                    <datalist id="golonganList">
                        <option value="Penata Muda, III/a">
                        <option value="Penata Muda Tk. I, III/b">
                        <option value="Penata, III/c">
                        <option value="Penata Tk. I, III/d">
                        <option value="Pembina, IV/a">
                        <option value="Pembina Tk. I, IV/b">
                        <option value="Pembina Utama Muda, IV/c">
                        <option value="Pembina Utama Madya, IV/d">
                        <option value="Pembina Utama, IV/e">
                    </datalist>
                </div>

                <div class="col-md-4">
                    <label class="form-label">Jabatan</label>
                    <input type="text" name="jabatan" list="jabatanList" class="form-control"
                        value="<?= old('jabatan', $teacher['jabatan'] ?? '') ?>" placeholder="Contoh: Pendidik / Guru">
                    <datalist id="jabatanList">
                        <option value="Pendidik / Guru">
                        <option value="Kepala Urusan TU">
                        <option value="Staf TU / Tenaga Kependidikan">
                        <option value="Kepala Madrasah">
                        <option value="Bendahara Madrasah">
                        <option value="Guru BK">
                        <option value="Wali Kelas">
                    </datalist>
                </div>

                <div class="col-md-4">
                    <label class="form-label">Status Kepegawaian</label>
                    <select name="status_kepegawaian" id="statusKepegawaian" class="form-select">
                        <option value="">-- Pilih Status --</option>
                        <option value="PNS" <?= old('status_kepegawaian', $teacher['status_kepegawaian'] ?? '') == 'PNS' ? 'selected' : '' ?>>PNS</option>
                        <option value="PPPK" <?= old('status_kepegawaian', $teacher['status_kepegawaian'] ?? '') == 'PPPK' ? 'selected' : '' ?>>PPPK</option>
                        <option value="Honorer" <?= old('status_kepegawaian', $teacher['status_kepegawaian'] ?? '') == 'Honorer' ? 'selected' : '' ?>>Honorer (GTT/PTT)</option>
                        <option value="GTY" <?= old('status_kepegawaian', $teacher['status_kepegawaian'] ?? '') == 'GTY' ? 'selected' : '' ?>>GTY (Guru Tetap Yayasan)</option>
                        <option value="Lainnya" <?= old('status_kepegawaian', $teacher['status_kepegawaian'] ?? '') == 'Lainnya' ? 'selected' : '' ?>>Lainnya</option>
                    </select>
                </div>

                <div class="col-md-4">
                    <label class="form-label">Status Sertifikasi</label>
                    <select name="is_sertifikasi" class="form-select">
                        <option value="0" <?= old('is_sertifikasi', $teacher['is_sertifikasi'] ?? 0) == 0 ? 'selected' : '' ?>>Belum Sertifikasi</option>
                        <option value="1" <?= old('is_sertifikasi', $teacher['is_sertifikasi'] ?? 0) == 1 ? 'selected' : '' ?>>Sudah Sertifikasi</option>
                    </select>
                </div>

                <div class="col-md-8">
                    <div class="form-check mt-4">
                        <input class="form-check-input" type="checkbox" name="is_active" value="1" id="isActive"
                            <?= $teacher['is_active'] ? 'checked' : '' ?>>
                        <label class="form-check-label" for="isActive">Guru/Pegawai Aktif</label>
                    </div>
                </div>

                <!-- Buat Akun (hanya muncul jika belum punya) -->
                <?php if (!$teacher['user_id']): ?>
                <?php $identifier = teacher_login_identifier($teacher); ?>
                <div class="col-12">
                    <hr class="my-1">
                    <div class="form-check mb-3">
                        <input class="form-check-input" type="checkbox" id="createAccount" name="create_account" value="1"
                            onchange="toggleAccountFields(this.checked)">
                        <label class="form-check-label fw-semibold" for="createAccount">
                            <i class="bi bi-key me-1 text-warning"></i>Buatkan akun login sekarang
                        </label>
                        <?php if (empty($identifier)): ?>
                        <div class="text-danger small mt-1">
                            <i class="bi bi-exclamation-triangle"></i> NIP/PEG ID kosong — isi dulu agar bisa buat akun!
                        </div>
                        <?php endif; ?>
                    </div>
                    <div id="accountFields" style="display:none">
                        <div class="p-3 rounded border border-warning bg-warning bg-opacity-10">
                            <div class="row g-3">
                                <div class="col-md-6">
                                    <label class="form-label small">Email (Opsional)</label>
                                    <input type="email" name="login_email" class="form-control form-control-sm"
                                        value="<?= esc($teacher['email'] ?? '') ?>">
                                </div>
                                <div class="col-md-6">
                                    <label class="form-label small">Password</label>
                                    <input type="text" name="login_password" class="form-control form-control-sm"
                                        placeholder="Kosongkan = NIP/PEG ID">
                                    <div class="form-text" id="passwordHint">
                                        Default: <?= !empty($identifier) ? esc($identifier) : 'guru123' ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <?php endif; ?>

            </div>

            <div class="d-flex justify-content-end mt-4 pt-3 border-top gap-2">
                <a href="/teacher" class="btn btn-secondary">
                    <i class="bi bi-x-circle me-1"></i>Batal
                </a>
                <button type="submit" class="btn btn-primary px-4">
                    <i class="bi bi-save me-2"></i>Update
                </button>
            </div>

        </form>
    </div>
</div>
</div>
</div>

<?= $this->endSection() ?>
<?= $this->section('scripts') ?>
<script>
function toggleAccountFields(show) {
    document.getElementById('accountFields').style.display = show ? '' : 'none';
}

document.getElementById('pegIdInput').addEventListener('input', function() {
    this.value = this.value.replace(/[^0-9]/g, '');
});

document.querySelector('form').addEventListener('submit', function(e) {
    const status = document.getElementById('statusKepegawaian').value;
    const nip = document.getElementById('nipInput').value;
    const pegId = document.getElementById('pegIdInput').value;
    const createAccount = document.getElementById('createAccount');

    if ((status === 'PNS' || status === 'PPPK') && !nip) {
        e.preventDefault();
        alert('NIP wajib diisi untuk status PNS / PPPK!');
        document.getElementById('nipInput').focus();
        return false;
    }

    if (status && status !== 'PNS' && status !== 'PPPK' && !pegId) {
        e.preventDefault();
        alert('PEG ID / NPK / NUPTK wajib diisi untuk status selain PNS / PPPK!');
        document.getElementById('pegIdInput').focus();
        return false;
    }

    if (createAccount && createAccount.checked && !nip && !pegId) {
        e.preventDefault();
        alert('NIP atau PEG ID/NPK/NUPTK wajib diisi jika ingin membuat akun login!');
        return false;
    }
});
</script>
<?= $this->endSection() ?>
