<?= $this->extend('Admin/Layout/app_layout'); ?>
<?= $this->section('content'); ?>

<div class="layout-px-spacing">
    <div class="middle-content container-xxl p-0">
        <div class="layout-top-spacing">
            <div class="widget widget-three">
                <form class="form-horizontal" id="profil-form">
                    <div class="widget-heading">
                        <h5 class="">Detail Siswa</h5>
                        <div class="task-action">
                            <button type="submit" class="btn btn-primary"><i data-feather="save"></i> <span class="btn-text-inner">Simpan</span></button>
                        </div>
                    </div>
                    <div class="widget-content">
                        <input type="hidden" name="id" class="form-control form-control-sm">
                        <div class="form-group row align-items-center mb-3">
                            <label for="appname" class="form-control-label col-sm-3 text-md-right">Nama Lengkap</label>
                            <div class="col-sm-6 col-md-9">
                                <input type="text" name="nama" class="form-control form-control-sm">
                            </div>
                        </div>
                        <div class="form-group row align-items-center mb-3">
                            <label for="npsn" class="form-control-label col-sm-3 text-md-right">NIS</label>
                            <div class="col-sm-6 col-md-9">
                                <input type="text" name="nis" class="form-control form-control-sm">
                            </div>
                        </div>
                        <div class="form-group row align-items-center mb-3">
                            <label for="nisn" class="form-control-label col-sm-3 text-md-right">NISN</label>
                            <div class="col-sm-6 col-md-9">
                                <input type="text" name="nisn" class="form-control form-control-sm">
                            </div>
                        </div>
                        <div class="form-group row align-items-center mb-3">
                            <label for="tempat" class="form-control-label col-sm-3 text-md-right">Tempat</label>
                            <div class="col-sm-6 col-md-9">
                                <input type="text" name="tempat" class="form-control form-control-sm">
                            </div>
                        </div>
                        <div class="form-group row align-items-center mb-3">
                            <label for="tgl_lahir" class="form-control-label col-sm-3 text-md-right">Tanggal Lahir</label>
                            <div class="col-sm-6 col-md-9">
                                <input type="text" name="tgl_lahir" class="form-control form-control-sm">
                            </div>
                        </div>
                        <div class="form-group row align-items-center mb-3">
                            <label for="agama" class="form-control-label col-sm-3 text-md-right">Agama</label>
                            <div class="col-sm-6 col-md-9">
                                <input type="text" name="agama" class="form-control form-control-sm">
                            </div>
                        </div>
                        <div class="form-group row align-items-center mb-3">
                            <label for="alamat" class="form-control-label col-sm-3 text-md-right">Alamat Lengkap</label>
                            <div class="col-sm-6 col-md-9">
                                <input type="text" name="alamat" class="form-control form-control-sm">
                            </div>
                        </div>
                        <div class="form-group row align-items-center mb-3">
                            <label for="nohp" class="form-control-label col-sm-3 text-md-right">No Hp</label>
                            <div class="col-sm-6 col-md-9">
                                <input type="text" name="nohp" class="form-control form-control-sm">
                            </div>
                        </div>
                        <div class="form-group row align-items-center mb-3">
                            <label for="sekolah_asal" class="form-control-label col-sm-3 text-md-right">Asal Sekolah</label>
                            <div class="col-sm-6 col-md-9">
                                <input type="text" name="sekolah_asal" class="form-control form-control-sm">
                            </div>
                        </div>
                        <div class="form-group row align-items-center mb-3">
                            <label for="diterima_kelas" class="form-control-label col-sm-3 text-md-right">Diterima dikelas</label>
                            <div class="col-sm-6 col-md-9">
                                <input type="text" name="diterima_kelas" class="form-control form-control-sm">
                            </div>
                        </div>
                        <div class="form-group row align-items-center mb-3">
                            <label for="tgl_diterima" class="form-control-label col-sm-3 text-md-right">Tanggal Diterima</label>
                            <div class="col-sm-6 col-md-9">
                                <input type="text" name="tgl_diterima" class="form-control form-control-sm">
                            </div>
                        </div>
                        <div class="form-group row align-items-center mb-3">
                            <label for="nama_ayah" class="form-control-label col-sm-3 text-md-right">Nama Ayah</label>
                            <div class="col-sm-6 col-md-9">
                                <input type="text" name="nama_ayah" class="form-control form-control-sm">
                            </div>
                        </div>
                        <div class="form-group row align-items-center mb-3">
                            <label for="nama_ibu" class="form-control-label col-sm-3 text-md-right">Nama Ibu</label>
                            <div class="col-sm-6 col-md-9">
                                <input type="text" name="nama_ibu" class="form-control form-control-sm">
                            </div>
                        </div>
                        <div class="form-group row align-items-center mb-3">
                            <label for="pekerjaan_ayah" class="form-control-label col-sm-3 text-md-right">Pekerjaan Ayah</label>
                            <div class="col-sm-6 col-md-9">
                                <input type="text" name="pekerjaan_ayah" class="form-control form-control-sm">
                            </div>
                        </div>
                        <div class="form-group row align-items-center mb-3">
                            <label for="pekerjaan_ibu" class="form-control-label col-sm-3 text-md-right">Pekerjaan Ibu</label>
                            <div class="col-sm-6 col-md-9">
                                <input type="text" name="pekerjaan_ibu" class="form-control form-control-sm">
                            </div>
                        </div>
                        <div class="form-group row align-items-center mb-3">
                            <label for="nama_wali" class="form-control-label col-sm-3 text-md-right">Nama Wali</label>
                            <div class="col-sm-6 col-md-9">
                                <input type="text" name="nama_wali" class="form-control form-control-sm">
                            </div>
                        </div>
                        <div class="form-group row align-items-center mb-3">
                            <label for="pekerjaan_wali" class="form-control-label col-sm-3 text-md-right">Pekerjaan Wali</label>
                            <div class="col-sm-6 col-md-9">
                                <input type="text" name="pekerjaan_wali" class="form-control form-control-sm">
                            </div>
                        </div>
                        <div class="form-group row align-items-center mb-3">
                            <label class="form-control-label col-sm-3 text-md-right">Foto Profil</label>
                            <div class="col-sm-6 col-md-9">
                                <div class="custom-file">
                                    <input type="file" name="fotoprofil" class="custom-file-input form-control" id="fotoprofil">

                                </div>
                                <div class="form-text text-muted">The image must have a maximum size of 1MB</div>
                                <img id="profilPreview" src="" alt="fotoprofil Preview" style="max-width: 100px; display: none;">
                            </div>
                        </div>

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



<?= $this->endSection(); ?>
<?= $this->section('pagejs'); ?>
<script type="text/javascript">
    $(document).ready(function() {
        $.ajax({
            url: "<?= base_url('siswa/edit/') . $siswa->id ?>",
            type: "GET",
            dataType: "JSON",
            success: function(data) {
                console.log(data);
                $('[name="id"]').val(data.id);
                $('[name="nama"]').val(data.nama);
                $('[name="nis"]').val(data.nis);
                $('[name="nisn"]').val(data.nisn);
                $('[name="tempat"]').val(data.tempat);
                $('[name="tgl_lahir"]').val(data.tgl_lahir);
                $('[name="agama"]').val(data.agama);
                $('[name="alamat"]').val(data.alamat);
                $('[name="nohp"]').val(data.nohp);
                $('[name="sekolah_asal"]').val(data.sekolah_asal);
                $('[name="diterima_kelas"]').val(data.diterima_kelas);
                $('[name="tgl_diterima"]').val(data.tgl_diterima);
                $('[name="nama_ayah"]').val(data.nama_ayah);
                $('[name="nama_ibu"]').val(data.nama_ibu);
                $('[name="pekerjaan_ayah"]').val(data.pekerjaan_ayah);
                $('[name="pekerjaan_ibu"]').val(data.pekerjaan_ibu);
                $('[name="nama_wali"]').val(data.nama_wali);
                $('[name="pekerjaan_wali"]').val(data.pekerjaan_wali);
                $('#profilPreview').attr('src', '<?= base_url('assets/img/fotosiswa/') ?>' + data.foto).show();

            },
            error: function(jqXHR, textStatus, errorThrown) {
                //console.log(jqXHR);
                alert('Error get data from ajax');
            }
        });
        $('#fotoprofil').change(function() {
            readURL(this, '#profilPreview');
            $(this).next('.custom-file-label').html(event.target.files[0].name);
        });

        function readURL(input, previewElement) {
            if (input.files && input.files[0]) {
                var reader = new FileReader();

                reader.onload = function(e) {
                    $(previewElement).attr('src', e.target.result).show();
                }

                reader.readAsDataURL(input.files[0]);
            }
        }
        $('#profil-form').submit(function(e) {
            e.preventDefault();
            var formData = new FormData(this);
            // console.log(formData);
            $.ajax({
                url: '<?= base_url('siswa/updateprofil') ?>', // Sesuaikan dengan URL CI4 Anda
                type: 'POST',
                data: formData,
                dataType: 'json',
                contentType: false,
                processData: false,
                success: function(response) {
                    // $('#responseMessage').html('<div class="alert alert-success" role="alert">' + response.message + '</div>');
                    Snackbar.show({
                        text: response.message,
                        pos: 'top-center'
                    });
                },
                error: function() {
                    // $('#responseMessage').html('<div class="alert alert-danger" role="alert">Terjadi kesalahan saat mengirim data.</div>');
                }
            });
        });
    });
</script>
<?= $this->endSection(); ?>