<?php
if(!defined('INDEX')) die();

$id = $_GET['id'];
$query = mysqli_query($con, "SELECT * FROM siswa WHERE id_siswa='$id'");
$data = mysqli_fetch_array($query);
?>

<section class="content-header">
    <h1><i class="fa fa-edit"></i> Edit Data Siswa</h1>
</section>

<section class="content">
    <div class="row">
        <div class="col-md-3">
            <div class="box box-primary">
                <div class="box-body box-profile">
                    <img id="preview" class="profile-user-img img-responsive img-bordered" 
                         src="images/<?= $data['foto'] ?>" 
                         style="width:150px; height:150px; object-fit:cover;" alt="User profile picture">
                    <h3 class="profile-username text-center"><?= $data['nama_siswa'] ?></h3>
                    <p class="text-muted text-center"><?= $data['nis'] ?></p>
                    <ul class="list-group list-group-unbordered">
                        <li class="list-group-item">
                            <b>Poin Saat Ini</b> <a class="pull-right badge bg-blue"><?= $data['poin_sis'] ?></a>
                        </li>
                    </ul>
                </div>
            </div>
        </div>

        <div class="col-md-9">
            <div class="box box-primary">
                <div class="box-header with-border">
                    <h3 class="box-title">Formulir Pembaruan Data</h3>
                </div>
                
                <form action="?hal=siswa_update" method="post" enctype="multipart/form-data" class="form-horizontal">
                    <input type="hidden" name="id" value="<?= $data['id_siswa'] ?>">
                    
                    <div class="box-body">
                        <div class="form-group">
                            <label class="col-sm-2 control-label">Ganti Foto</label>
                            <div class="col-sm-10">
                                <input type="file" name="foto" class="form-control" onchange="readURL(this);">
                                <small class="text-muted">*Kosongkan jika tidak ingin mengubah foto</small>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">Nama Lengkap</label>
                            <div class="col-sm-10">
                                <div class="input-group">
                                    <span class="input-group-addon"><i class="fa fa-user"></i></span>
                                    <input type="text" name="nama_siswa" class="form-control" value="<?= $data['nama_siswa'] ?>" required>
                                </div>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">NISN</label>
                            <div class="col-sm-10">
                                <div class="input-group">
                                    <span class="input-group-addon"><i class="fa fa-id-card"></i></span>
                                    <input type="text" name="nis" class="form-control" value="<?= $data['nis'] ?>" readonly>
                                </div>
                                <small class="text-red">*NISN tidak dapat diubah</small>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">Tanggal Lahir</label>
                            <div class="col-sm-10">
                                <div class="input-group">
                                    <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                                    <input type="date" name="tanggal_lahir" class="form-control" value="<?= $data['tanggal_lahir'] ?>" required>
                                </div>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">Kelas</label>
                            <div class="col-sm-10">
                                <?php $k = $data['kelas']; ?>
                                <label class="radio-inline"><input type="radio" name="kelas" value="10" <?= ($k=='10')?'checked':'' ?>> 10</label>
                                <label class="radio-inline"><input type="radio" name="kelas" value="11" <?= ($k=='11')?'checked':'' ?>> 11</label>
                                <label class="radio-inline"><input type="radio" name="kelas" value="12" <?= ($k=='12')?'checked':'' ?>> 12</label>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">Jurusan</label>
                            <div class="col-sm-10">
                                <?php $j = $data['jurusan']; ?>
                                <label class="radio-inline"><input type="radio" name="jurusan" value="PPLG" <?= ($j=='PPLG')?'checked':'' ?>> PPLG</label>
                                <label class="radio-inline"><input type="radio" name="jurusan" value="AKL" <?= ($j=='AKL')?'checked':'' ?>> AKL</label>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">Gender</label>
                            <div class="col-sm-10">
                                <?php $g = $data['gender']; ?>
                                <label class="radio-inline"><input type="radio" name="gender" value="L" <?= ($g=='L')?'checked':'' ?>> Laki-laki</label>
                                <label class="radio-inline"><input type="radio" name="gender" value="P" <?= ($g=='P')?'checked':'' ?>> Perempuan</label>
                            </div>
                        </div>
                    </div>

                    <div class="box-footer">
                        <div class="pull-right">
                            <a href="?hal=siswa" class="btn btn-default"><i class="fa fa-rotate-left"></i> Batal</a>
                            <button type="submit" class="btn btn-primary"><i class="fa fa-save"></i> Simpan Perubahan</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</section>

<script>
// Fungsi untuk preview foto saat dipilih
function readURL(input) {
    if (input.files && input.files[0]) {
        var reader = new FileReader();
        reader.onload = function (e) {
            $('#preview').attr('src', e.target.result);
        };
        reader.readAsDataURL(input.files[0]);
    }
}
</script>