<?php
defined('APK') or exit('Anda tidak dizinkan mengakses langsung script ini!');
?>

 <div class="modal fade" id="tambahjadwal" tabindex="-1" aria-labelledby="tambahjadwal" aria-hidden="true">
   <div class="modal-dialog">
       <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title" id="tambahjadwal">Tambah Jadwal</h5>
               <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                      <div class="modal-body">
                <form id="formtambahujian" method='post'>
                    <div class='form-group-sm'>
                        <label class="bold">Nama Bank Soal</label>
                        <select name='idmapel' class='form-control' required='true'>
                            <?php
                            if ($user['level'] == 'admin') {
                                $namamapelx = mysqli_query($koneksi, "SELECT * FROM banksoal where status='1'  order by nama ASC");
                            } else {
                                $namamapelx = mysqli_query($koneksi, "SELECT * FROM banksoal where status='1' and idguru='$id_user'  order by nama ASC");
                            }
                            while ($namamapel = mysqli_fetch_array($namamapelx)) {
                                $dataArray = unserialize($namamapel['kelas']);
                                echo "<option value='$namamapel[id_bank]'>$namamapel[kode] [$namamapel[nama] [$namamapel[groupsoal]] -";
                                foreach ($dataArray as $key => $value) {
                                    echo "$value ";
                                }
                                echo "</option>";
                            }
                            ?>
                        </select>
                    </div>
					<div class='form-group-sm'>
                        <label class="bold">Pembuat Soal</label>
                        <select name='idguru' class='form-control' required='true'>
						<?php if($user['level']=='admin'){ ?>
                            <option value=''>Pilih Pembuat Soal </option>
                            <?php
                            $nama = mysqli_query($koneksi, "SELECT * FROM users WHERE level='guru'");
                            while ($namaQ = mysqli_fetch_array($nama)) {
                                echo "<option value='$namaQ[id_user]'>$namaQ[nama] </option>";
                            }
                            ?>
                        </select>
						<?php }else{ ?>
						<?php
                            $nama = mysqli_query($koneksi, "SELECT * FROM users WHERE  id_user='$_SESSION[id_user]'");
                            while ($namaQ = mysqli_fetch_array($nama)) {
                                echo "<option value='$namaQ[id_user]'>$namaQ[nama] </option>";
                            }
                            ?>
                        </select>
						<?php } ?>
                    </div>
                    <div class='form-group-sm'>
                        <label class="bold">Nama Jenis Ujian</label>
                        <select name='kode_ujian' class='form-control' required='true'>
                            <option value=''>Pilih Jenis Ujian </option>
                            <?php
                            $namaujianx = mysqli_query($koneksi, "SELECT * FROM jenis where status='aktif' order by nama ASC");
                            while ($ujian = mysqli_fetch_array($namaujianx)) {
                                echo "<option value='$ujian[id_jenis]'>$ujian[id_jenis] - $ujian[nama] </option>";
                            }
                            ?>
                        </select>
                    </div>
                    <div class='form-group-sm'>
                        <div class='row'>
                            <div class='col-md-6'>
                                <label class="bold">Tanggal Mulai Ujian</label>
                                <input type='text' name='tgl_ujian' class='tgl form-control' autocomplete='off' required='true' />
                            </div>
                            <div class='col-md-6'>
                                <label class="bold">Tanggal Waktu Expired</label>
                                <input type='text' name='tgl_selesai' class='tgl form-control' autocomplete='off' required='true' />
                            </div>
                        </div>
                    </div>
                    <div class='row'>
                        <div class='col-md-6'>
                            <div class='form-group-sm'>
                                <label class="bold">Sesi</label>
                                <select name='sesi' class='form-control' required='true'>
                                    <?php
                                    $sesix = mysqli_query($koneksi, "SELECT * from sesi");
                                    while ($sesi = mysqli_fetch_array($sesix)) {
                                        echo "<option value='$sesi[kode_sesi]'>$sesi[kode_sesi]</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>
                        <div class='col-md-6'>
                            <div class='form-group-sm'>
                                <label class="bold">Lama ujian (menit)</label>
                                <input type='number' name='lama_ujian' class='form-control' required='true' />
                            </div>
                        </div>  
                            <div class='col-md-6'>
							<div class='form-group-sm'>
                            <label class="bold">Muncul Tombol Selesai(Menit)</label>
                            <input type='number' name='btn_selesai' class='form-control' required='true' />
                        </div>
						 </div>
						<div class='col-md-6'>
                            <div class='form-group-sm'>
                                <label class="bold">Dilarang Keluar Aplikasi</label>
								<select name='pelanggaran' class='form-control' required>
                                <option value=''>Pilih Pelanggaran</option>
							<option value='0'>Tidak Menggunakan</option>
							<option value='10'>Waktu 10 Detik </option>
							<option value='20'>Waktu 20 Detik </option>
							<option value='30'>Waktu 30 Detik </option>
							<option value='40'>Waktu 40 Detik </option>
							<option value='50'>Waktu 50 Detik </option>
							<option value='60'>Waktu 60 Detik </option>
							</select>
                            </div>
                        </div>  
						
                    </div>
                  
                    <div class='form-group-sm'>
                        <label></label><br>
                        <label>
                            <input type='checkbox' class='icheckbox_square-green' name='acak' value='1'  /> Acak Soal
                        </label>
                       
                        <label>
                            <input type='checkbox' class='icheckbox_square-green' name='token' value='1'  /> Token Soal
                        </label>

                        <label>
                            <input type='checkbox' class='icheckbox_square-green' name='hasil' value='1' /> Hasil Tampil
                        </label>
                        <label>
                            <input type='checkbox' class='icheckbox_square-green' name='reset' value='1'  /> Reset Login
                        </label>
                    </div>
                    <div class='modal-footer'>
                        <button name='tambahjadwal' class='btn btn-outline-primary'><i class='fa fa-check-circle'></i> Simpan</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<div>
 <div class="row">
     <div class="col-md-12">
		<div class="card">
             <div class="card-header">	
			  <?php if($menu['aplikasi']==0){ ?>
                 <div class="example-content right"> 				  
                 	 <button class='btn btn-outline-primary' data-bs-toggle="modal" data-bs-target="#tambahjadwal"><i class="fa fa-plus-circle" data-bs-toggle="tooltip" data-bs-placement="top" title="Tambah Jadwal"></i>Jadwal</button>
				    <a class='btn btn-outline-danger' href='?'><i class="fa fa-times-circle"></i>Back</a>
				  </div>
                    <?php } ?>				  
					<h5 class='card-title'> Jadwal Ujian</h5>
				</div>
			 
				<p>
            <div class='card-body'>			
                <div class="col-md-1"></div>
				<div class="row">
                <div class="col-md-6">
                    <form id='formaktivasi' action="">
                        <div class="form-group">
                            <label class="bold">Pilih Jadwal Ujian</label>
                            <select class="form-select select2" name="ujian[]" style="width:100%" multiple='true' required>
                                 <?php if ($user['level'] == 'admin') {
                                    $jadwal = mysqli_query($koneksi, "SELECT * FROM ujian  ORDER BY tgl_ujian ASC, waktu_ujian ASC");
                                } else {
                                    $jadwal = mysqli_query($koneksi, "SELECT * FROM ujian where id_guru='$id_user'  ORDER BY tgl_ujian ASC, waktu_ujian ASC");
                                } ?>
                                <?php while ($ujian = mysqli_fetch_array($jadwal)) : ?>

                                    <option value="<?= $ujian['id_ujian'] ?>"><?= $ujian['kode_nama'] . " - " . $ujian['nama'] ?></option>
                                <?php endwhile; ?>
                            </select>
                        </div>
                        <div class="form-group">
                            <div class="row">
                                <div class="col-md-6">
                                    <label class="bold">Pilih Kelompok Test / Sesi</label>
                                    <select class="form-select" name="sesi" id="">
                                        <?php $sesi = mysqli_query($koneksi, "select * from siswa group by sesi"); ?>
                                        <?php while ($ses = mysqli_fetch_array($sesi)) : ?>
                                            <option value="<?= $ses['sesi'] ?>"><?= $ses['sesi'] ?></option>
                                        <?php endwhile; ?>
                                    </select>
                                </div>
                                <div class="col-md-6">
                                    <label class="bold">Action</label>
                                    <select class="form-select" name="aksi" required>

                                        <option value="">Pilih Action</option>
                                        <option value="1">Aktif</option>
                                        <option value="0">Non aktif</option>
                                        <option value="hapus">Hapus</option>
                                    </select>
                                </div>
                            </div>
						</div>
						<br>
                        <button name="simpan" class="btn btn-sm btn-outline-primary right">Simpan </button>
                    </form>
               </div>
			    <div class="col-xl-5">
                                <div class="card widget widget-stats">
                                    <div class="card-body">
                                        <div class="widget-stats-container d-flex">
                                            <div class="widget-stats-icon widget-stats-icon-primary">
                                                <i class="fa fa-barcode"></i>
                                            </div>
                                            <div class="widget-stats-content flex-fill">
                                                <span class="widget-stats-title">Token Ujian</span>
												 <?php $token = mysqli_fetch_array(mysqli_query($koneksi, "select token from token")) ?>
                                                <span class="widget-stats-amount"  name='token' id='isi_token'><?= $token['token'] ?></span>
                                                <span class="widget-stats-info"> <a id="btntoken" href="#"><i class='fa fa-spin fa-refresh'></i> Refreh Now</a></span>
                                            </div>
                                            <div class="widget-stats-indicator widget-stats-indicator-negative align-self-start">
                                                <i class="fa fa-chevron-down"></i> 15 Menit
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                    </div>
			
					 </div>	 </div>	</div>	</div>	</div>	
               
<div class="row">
  <div class="col-md-12">
     <div class="card">
         <div class="card-header">
            <h5 class="card-title">Status Ujian</h5>
                   </div>
	<div class="card-body">			   
    <div id='tablereset' class='table-responsive'>
        <table class='table' id="datatable1">
            <thead>
                <tr>

                    <th width='5px'>#</th>
                    <th>Bank Soal</th>
                    <th>Level</th>
                    <th>Durasi</th>
                    <th>Waktu Ujian</th>
                    <th>Keterangan</th>
                    <th>Jadwal</th>
                    <th>Status</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                <?php

                if ($user['level'] == 'admin') {
                    $mapelQ = mysqli_query($koneksi, "SELECT * FROM ujian  ORDER BY tgl_ujian ASC, waktu_ujian ASC");
                } else {
                    $mapelQ = mysqli_query($koneksi, "SELECT * FROM ujian where id_guru='$id_user'  ORDER BY tgl_ujian ASC, waktu_ujian ASC");
                }
                ?>
                <?php while ($mapel = mysqli_fetch_array($mapelQ)) : ?>
                    <?php if ($mapel['tgl_ujian'] > date('Y-m-d H:i:s') and $mapel['tgl_selesai'] > date('Y-m-d H:i:s')) {
                        $color = "bg-gray";
                        $status = "BELUM MULAI";
                    } elseif ($mapel['tgl_ujian'] < date('Y-m-d H:i:s') and $mapel['tgl_selesai'] > date('Y-m-d H:i:s')) {
                        $color = "bg-blue";
                        $status = "<i class='fa fa-spinner fa-spin'></i> MULAI UJIAN";
                    } else {
                        $color = "bg-red";
                        $status = "<i class='fa fa-times'></i> WAKTU HABIS";
                    } ?>
                    <?php
                    $tgl = explode(" ", $mapel['tgl_ujian']);
                    $tgl = $tgl[0];
                    $no++;
                    ?>

                    <tr>
                        <td><?= $no ?></td>
                        <td>
                            <?php
                            if ($mapel['id_pk'] == '0') {
                                $jur = 'Semua';
                            } else {
                                $jur = $mapel['id_pk'];
                            }
                            ?>

                            <?= $mapel['kode_nama'] ?>
                        </td>
                        <td>
                            <?= $mapel['kode_ujian'] ?> &nbsp;
                            <?= $mapel['level'] ?> &nbsp;
                            
                            <?php
                            $dataArray = unserialize($mapel['id_pk']);
                            foreach ($dataArray as $key => $value) :
                                echo $value . " ";
                            endforeach;
                            ?>
                            <br>
                            <?php
                            $dataArray = unserialize($mapel['kelas']);
                            foreach ($dataArray as $key => $value) :
                                echo $value . " ";
                            endforeach;
                            ?>
                        </td>
                        <td>
                            <small class='label label-warning'>
                                <?= ($mapel['tampil_pg']+$mapel['tampil_esai']+$mapel['tampil_multi']+$mapel['tampil_bs']+$mapel['tampil_urut']) ?> Soal / <?= $mapel['lama_ujian'] ?> m </small>
                        </td>
                        <td>
                            <small> <?= $mapel['tgl_ujian'] ?></small><br>
                            <small><?= $mapel['tgl_selesai'] ?></small>
                        </td>

                        <td>
                            <?php
                            if ($mapel['token'] == 1) {
                               echo "<label class='checkbox right'><input class='hidden radio-label' type='checkbox' checked /><span class='check'></span> Token</label> ";
                            } elseif ($mapel['token'] == 0) {
                                echo "<label class='checkbox right'><input class='hidden radio-label' type='checkbox' disabled /><span class='check'></span> Token</label> ";
                            }
                           
                           if ($mapel['hasil'] == 1) {
                                 echo "<label class='checkbox right'><input class='hidden radio-label' type='checkbox' checked /><span class='check'></span> Hasil</label> ";
                            } elseif ($mapel['hasil'] == 0) {
                                 echo "<label class='checkbox right'><input class='hidden radio-label' type='checkbox' disabled /><span class='check'></span> Hasil</label> ";
                            }
                           
                            if ($mapel['reset'] == 1) {
                                 echo "<label class='checkbox right'><input class='hidden radio-label' type='checkbox' checked /><span class='check'></span> Reset</label> ";
                            } elseif ($mapel['reset'] == 0) {
                                 echo "<label class='checkbox right'><input class='hidden radio-label' type='checkbox' disabled /><span class='check'></span> Reset</label> ";
                            }
							 if ($mapel['acak'] == 1) {
                                echo "<label class='checkbox right'><input class='hidden radio-label' type='checkbox' checked /><span class='check'></span> Acak</label> ";
                            } elseif ($mapel['acak'] == 0) {
                                echo "<label class='checkbox right'><input class='hidden radio-label' type='checkbox' disabled /><span class='check'></span> Acak</label> ";
                            }
                            ?>
                        </td>
                        <td style="text-align:center">
                            <?php
                            if ($mapel['status'] == 1) {
                                echo "<span class='badge badge-success m-l-xxs'>Aktif Sesi $mapel[sesi]</span>";
                            } elseif ($mapel['status'] == 0) {
                                echo "<label class='badge bg-red'>Tidak Aktif</label>";
                            }
                            ?>
							
                        </td>
                        <td style="text-align:center">
                            <?=
                                $status
                            ?> <br>
                            <i class="fa fa-circle text-green"></i>
                            <?=
                                $useronline = mysqli_num_rows(mysqli_query($koneksi, "select * from nilai where id_bank='$mapel[id_bank]' and id_ujian='$mapel[id_ujian]' and ujian_selesai is null"));
                            ?>
                            <i class="fa fa-circle text-danger"></i>
                            <?=
                                $userend = mysqli_num_rows(mysqli_query($koneksi, "select * from nilai where id_bank='$mapel[id_bank]' and id_ujian='$mapel[id_ujian]' and ujian_selesai <> ''"));
                            ?>
                        </td>
                        <td>
                            
                                 <?php if($menu['aplikasi']==0){ ?>
							   <a class='btn btn-sm btn-outline-info' data-id="<?= $mapel['id_ujian'] ?>" data-bs-toggle='modal' data-bs-target="#edit<?= $mapel['id_ujian'] ?>"><i class='fa fa-edit'></i></a>
								 <?php } ?>
						   <a href="?pg=status&id=<?= $mapel['id_ujian'] ?>" class='btn btn-sm btn-outline-danger ' data-bs-toggle="tooltip" data-bs-placement="top" title="Status Peserta"><i class='fa fa-users'></i></a>
                            
                        </td>
                    </tr>
                 
                    <div class='modal fade' id='edit<?= $mapel['id_ujian'] ?>' style='display: none;'>
                        <div class='modal-dialog'>
                            <div class='modal-content'>
                                <div class='modal-header bg-aqua'>
                                    <h5 class='modal-title'>Edit Waktu Ujian</h5>

                                </div>
                                <form id="formedit<?= $mapel['id_ujian'] ?>">
                                    <div class='modal-body'>
                                        <input type='hidden' name='idm' value="<?= $mapel['id_ujian'] ?>" />
                                        <div class="form-group-sm">
                                            <label for="mulaiujian">Waktu Mulai Ujian</label>
                                            <input type="text" class="tgl form-control" name="tgl_ujian" value="<?= $mapel['tgl_ujian'] ?>" aria-describedby="helpId" placeholder="">
                                            <small id="helpId" class="form-text text-muted">Tanggal dan waktu ujian dibuka</small>
                                        </div>
                                        <div class="form-group-sm">
                                            <label for="selesaiujian">Waktu Ujian Ditutup</label>
                                            <input type="text" class="tgl form-control" name="tgl_selesai" value="<?= $mapel['tgl_selesai'] ?>" aria-describedby="helpId" placeholder="">
                                            <small id="helpId" class="form-text text-muted">Tanggal dan waktu ujian ditutup</small>
                                        </div>
                                        <div class='form-group-sm'>
                                            <div class='row'>
                                                <div class='col-md-6'>
                                                    <label>Lama Ujian</label>
                                                    <input type='number' name='lama_ujian' value="<?= $mapel['lama_ujian'] ?>" class='form-control' required='true' />
                                                </div>
                                                <div class='col-md-6'>
                                                    <label>Sesi</label>
                                                    <input type='number' name='sesi' value="<?= $mapel['sesi'] ?>" class='form-control' required='true' />
                                                </div>
                                            </div>
                                        </div><p>
                                        <div class='form-group-sm'>
                                            <label>
                                                <input type='checkbox' class='icheckbox_square-green' name='acak' value='1' <?php if ($mapel['acak'] == 1) {
                                                                                                                                echo "checked='true'";
                                                                                                                            } ?>  /> Acak Soal
                                            </label>
                                            <label>
											<?php if($menu['soal']==1){ ?>
                                                <input type='checkbox' class='icheckbox_square-green' name='acakopsi' value='1' <?php if ($mapel['ulang'] == 1) {
                                                                                                                                    echo "checked='true'";
                                                                                                                                } ?> disabled /> Acak Opsi
											<?php }else{ ?>
											<input type='checkbox' class='icheckbox_square-green' name='acakopsi' value='1' <?php if ($mapel['ulang'] == 1) {
                                                                                                                                    echo "checked='true'";
                                                                                                                                } ?>  /> Acak Opsi
											<?php } ?>																					          
                                            </label>
                                            <label>
                                                <input type='checkbox' class='icheckbox_square-green' name='token' value='1' <?php if ($mapel['token'] == 1) {
                                                                                                                                    echo "checked='true'";
                                                                                                                                } ?>  /> Token Soal
                                            </label>
                                            <label>
                                                <input type='checkbox' class='icheckbox_square-green' name='hasil' value='1' <?php if ($mapel['hasil'] == 1) {
                                                                                                                                    echo "checked='true'";
                                                                                                                                } ?> /> Hasil Tampil
                                            </label>
                                            <label>
                                                <input type='checkbox' class='icheckbox_square-green' name='reset' value='1' <?php if ($mapel['reset'] == 1) {
                                                                                                                                    echo "checked='true'";
                                                                                                                                } ?>  /> Reset Login
                                            </label>
                                        </div>
                                    </div>
                                    <div class='modal-footer'>

                                        <center>
                                            <button type="submit" class='btn btn-info' name='simpan'><i class='fa fa-save'></i> Ganti Waktu Ujian</button>
                                        </center>

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

                    <script>
                        $("#formedit<?= $mapel['id_ujian'] ?>").submit(function(e) {
                            e.preventDefault();
                            $.ajax({
                                type: 'POST',
                                url: 'sandik_ujian/crud_jadwal.php?pg=ubah',
                                data: $(this).serialize(),
                                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;
                        });
                    </script>
                <?php endwhile ?>
            </tbody>
        </table>
    </div>
</div>

    <script>
        $(document).ready(function() {
            $("#btntoken").click(function() {
                $.ajax({
                    url: "sandik_ujian/crud_jadwal.php?pg=token",
                    type: "POST",
                    success: function(respon) {
                        $('#isi_token').html(respon);
                        iziToast.success({
                            title: 'Sukses',
                            message: 'Token diperbarui',
                            position: 'topRight'
                        });
                    }
                });
                return false;
            })
            $('#formaktivasi').submit(function(e) {
                e.preventDefault();
                $.ajax({
                    type: 'POST',
                    url: 'sandik_ujian/crud_jadwal.php?pg=aktivasi',
                    data: $(this).serialize(),
                    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;
            });

        });
    </script>
    <script>
         $('#formtambahujian').submit(function(e) {
            e.preventDefault();
            $.ajax({
                type: 'POST',
                url: 'sandik_ujian/crud_jadwal.php?pg=tambah',
                data: $(this).serialize(),

                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 {
                        Swal.fire({	
				title: '<a href="#" class="sandik" style="color:red">Gagal !! Bank Soal Sudah ada</a>',				
				showConfirmButton: false,
				 animation: false,
				  customClass: 'animated tada',				  
				  imageUrl: '../dist/img/sandik_kecil.gif',
                 footer: '<a href="#"><b style="color:red">Sistem Administrasi Pendidik (SANDIK)</b></a>'
                    });
					setTimeout(function() {
                        window.location.reload();
                    }, 2000);
                }

            }
        });
        return false;
    });
    </script>