<link href="<?= $homeurl ?>/font/material.css" rel="stylesheet">
 
    <link href="<?= $homeurl ?>/assets/plugins/pace/pace.css" rel="stylesheet">
    <link href="<?= $homeurl ?>/assets/plugins/highlight/styles/github-gist.css" rel="stylesheet">
	  <link href="<?= $homeurl ?>/assets/css/main.css" rel="stylesheet">
<?php
include "../../config/koneksi.php";
include "../../config/function.php";

if (empty($_GET['idu'])) {
    $queryidu = "";
} else {
    $queryidu = "and s.id_ujian='" . $_GET['idu'] . "'";
}

$tglsekarang = date('Y-m-d');
$nilaiq = mysqli_query($koneksi, "SELECT *  FROM nilai  s LEFT JOIN ujian c ON s.id_ujian=c.id_ujian  where c.status='1' and s.id_siswa<>'' " . $queryidu . " GROUP by s.id_nilai DESC");
 ?>
<div class='table-responsive'>
    <table id='datatable1' class='table table-bordered' width="100%">
        <thead>
            <tr>
                <th width='5px'>#</th>
                <th>Kelas - Nama Siswa</th>
               
                <th>Jumlah</th>
                <th>Status</th>
                <th>Reset</th>
            </tr>
        </thead>
        <tbody id='logstatus'>
            <?php while ($nilai = mysqli_fetch_array($nilaiq)) {
                $tglx = strtotime($nilai['ujian_mulai']);
                $tgl = date('Y-m-d', $tglx);

                $no++;
                $ket = '';
                $lama = $jawaban = $skor = '--';
                $siswa = mysqli_fetch_array(mysqli_query($koneksi, "SELECT * FROM siswa WHERE id_siswa='$nilai[id_siswa]' "));
                
                $bank = mysqli_fetch_array(mysqli_query($koneksi, "SELECT * FROM banksoal WHERE id_bank='$nilai[id_bank]'"));
                $nilaiQ = mysqli_query($koneksi, "SELECT * FROM nilai WHERE id_siswa='$siswa[id_siswa]'");
                $nilaiC = mysqli_num_rows($nilaiQ);
                 $totalsoal = mysqli_num_rows(mysqli_query($koneksi, "SELECT * FROM soal WHERE id_bank='$nilai[id_bank]'"));
                 $brows = mysqli_num_rows(mysqli_query($koneksi, "SELECT * FROM reset WHERE idsiswa='$nilai[id_siswa]' AND idnilai='$nilai[id_nilai]'"));
                if ($nilaiC <> 0) {
                    $lama = '';
                    if ($nilai['ujian_mulai'] <> '' and $nilai['ujian_selesai'] <> '' and $nilai['ujian_berlangsung'] <> '' and $nilai['browser']=='1') {
                        $selisih = strtotime($nilai['ujian_selesai']) - strtotime($nilai['ujian_mulai']);
                        $ket = "<label class='label label-success'>Ujian Selesai</label>";
                        $btn = "<button class='btn btn-sm btn-secondary' disabled><i class='material-icons'>lock</i></button>";
					     $btn2 = "<button  class='btn btn-sm btn-light'><i class='material-icons'>lock</i></button>";
					    $totaljawaban = $nilai['jumjawab'];
					} elseif ($nilai['ujian_mulai'] <> '' and $nilai['ujian_selesai'] <> '' and $nilai['ujian_berlangsung'] <> '' and $nilai['browser']=='0') {
						   $totaljawaban = $nilai['jumjawab'];
						$ket = "<label class='label label-success'>Selesai - tidak terjawab semua</label>";
                        $btn = "<button data-id='$nilai[id_nilai]' class='reset btn btn-sm btn-primary' ><i class='material-icons'>delete</i>Reset</button>";                  
					    $minta = "<h5><span data-id='$nilai[id_nilai]' class='badge badge-danger'>Minta Reset</span></h5>";
					    $btn2 = "<button data-idp='$nilai[id_nilai]' class='selesai btn btn-sm btn-danger'><i class='material-icons'>edit</i>Selesai Paksa</button>";
					
					} elseif ($nilai['ujian_mulai'] <> '' and $nilai['ujian_selesai'] == '' and $nilai['ujian_berlangsung'] <> '' and $nilai['browser']=='0') {
                        $selisih = strtotime($nilai['ujian_berlangsung']) - strtotime($nilai['ujian_mulai']);
                          $totaljawaban = mysqli_num_rows(mysqli_query($koneksi, "SELECT * FROM jawaban WHERE id_bank='$nilai[id_bank]' AND id_siswa='$nilai[id_siswa]'"));
                        $ket = "<h5><span class='badge badge-success'>Sedang Dikerjakan</span></h5>";
                        $btn = "<button  class='btn btn-sm btn-light'><i class='material-icons'>lock</i></button>";
                    	$btn2 = "<button  class='btn btn-sm btn-light'><i class='material-icons'>lock</i></button>";
					
					} else{
						$ket = "<h5><span class='badge badge-danger'>Error</span></h5>";
                         $btn = "<button data-id='$nilai[id_nilai]' class='kosongkan btn btn-sm btn-danger' ><i class='material-icons'>delete</i></button>";                  
							$btn2 = "<button  class='btn btn-sm btn-light'><i class='material-icons'>lock</i></button>";
					}
                }
            ?>
                <tr>
                    <td><?= $no ?></td>
					<td>
                    <?= $siswa['kelas'] ?> | 
                    <small><?= $siswa['nama'] ?></small>
                   
					</td>
                    
                    <td><h5><span class="badge badge-primary">Soal <?= $totalsoal ?></span> <span class="badge badge-dark">Jawab <?= $totaljawaban ?></span></h5></td>
                    <td><?= $ket ?> 
					<?php if($brows >=1): ?>
					<?= $minta ?>
					<?php endif; ?>
					</td>
					
                    <td>
					<?= $btn ?>
					<?= $btn2 ?>
					</td>

                </tr>

            <?php
            }
            ?>
        </tbody>
    </table>
</div>
 
    <script src="<?= $homeurl ?>/assets/plugins/pace/pace.min.js"></script>
    <script src="<?= $homeurl ?>/assets/plugins/highlight/highlight.pack.js"></script>
    <script src="<?= $homeurl ?>/assets/js/main.js"></script>
  
     <script>
   
           $('#datatable1').on('click', '.reset', function() {
			var id = $(this).data('id');
			console.log(id);
				Swal.fire({
					title: 'Yakin Reset Data Ujian ?',
					text: "Informasi : Jangan khawatir Data Jawaban tidak terhapus saat di reset",
					type: 'warning',
					showCancelButton: true,
					confirmButtonColor: '#3085d6',
					cancelButtonColor: '#d33',
					confirmButtonText: 'Ya, Reset!',
					cancelButtonText: "Batal"				  
					}).then((result) => {
					if (result.value) {
						$.ajax({
						url: 'asesmen/tsiswa.php?pg=hapus',
						method: "POST",
						data: 'id=' + id,
						success: function(data) {
						Swal.fire(
						'Deleted!',
						'Peserta berhasil direset.',
						'success'
						) 
						setTimeout(function() {
						window.location.reload();
								}, 1000);
							}
						});
					}
					return false;
					})
			});
     $('#datatable1').on('click', '.kosongkan', function() {
			var id = $(this).data('id');
			console.log(id);
				Swal.fire({
					title: 'Yakin Reset Data Ujian Error?',
					text: "Informasi : Data Jawaban akan ikut dihapus",
					type: 'warning',
					showCancelButton: true,
					confirmButtonColor: '#3085d6',
					cancelButtonColor: '#d33',
					confirmButtonText: 'Ya, Reset!',
					cancelButtonText: "Batal"				  
					}).then((result) => {
					if (result.value) {
						$.ajax({
						url: 'asesmen/tsiswa.php?pg=reset',
						method: "POST",
						data: 'id=' + id,
						success: function(data) {
						Swal.fire(
						'Deleted!',
						'Data berhasil direset.',
						'success'
						) 
						setTimeout(function() {
						window.location.reload();
								}, 1000);
							}
						});
					}
					return false;
					})
			});
			$('#datatable1').on('click', '.selesai', function() {
			var idp = $(this).data('idp');
			console.log(idp);
				Swal.fire({
					title: 'Yakin Akan Selesai Paksa ?',
					text: "Informasi : Jka dipaksa selesai maka siswa sudah tidak dapat lanjut Ujian pada mapel ini",
					type: 'warning',
					showCancelButton: true,
					confirmButtonColor: '#3085d6',
					cancelButtonColor: '#d33',
					confirmButtonText: 'Ya, Selesai !',
					cancelButtonText: "Batal"				  
					}).then((result) => {
					if (result.value) {
						$.ajax({
						url: 'asesmen/tsiswa.php?pg=selesai',
						method: "POST",
						data: 'idp=' + idp,
						success: function(data) {
						Swal.fire(
						'Deleted!',
						'Ujian telah diselesaikan.',
						'success'
						) 
						setTimeout(function() {
						window.location.reload();
								}, 1000);
							}
						});
					}
					return false;
					})
			});
</script>