<?php
defined('APK') or exit('Anda tidak dizinkan mengakses langsung script ini!');
?>
<?php if ($ac == '') : ?>
    <div class='row'>
        <div class='col-md-12'>
           <div class="card">
              <div class="card-header">	
              <?php if($user['level']=='admin'): ?>			  
                    <div class='right'>                    
                         <a href="#" data-bs-toggle='modal' data--bs-backdrop="static" data-bs-target='#tambahsiswa' class='btn btn-outline-success'><i class='fa fa-plus-circle'></i>Siswa</a>                         
                        <a href='?pg=uplfotosiswa' class='btn btn-outline-primary'><i class='fas fa-upload'></i>Foto</a>        
                    </div>
					<?php endif; ?>
					 <h5 class='box-title'>Peserta Ujian</h5>
                </div>
			
			  <div class="card-body">
                    <div class='table-responsive'>
                        <table id='datatable1' class='table'>
                            <thead>
                                <tr>
                                    <th>No</th>
                                    <th>No_Peserta</th>
                                    <th>Nama</th>
                                    <th>Kelas</th>
                                    <th>sesi</th>
                                    <th>ruang</th>
                                    <th>Username</th>
                                    <th>Password</th>
									  <?php if($user['level']=='admin'): ?>
                                   <th>Action</th>
								   	<?php endif; ?>
                                </tr>
                            </thead>
							 <tbody>
                            <?php
							$no = 0;
                            $query = mysqli_query($koneksi, "select * from siswa order by id_siswa DESC");
                            while ($siswa = mysqli_fetch_array($query)) {							
                                $no++;
                            ?>
                                <tr>
                                    <td><?= $no; ?></td>
                                    <td><?= $siswa['no_peserta'] ?></td>
                                    <td><?= $siswa['nama'] ?></td>
									<td><?= $siswa['id_kelas'] ?></td>
									<td><?= $siswa['sesi'] ?> </td>
									<td><?= $siswa['ruang'] ?></td>
									<td><?= $siswa['username'] ?></td>
									<td><?= $siswa['password'] ?> </td>
									
									<?php if($user['level']=='admin'): ?>
									<td>
									 <a href="?pg=siswa&ac=edit&id=<?= $siswa['id_siswa'] ?>" class="btn btn-sm btn-outline-primary"><i class="fas fa-edit"></i></a>
									<button data-id="<?= $siswa['id_siswa'] ?>" class="hapus btn-sm btn btn-outline-danger"><i class="fas fa-trash"></i></button>
									</td>
										<?php endif; ?>
							       <?php } ?>  
                                </tr>
                        </tbody>
                        </table>
                    </div>
                   <div class="modal fade" id="tambahsiswa" tabindex="-1" aria-labelledby="tambahsiswa" aria-hidden="true">
				   <div class="modal-dialog modal-lg">
					   <div class="modal-content">
						  <div class="modal-header">
							<h5 class="modal-title" id="tambahjadwal">Tambah Siswa</h5>
							   <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
									</div>
									  <div class="modal-body">
                                    <form id='form-tambah' action=''>
                                        <div class='form-group'>
                                            <div class='row'>
                                                <div class='col-md-6'>
                                                    <label class="bold">NIS</label>
                                                    <input type='text' name='nis' class='form-control' required='true' />
                                                </div>
                                                <div class='col-md-6'>
                                                    <label class="bold">Nomor Peserta</label>
                                                    <input type='text' name='no_peserta' class='form-control' required='true' />
                                                </div>
                                            </div>
                                        </div><p>
                                        <div class='form-group'>
                                            <label class="bold">Nama</label>
                                            <input type='text' name='nama' class='form-control' required='true' />
                                        </div><p>
                                        <div class='form-group'>
                                            <div class='row'>
                                                <div class='col-md-4'>
                                                    <label class="bold">Kelas</label>
                                                    <select name='id_kelas' class='form-select' required='true'>
                                                      
                                                        <?php
                                                        $kelasQ = mysqli_query($koneksi, "SELECT * FROM kelas ORDER BY nama ASC");
                                                        while ($kelas = mysqli_fetch_array($kelasQ)) {
                                                            echo "<option value='$kelas[id_kelas]'>$kelas[nama]</option>";
                                                        }
                                                        ?>
                                                    </select>
                                                </div>
                                                <div class='col-md-4'>
                                                    <label class="bold">Level</label>
                                                    <select name='level' class='form-select' required='true'>
                                                      
                                                        <?php
                                                        $pkQ = mysqli_query($koneksi, "SELECT * FROM level ");
                                                        while ($pk = mysqli_fetch_array($pkQ)) {
                                                            echo "<option value='$pk[kode_level]'>$pk[kode_level]</option>";
                                                        }
                                                        ?>
                                                    </select>
                                                </div>

                                                <div class='col-md-4'>
                                                    <label class="bold">Jurusan</label>
                                                    <select name='idpk' class='form-select' required='true'>
                                                       
                                                        <?php
                                                        $pkQ = mysqli_query($koneksi, "SELECT * FROM siswa GROUP BY idpk");
                                                        while ($pk = mysqli_fetch_array($pkQ)) {
                                                            echo "<option value='$pk[idpk]'>$pk[idpk]</option>";
                                                        }
                                                        ?>
                                                    </select>
                                                </div>

                                            </div>
                                        </div><p>
                                        <div class='form-group'>
                                            <div class='row'>
                                                <div class='col-md-4'>
                                                    <label class="bold">Sesi</label>
                                                    <select name='idsesi' class='form-select' required='true'>
                                                       
                                                        <?php
                                                        $sesiQ = mysqli_query($koneksi, "SELECT * FROM sesi ");
                                                        while ($sesi = mysqli_fetch_array($sesiQ)) {

                                                            echo "<option value='$sesi[kode_sesi]' $s>$sesi[kode_sesi]</option>";
                                                        }
                                                        ?>
                                                    </select>
                                                </div>
                                                <div class='col-md-4'>
                                                    <label class="bold">Ruang</label>
                                                    <select name='ruang' class='form-select' required='true'>
                                                        
                                                        <?php
                                                        $pkQ = mysqli_query($koneksi, "SELECT * FROM ruang ");
                                                        while ($pk = mysqli_fetch_array($pkQ)) {
                                                            echo "<option value='$pk[kode_ruang]'>$pk[kode_ruang]</option>";
                                                        }
                                                        ?>
                                                    </select>
                                                </div>
                                                <div class='col-md-4'>
                                                    <label class="bold">Server</label>
                                                    <select name='server' class='form-select' required='true'>
                                                       
                                                        <?php
                                                        $pkQ = mysqli_query($koneksi, "SELECT * FROM server ");
                                                        while ($sr = mysqli_fetch_array($pkQ)) {
                                                            echo "<option value='$sr[kode_server]'>$sr[kode_server]</option>";
                                                        }
                                                        ?>
                                                    </select>
                                                </div>
                                            </div>
                                        </div><p>
                                        <div class='row'>
                                            <div class='col-md-6'>
                                                <div class='form-group'>
                                                    <label class="bold">Username</label>
                                                    <input type='text' name='username' class='form-control' required='true' />
                                                </div>
                                            </div>
                                            <div class='col-md-6'>
                                                <div class='form-group-sm'>
                                                    <label class="bold">Agama</label>
                                                    <input type='text' name='agama' class='form-control' required='true' />
                                                </div>
                                            </div>
                                        </div><p>
                                        <div class='form-group'>
                                            <div class='row'>
                                                <div class='col-md-6'>
                                                    <label class="bold">Password</label>
                                                    <input type='password' name='pass1' class='form-control' required='true' />
                                                </div>
                                                <div class='col-md-6'>
                                                    <label class="bold">Ulang Password</label>
                                                    <input type='password' name='pass2' class='form-control' required='true' />
                                                </div>
                                            </div>
                                        </div><p>
                                        <div class='row'>
                                            <div class='col-md-6'>
                                                <div class='form-group'>
                                                    <label class="bold">No hp</label>
                                                    <input type='text' name='hp' class='form-control' />
                                                </div>
												</div>
												<div class='col-md-6'>
                                                <div class='form-group'>
                                                    <label class="bold">Jeis Kelamin</label>
                                                     <select name='jk' class='form-select' required='true'>
                                                        <option value=''></option>
                                                        <option value='Laki-laki'>Laki-laki</option>
														<option value='Perempuan'>Perempuan</option>
                                                    </select>
                                                </div>
                                            </div>
                                        </div>
                                        <div class='modal-footer'>
                                            <div class='box-tools pull-right btn-group'>
                                                <button type='submit' name='tambahsiswa' class='btn btn-sm btn-outline-primary'> Simpan</button>
                                              
                                            </div>
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
	
<?php elseif ($ac == 'edit') : ?>
    <?php
    $id = $_GET['id'];
    $siswa = mysqli_fetch_array(mysqli_query($koneksi, "SELECT * FROM siswa WHERE id_siswa='$id'"));
    ?>
   <div class='row'>
        <div class='col-md-12'>
           <div class="card">
              <div class="card-header">	
			  <form id="form-edit" method='post' class="form-horizontal">             
                        <div class='right'>
                            <button type='submit' name='submit' class='btn btn-sm btn-outline-primary'><i class='fa fa-check-circle'></i>Simpan</button>
                            <a href='?pg=<?= $pg ?>' class='btn btn-sm btn-outline-danger' ><i class='fa fa-times-circle'></i>Back</a>
                        </div>
					 <h5 class='box-title'>Peserta Ujian</h5>
                </div>
			
                    <div class='card-body'>
					
                        <input type='hidden' name='idu' value="<?= $siswa['id_siswa'] ?>" />
                        <div class='form-group'>
                            <div class='row'>
                                <div class='col-md-3'>
                                    <label class="bold">NIS</label>
                                    <input type='text' name='nis' class='form-control' value="<?= $siswa['nis'] ?>" required='true' />
                                </div>
                                <div class='col-md-3'>
                                    <label class="bold">Nomor Peserta</label>
                                    <input type='text' name='no_peserta' class='form-control' value="<?= $siswa['no_peserta'] ?>" readonly />
                                </div>  
                        <div class='col-md-6'>
                            <label class="bold">Nama</label>
                            <input type='text' name='nama' class='form-control' value="<?= $siswa['nama'] ?>" required='true' />
                        </div>
						</div>
						</div><p>
                        <div class='form-group'>
                            <div class='row'>
                                <div class='col-md-3'>
                                    <label class="bold">Kelas</label>
                                    <select name='id_kelas' class='form-select' required='true'>
                                      
                                        <?php
                                        $kelasQ = mysqli_query($koneksi, "SELECT * FROM kelas ORDER BY nama ASC");
                                        while ($kelas = mysqli_fetch_array($kelasQ)) {
                                            ($kelas['id_kelas'] == $siswa['id_kelas']) ? $s = 'selected' : $s = '';
                                            echo "<option value='$kelas[id_kelas]' $s>$kelas[nama]</option>";
                                        }
                                        ?>
                                    </select>
                                </div>
                                <div class='col-md-3'>
                                    <label class="bold">Level</label>
                                    <select name='level' class='form-select' required='true'>
                                      
                                        <?php
                                        $pkQ = mysqli_query($koneksi, "SELECT * FROM level ");
                                        while ($pk = mysqli_fetch_array($pkQ)) {
                                            ($pk['kode_level'] == $siswa['level']) ? $s = 'selected' : $s = '';
                                            echo "<option value='$pk[kode_level]' $s>$pk[kode_level]</option>";
                                        }
                                        ?>
                                    </select>
                                </div>

                                <div class='col-md-3'>
                                    <label class="bold">Jurusan</label>
                                    <select name='idpk' class='form-select' >
                                      
                                        <?php
                                        $pkQ = mysqli_query($koneksi, "SELECT * FROM siswa GROUP BY idpk");
                                        while ($pk = mysqli_fetch_array($pkQ)) {
                                            ($pk['idpk'] == $siswa['idpk']) ? $s = 'selected' : $s = '';
                                            echo "<option value='$pk[idpk]' $s>$pk[idpk]</option>";
                                        }
                                        ?>
                                    </select>
                                </div>
           
                                <div class='col-md-3'>
                                    <label class="bold">Sesi</label>
                                    <select name='idsesi' class='form-select' required='true'>
                                       
                                        <?php
                                        $sesiQ = mysqli_query($koneksi, "SELECT * FROM sesi ");
                                        while ($sesi = mysqli_fetch_array($sesiQ)) {
                                            ($sesi['kode_sesi'] == $siswa['sesi']) ? $s = 'selected' : $s = '';
                                            echo "<option value='$sesi[kode_sesi]' $s>$sesi[kode_sesi]</option>";
                                        }
                                        ?>
                                    </select>
                                </div>
								</div>
								</div><p>
								<div class='form-group'>
                            <div class='row'>
                                <div class='col-md-3'>
                                    <label class="bold">Ruang</label>
                                    <select name='ruang' class='form-select' required='true'>
                                      
                                        <?php
                                        $pkQ = mysqli_query($koneksi, "SELECT * FROM ruang ");
                                        while ($pk = mysqli_fetch_array($pkQ)) {
                                            ($pk['kode_ruang'] == $siswa['ruang']) ? $s = 'selected' : $s = '';
                                            echo "<option value='$pk[kode_ruang]' $s>$pk[kode_ruang]</option>";
                                        }
                                        ?>
                                    </select>
                                </div>
                                <div class='col-md-3'>
                                    <label class="bold">Server</label>
                                    <select name='server' class='form-select' required='true'>
                                       
                                        <?php
                                        $pkQ = mysqli_query($koneksi, "SELECT * FROM server ");
                                        while ($sr = mysqli_fetch_array($pkQ)) {
                                            ($sr['kode_server'] == $siswa['server']) ? $s = 'selected' : $s = '';
                                            echo "<option value='$sr[kode_server]' $s>$sr[kode_server]</option>";
                                        }
                                        ?>
                                    </select>
                                </div>
                            
                                <div class='col-md-3'>
                                    <label class="bold">Username</label>
                                    <input type='text' name='username' class='form-control' value="<?= $siswa['username'] ?>" readonly />
                                </div>
                           
                             <div class='col-md-3'>
                                    <label class="bold">Agama</label>
                                    <input type='text' name='agama' class='form-control' value="<?= $siswa['agama'] ?>" required='true' />
                                </div>
                            </div>
                        </div><p>
                        <div class='form-group'>
                            <div class='row'>
                                <div class='col-md-6'>
                                    <label class="bold">Password</label>
                                    <input type='password' name='pass1' class='form-control' />
                                </div>
                                <div class='col-md-6'>
                                    <label class="bold">Ulang Password</label>
                                    <input type='password' name='pass2' class='form-control' />
                                </div>
                            </div>
                        </div><p>
						<div class='form-group'>
                        <div class='row'>
                            <div class='col-md-6'>
                                    <label class="bold">No hp</label>
                                    <input type='text' name='hp' class='form-control' value="<?= $siswa['hp'] ?>" />
                                </div>
								<div class='col-md-6'>
                                    <label class="bold">Photo</label>
                                    <input type='file' name='file' class='form-control'  />
                                </div>
                            </div>
                        </div>
						
                        <label class="bold">
						<br>
                            <input type='checkbox' class='icheckbox_square-green' name='status' value='aktif' <?php if ($siswa['status'] == 'aktif') {
                                                                                                                    echo "checked='true'";
                                                                                                                } ?> /> Status Siswa aktif / tidak
                        </label>
                    </div>
                </div>
            </form>
        </div>
    </div>

<?php endif ?>

<script>
    $('#form-tambah').submit(function(e) {
        e.preventDefault();
        $.ajax({
            type: 'POST',
            url: 'sandik_ujian/crud_siswa.php?pg=tambah',
            data: $(this).serialize(),
            beforeSend: function() {
                $('form button').on("click", function(e) {
                    e.preventDefault();
                });
            },
            success: function(data) {
                console.log(data);
                if (data == 'OK') {
             iziToast.info(
            {
                title: 'Sukses!',
                message: 'Data berhasil disimpan',
				titleColor: '#FFFF00',
				messageColor: '#fff',
				backgroundColor: 'rgba(0, 0, 0, 0.5)',
				 progressBarColor: '#FFFF00',
                  position: 'topRight'
                    });
                    setTimeout(function() {
                        window.location.reload();
                    }, 2000);
                } else {
                  iziToast.info(
            {
                title: 'Gagal!',
                message: 'Siswa suda ada',
				titleColor: '#FFFF00',
				messageColor: '#fff',
				backgroundColor: 'rgba(0, 0, 0, 0.5)',
				 progressBarColor: '#FFFF00',
                  position: 'topRight'
                    });
					setTimeout(function() {
                        window.location.reload();
                    }, 2000);
                }

            }
        });
        return false;
    });
	
    $('#form-edit').submit(function(e) {
      e.preventDefault();
        var data = new FormData(this);
      
        $.ajax({
            type: 'POST',
            url: 'sandik_ujian/crud_siswa.php?pg=ubah',
            enctype: 'multipart/form-data',
            data: data,
            cache: false,
            contentType: false,
            processData: false,
            success: function(data) {
              iziToast.info(
            {
                title: 'Sukses!',
                message: 'Data berhasil disimpan',
				titleColor: '#FFFF00',
				messageColor: '#fff',
				backgroundColor: 'rgba(0, 0, 0, 0.5)',
				 progressBarColor: '#FFFF00',
                  position: 'topRight'			  
                });
                setTimeout(function() {
                    window.location.reload();
                }, 2000);

            }
        });
        return false;
    });
 
    $('#datatable1').on('click', '.hapus', function() {
        var id = $(this).data('id');
        console.log(id);
        Swal.fire({
				  title: 'Are you sure?',
				  text: "You won't be able to revert this!",
				  type: 'warning',
				  showCancelButton: true,
				  confirmButtonColor: '#3085d6',
				  cancelButtonColor: '#d33',
				  confirmButtonText: 'Yes, delete it!'	
        }).then((result) => {
            if (result.value) {
                $.ajax({
                    url: 'sandik_ujian/crud_siswa.php?pg=hapus',
                    method: "POST",
                    data: 'id_siswa=' + id,
                    success: function(data) {
                  iziToast.info(
            {
                title: 'Sukses!',
                message: 'Data berasil dihapus',
				titleColor: '#FFFF00',
				messageColor: '#fff',
				backgroundColor: 'rgba(0, 0, 0, 0.5)',
				 progressBarColor: '#FFFF00',
                  position: 'topRight'			  
                });
                        setTimeout(function() {
                            window.location.reload();
                        }, 2000);
                    }
                });
            }
            return false;
        })

    });
</script>