<?php
session_start();
include '../koneksi.php';
include 'layout_header.php';

$id = isset($_GET['id']) ? (int)$_GET['id'] : '';
$data = [];
if($id != '') {
    $data = mysqli_fetch_assoc(mysqli_query($koneksi, "SELECT * FROM master_siswa WHERE id_siswa='$id'"));
}
function val($key) { global $data; return isset($data[$key]) ? htmlspecialchars($data[$key]) : ''; }
?>

<div class="container-fluid mt-3">
    <h3><?= ($id == '') ? 'Tambah' : 'Edit' ?> Data Siswa</h3>
    <form action="siswa_aksi.php" method="POST" enctype="multipart/form-data">
        <input type="hidden" name="id" value="<?= $id ?>">
        
        <div class="accordion" id="accordionSiswa">
            
            <div class="accordion-item">
                <h2 class="accordion-header"><button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapsePribadi">Data Pribadi</button></h2>
                <div id="collapsePribadi" class="accordion-collapse collapse show" data-bs-parent="#accordionSiswa">
                    <div class="accordion-body">
                        <div class="row">
                            <div class="col-md-6">
                                <label>NIS</label> <input type="text" name="nis" value="<?= val('nis') ?>" class="form-control mb-2" required>
                                <label>NISN</label> <input type="text" name="nisn" value="<?= val('nisn') ?>" class="form-control mb-2">
                                <label>Nama Lengkap</label> <input type="text" name="nama" value="<?= val('nama') ?>" class="form-control mb-2" required>
                            </div>
                            <div class="col-md-6">
                                <label>Jenis Kelamin</label>
                                <select name="jk" class="form-select mb-2">
                                    <option value="Laki-laki" <?= val('jk')=='Laki-laki'?'selected':'' ?>>Laki-laki</option>
                                    <option value="Perempuan" <?= val('jk')=='Perempuan'?'selected':'' ?>>Perempuan</option>
                                </select>
                                <label>Status Siswa</label>
                                <select name="status" class="form-select mb-2">
                                    <option value="Aktif" <?= val('status')=='Aktif'?'selected':'' ?>>Aktif</option>
                                    <option value="Lulus" <?= val('status')=='Lulus'?'selected':'' ?>>Lulus</option>
                                    <option value="Pindah" <?= val('status')=='Pindah'?'selected':'' ?>>Pindah</option>
                                </select>
                                <label>Username</label> 
                                <input type="text" name="username" value="<?= val('username') ?>" class="form-control mb-2" required>

                                <label>Password <?= ($id != '') ? '<small class="text-muted">(Kosongkan jika tidak ingin diubah)</small>' : '' ?></label> 
                                <input type="password" name="password" class="form-control mb-2" <?= ($id == '') ? 'required' : '' ?>>
                                <label>Foto</label> <input type="file" name="foto" class="form-control mb-2">
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="accordion-item">
                <h2 class="accordion-header"><button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseAlamat">Domisili</button></h2>
                <div id="collapseAlamat" class="accordion-collapse collapse" data-bs-parent="#accordionSiswa">
                    <div class="accordion-body">
                        <label>Alamat</label> <textarea name="alamat" class="form-control mb-2"><?= val('alamat') ?></textarea>
                        <div class="row">
                            <div class="col-md-2"><label>RT</label> <input type="text" name="rt" value="<?= val('rt') ?>" class="form-control mb-2"></div>
                            <div class="col-md-2"><label>RW</label> <input type="text" name="rw" value="<?= val('rw') ?>" class="form-control mb-2"></div>
                            <div class="col-md-4"><label>Kelurahan</label> <input type="text" name="kelurahan" value="<?= val('kelurahan') ?>" class="form-control mb-2"></div>
                            <div class="col-md-4"><label>Kecamatan</label> <input type="text" name="kecamatan" value="<?= val('kecamatan') ?>" class="form-control mb-2"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="accordion-item">
                <h2 class="accordion-header"><button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseOrtu">Data Orang Tua</button></h2>
                <div id="collapseOrtu" class="accordion-collapse collapse" data-bs-parent="#accordionSiswa">
                    <div class="accordion-body">
                        <div class="row">
                            <div class="col-md-4">
                                <h6>Ayah</h6>
                                <input type="text" name="nama_ayah" placeholder="Nama" value="<?= val('nama_ayah') ?>" class="form-control mb-2">
                                <input type="text" name="pendidikan_ayah" placeholder="Pendidikan" value="<?= val('pendidikan_ayah') ?>" class="form-control mb-2">
                                <input type="text" name="pekerjaan_ayah" placeholder="Pekerjaan" value="<?= val('pekerjaan_ayah') ?>" class="form-control mb-2">
                            </div>
                            <div class="col-md-4">
                                <h6>Ibu</h6>
                                <input type="text" name="nama_ibu" placeholder="Nama" value="<?= val('nama_ibu') ?>" class="form-control mb-2">
                                <input type="text" name="pendidikan_ibu" placeholder="Pendidikan" value="<?= val('pendidikan_ibu') ?>" class="form-control mb-2">
                                <input type="text" name="pekerjaan_ibu" placeholder="Pekerjaan" value="<?= val('pekerjaan_ibu') ?>" class="form-control mb-2">
                            </div>
                            <div class="col-md-4">
                                <h6>Wali</h6>
                                <input type="text" name="nama_wali" placeholder="Nama" value="<?= val('nama_wali') ?>" class="form-control mb-2">
                                <input type="text" name="pendidikan_wali" placeholder="Pendidikan" value="<?= val('pendidikan_wali') ?>" class="form-control mb-2">
                                <input type="text" name="pekerjaan_wali" placeholder="Pekerjaan" value="<?= val('pekerjaan_wali') ?>" class="form-control mb-2">
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="mt-3">
            <button type="submit" name="simpan" class="btn btn-primary px-4"><i class="fa fa-save"></i> Simpan Data</button>
            <a href="siswa.php" class="btn btn-secondary">Kembali</a>
        </div>
    </form>
</div>
<?php include 'layout_footer.php'; ?>