<?php
/**
 * @var array $prodi
 * @var array $fakultas
 * @var \CodeIgniter\Validation\ValidationInterface $validation
 */
?>
<?= $this->extend('layout/default') ?>

<?= $this->section('content') ?>

<div style="display: flex; align-items: center; margin-bottom: 2rem; gap: 1rem;">
    <a href="<?= base_url('prodi') ?>" class="btn btn-secondary" style="padding: 0.5rem 1rem;">
        <i class='bx bx-arrow-back'></i> Kembali
    </a>
    <h1 class="page-title" style="margin-bottom: 0;">
        <?= isset($prodi) ? 'Edit Program Studi' : 'Tambah Program Studi Baru' ?>
    </h1>
</div>

<div class="form-container">
    <form action="<?= isset($prodi) ? base_url('prodi/update/'.$prodi['id']) : base_url('prodi/store') ?>" method="post">
        <?= csrf_field() ?>
        
        <div class="form-group">
            <label class="form-label" for="fakultas_id">Fakultas</label>
            <select class="form-control" id="fakultas_id" name="fakultas_id" autofocus>
                <option value="">-- Pilih Fakultas --</option>
                <?php foreach($fakultas as $f): ?>
                    <option value="<?= $f['id'] ?>" <?= old('fakultas_id', $prodi['fakultas_id'] ?? '') == $f['id'] ? 'selected' : '' ?>>
                        <?= esc($f['nama_fakultas']) ?>
                    </option>
                <?php endforeach; ?>
            </select>
            <?php if(isset($validation) && $validation->hasError('fakultas_id')) : ?>
                <span class="invalid-feedback"><?= $validation->getError('fakultas_id') ?></span>
            <?php endif; ?>
        </div>

        <div class="form-group">
            <label class="form-label" for="kode_prodi">Kode Program Studi</label>
            <input type="text" class="form-control" id="kode_prodi" name="kode_prodi" 
                   value="<?= old('kode_prodi', $prodi['kode_prodi'] ?? '') ?>" 
                   placeholder="Contoh: TI, SI, AK">
            <?php if(isset($validation) && $validation->hasError('kode_prodi')) : ?>
                <span class="invalid-feedback"><?= $validation->getError('kode_prodi') ?></span>
            <?php endif; ?>
        </div>

        <div class="form-group">
            <label class="form-label" for="nama_prodi">Nama Program Studi</label>
            <input type="text" class="form-control" id="nama_prodi" name="nama_prodi" 
                   value="<?= old('nama_prodi', $prodi['nama_prodi'] ?? '') ?>" 
                   placeholder="Contoh: Teknik Informatika">
            <?php if(isset($validation) && $validation->hasError('nama_prodi')) : ?>
                <span class="invalid-feedback"><?= $validation->getError('nama_prodi') ?></span>
            <?php endif; ?>
        </div>

        <div class="form-group">
            <label class="form-label" for="jenjang">Jenjang Pendidikan</label>
            <input type="text" class="form-control" id="jenjang" name="jenjang" 
                   value="<?= old('jenjang', $prodi['jenjang'] ?? '') ?>" 
                   placeholder="Contoh: S1, D3, S2">
            <?php if(isset($validation) && $validation->hasError('jenjang')) : ?>
                <span class="invalid-feedback"><?= $validation->getError('jenjang') ?></span>
            <?php endif; ?>
        </div>

        <div class="form-group">
            <label class="form-label" for="akreditasi">Akreditasi</label>
            <select class="form-control" id="akreditasi" name="akreditasi">
                <option value="">-- Pilih Akreditasi --</option>
                <?php 
                $opsi_akreditasi = ['Unggul', 'Baik Sekali', 'Baik', 'A', 'B', 'C', 'Tidak Terakreditasi'];
                $selected_akreditasi = old('akreditasi', $prodi['akreditasi'] ?? '');
                foreach($opsi_akreditasi as $opsi): 
                ?>
                    <option value="<?= $opsi ?>" <?= $selected_akreditasi == $opsi ? 'selected' : '' ?>>
                        <?= $opsi ?>
                    </option>
                <?php endforeach; ?>
            </select>
        </div>

        <div class="form-group" style="margin-top: 2rem; display: flex; justify-content: flex-end; gap: 1rem;">
            <button type="reset" class="btn btn-secondary">Reset</button>
            <button type="submit" class="btn btn-primary">
                <i class='bx bx-save'></i> Simpan Data
            </button>
        </div>
    </form>
</div>

<?= $this->endSection() ?>
