<?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='panel panel-default'>
               <div class="panel-heading">
                  
                    <div class='box-tools pull-right'>
                        <?php if ($user['level'] == 'admin') : ?>
                            <?php if ($setting['server'] == 'pusat') : ?>
                                <a href="#" data-toggle='modal' data-backdrop="static" data-target='#tambahsiswa' class='btn btn-sm btn-default'><i class='glyphicon glyphicon-plus'></i> <span class='hidden-xs'>Siswa</span></a>
                            <?php endif; ?>
                            <a href='?pg=uplfotosiswa' class='btn btn-sm btn-default'><i class='fa fa-upload'></i> <span class='hidden-xs'> Foto</span></a>
                           
                        <?php endif ?>
                    </div>
					 <h4 class='box-title'><i class="fas fa-graduation-cap fa-fw"></i> Peserta Ujian</h4>
                </div>
				
			  <div class="box-body">
                    <ul class="nav nav-tabs">
                        <li class="active"><a href="#tab_1" data-toggle="tab" aria-expanded="true" >Peserta</a></li>
                        
                    </ul>
					
					<div class="tab-content">
                        <div class="tab-pane active" id="tab_1" >
						<br>
						<div class="panel panel-default">	
                <div class='box-body'>
                    <div class='table-responsive'>
                        <table style="font-size: 12px" id='example1' class='table'>
                            <thead>
                                <tr>
                                    <th width='3px'></th>
                                    <th>No_Peserta</th>
                                    <th width="20%">Nama</th>
                                    <th>Level</th>
                                    <th>Kelas</th>
                                    <th>Jurusan</th>
                                    <th>sesi</th>
                                    <th>ruang</th>
                                    <th>Username</th>
                                    <th>Password</th>
                                    <th>Server</th>
									
                                    <th>Photo</th>
                                   
                                  
                                </tr>
                            </thead>
							 <tbody>
                            <?php
							$no = 0;
                            $query = mysqli_query($koneksi, "select * from siswa WHERE ruang='$user[ruang]' 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['level'] ?> </td>
									<td><?= $siswa['id_kelas'] ?></td>
									<td><?= $siswa['idpk'] ?></td>
									<td><?= $siswa['sesi'] ?> </td>
									<td><?= $siswa['ruang'] ?></td>
									<td><?= $siswa['username'] ?></td>
									<td><?= $siswa['password'] ?> </td>
									<td><?= $siswa['server'] ?></td>
									
									<td>
									<?php if($siswa['jenis_kelamin']=='Laki-laki'): ?>
									<?php if($siswa['foto']<>''){ ?>
									<img src="../foto/fotosiswa/<?= $siswa['foto'] ?>" style="width:30px;height:30px" >
									<?php }else{ ?>
									<img src="../dist/img/laki.png" style="width:30px;height:30px"  >
									<?php } ?>
									<?php else: ?>
									<?php if($siswa['foto']<>''){ ?>
									<img src="../foto/fotosiswa/<?= $siswa['foto'] ?>" style="width:30px;height:30px" >
									<?php }else{ ?>
									<img src="../dist/img/prp.png" style="width:30px;height:30px" >
									<?php } ?>
									<?php endif; ?>
									</td>
									
							       <?php } ?>  
                                </tr>
                        </tbody>
                        </table>
                    </div>
                    <div class='modal fade' id='tambahsiswa' style='display: none;'>
                        <div class='modal-dialog'>
                            <div class='modal-content'>
                               <div class='panel panel-default'>
                               <div class="panel-heading">
                                    <button class='close' data-dismiss='modal'><span aria-hidden='true'><i class='glyphicon glyphicon-remove'></i></span></button>
                                    <h4 class='modal-title'><i class="fas fa-user-friends fa-fw   "></i> Tambah Siswa</h4>
                                </div>
                                <div class='modal-body'>
                                    <form id='form-tambah' action=''>
                                        <div class='form-group-sm'>
                                            <div class='row'>
                                                <div class='col-md-6'>
                                                    <label>NIS</label>
                                                    <input type='text' name='nis' class='form-control' required='true' />
                                                </div>
                                                <div class='col-md-6'>
                                                    <label>Nomor Peserta</label>
                                                    <input type='text' name='no_peserta' class='form-control' required='true' />
                                                </div>
                                            </div>
                                        </div>
                                        <div class='form-group-sm'>
                                            <label>Nama</label>
                                            <input type='text' name='nama' class='form-control' required='true' />
                                        </div>
                                        <div class='form-group-sm'>
                                            <div class='row'>
                                                <div class='col-md-4'>
                                                    <label>Kelas</label>
                                                    <select name='id_kelas' class='form-control' 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>Level</label>
                                                    <select name='level' class='form-control' 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>Jurusan</label>
                                                    <select name='idpk' class='form-control' 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>
                                        <div class='form-group-sm'>
                                            <div class='row'>
                                                <div class='col-md-4'>
                                                    <label>Sesi</label>
                                                    <select name='idsesi' class='form-control' 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>Ruang</label>
                                                    <select name='ruang' class='form-control' 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>Server</label>
                                                    <select name='server' class='form-control' 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>
                                        <div class='row'>
                                            <div class='col-md-6'>
                                                <div class='form-group-sm'>
                                                    <label>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>Agama</label>
                                                    <input type='text' name='agama' class='form-control' required='true' />
                                                </div>
                                            </div>
                                        </div>
                                        <div class='form-group-sm'>
                                            <div class='row'>
                                                <div class='col-md-6'>
                                                    <label>Password</label>
                                                    <input type='password' name='pass1' class='form-control' required='true' />
                                                </div>
                                                <div class='col-md-6'>
                                                    <label>Ulang Password</label>
                                                    <input type='password' name='pass2' class='form-control' required='true' />
                                                </div>
                                            </div>
                                        </div>
                                        <div class='row'>
                                            <div class='col-md-6'>
                                                <div class='form-group-sm'>
                                                    <label>No hp</label>
                                                    <input type='text' name='hp' class='form-control' />
                                                </div>
												</div>
												<div class='col-md-6'>
                                                <div class='form-group-sm'>
                                                    <label>Jeis Kelamin</label>
                                                     <select name='jk' class='form-control' 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-info'><i class='fa fa-check'></i> 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='panel panel-default'>
               <div class="panel-heading">
                  
                    <div class='box-tools pull-right'>
                      
                    </div>
					 <h4 class='box-title'><i class="fas fa-user-friends fa-fw"></i> Peserta Ujian</h4>
                </div>
				
			  <div class="box-body">
                    <ul class="nav nav-tabs">
                        <li class="active"><a href="#tab_1" data-toggle="tab" aria-expanded="true" >Peserta</a></li>                       
                    </ul>					
					<div class="tab-content">
                        <div class="tab-pane active" id="tab_1" >	
                           <br>						
						<div class="panel panel-default">	
						<div class="box-body">
                       <form id="form-edit" method='post' class="form-horizontal">             
                        <div class='box-tools pull-right'>
                            <button type='submit' name='submit' class='btn btn-sm btn-info'><i class='fa fa-check'></i> Simpan</button>
                            <a href='?pg=<?= $pg ?>' class='btn btn-sm btn-danger' ><i class='fa fa-times'></i></a>
                        </div>
                    </div>
                    <div class='box-body'>
                        <input type='hidden' name='idu' value="<?= $siswa['id_siswa'] ?>" />
                        <div class='form-group-sm'>
                            <div class='row'>
                                <div class='col-md-3'>
                                    <label>NIS</label>
                                    <input type='text' name='nis' class='form-control' value="<?= $siswa['nis'] ?>" required='true' />
                                </div>
                                <div class='col-md-3'>
                                    <label>Nomor Peserta</label>
                                    <input type='text' name='no_peserta' class='form-control' value="<?= $siswa['no_peserta'] ?>" readonly />
                                </div>  
                        <div class='col-md-6'>
                            <label>Nama</label>
                            <input type='text' name='nama' class='form-control' value="<?= $siswa['nama'] ?>" required='true' />
                        </div>
						</div>
						</div>
                        <div class='form-group-sm'>
                            <div class='row'>
                                <div class='col-md-3'>
                                    <label>Kelas</label>
                                    <select name='id_kelas' class='form-control' 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>Level</label>
                                    <select name='level' class='form-control' 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>Jurusan</label>
                                    <select name='idpk' class='form-control' >
                                      
                                        <?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>Sesi</label>
                                    <select name='idsesi' class='form-control' 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>
								<div class='form-group-sm'>
                            <div class='row'>
                                <div class='col-md-3'>
                                    <label>Ruang</label>
                                    <select name='ruang' class='form-control' 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>Server</label>
                                    <select name='server' class='form-control' 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>Username</label>
                                    <input type='text' name='username' class='form-control' value="<?= $siswa['username'] ?>" readonly />
                                </div>
                           
                             <div class='col-md-3'>
                                    <label>Agama</label>
                                    <input type='text' name='agama' class='form-control' value="<?= $siswa['agama'] ?>" required='true' />
                                </div>
                            </div>
                        </div>
                        <div class='form-group-sm'>
                            <div class='row'>
                                <div class='col-md-6'>
                                    <label>Password</label>
                                    <input type='password' name='pass1' class='form-control' />
                                </div>
                                <div class='col-md-6'>
                                    <label>Ulang Password</label>
                                    <input type='password' name='pass2' class='form-control' />
                                </div>
                            </div>
                        </div>
						<div class='form-group-sm'>
                        <div class='row'>
                            <div class='col-md-6'>
                                    <label>No hp</label>
                                    <input type='text' name='hp' class='form-control' value="<?= $siswa['hp'] ?>" />
                                </div>
								<div class='col-md-6'>
                                    <label>Photo</label>
                                    <input type='file' name='file' class='form-control'  />
                                </div>
                            </div>
                        </div>
                        <label>
                            <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;
    });
 
    $('#example1').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>