<?php if ($ac == '') : ?>
    <div class='row'>
        <?php if (isset($_GET['id'])) { ?>
            <?php $qujian = mysqli_fetch_array(mysqli_query($koneksi, "SELECT * FROM ujian where id_ujian='$_GET[id]'")) ?>
            <?php $ikut = mysqli_num_rows(mysqli_query($koneksi, "SELECT * FROM nilai where id_ujian='$_GET[id]'")) ?>
            <?php
            $kelas = implode("','", unserialize($qujian['kelas']));
            if ($kelas == "semua") {
                if ($qujian['level'] == 'semua') {
                    $peserta = mysqli_num_rows(mysqli_query($koneksi, "SELECT * FROM siswa "));
                } else {
                    $peserta = mysqli_num_rows(mysqli_query($koneksi, "SELECT * FROM siswa where level='$qujian[level]'"));
                }
            } else {
                $peserta = mysqli_num_rows(mysqli_query($koneksi, "SELECT * FROM siswa where id_kelas IN ('" . $kelas . "')"));
            }
            ?>
             <div class="col-xl-8">
            <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-edit"></i>
                  </div>
				  <div class="widget-stats-content flex-fill">
                      <span class="widget-stats-title"><?= $qujian['tgl_ujian'] ?> S/d <?= $qujian['tgl_selesai'] ?></span>
                       <span class="widget-stats-amount"> <?= $qujian['nama'] ?></span>
                   </div>
                                    </div>
                                </div>
                              </div>
							  </div>
            <?php if ($qujian['tgl_ujian'] > date('Y-m-d H:i:s') and $qujian['tgl_selesai'] > date('Y-m-d H:i:s')) {
                $color = "bg-gray";
                $status = "BELUM MULAI";
            } elseif ($qujian['tgl_ujian'] < date('Y-m-d H:i:s') and $qujian['tgl_selesai'] > date('Y-m-d H:i:s')) {
                $color = "bg-blue";
                $status = "<i class='fa fa-spinner fa-spin' style='color:red;'></i><b style='color:red;'> Sedang Ujian</b>";
            } else {
                $color = "bg-red";
                $status = "WAKTU HABIS";
            } ?>
            <div class="col-xl-4">
            <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-wifi"></i>
                  </div>
                      <div class="widget-stats-content flex-fill">
                        <span class="widget-stats-title"><?= $status ?></span>
		                <span class="widget-stats-amount"><?= $qujian['lama_ujian'] ?> menit</span>
                    </div>
                    
                                        </div>
                                    </div>
                                </div>
                              </div>
                           <div class="col-xl-3">
                                <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-light">
                                                <i class="fa fa-users-cog"></i>
                                            </div>
                                            <div class="widget-stats-content flex-fill">
                                                <span class="widget-stats-title">SEDANG UJIAN</span>
                                                <span class="widget-stats-amount"><?= $ikut ?></span>
                                                
                                            </div>
                                            <div class="widget-stats-indicator widget-stats-indicator-positive align-self-start">
                                                <i class="material-icons"></i> SISWA
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                          <div class="col-xl-3">
                                <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-danger">
                                                <i class="fa fa-users"></i>
                                            </div>
                                            <div class="widget-stats-content flex-fill">
                                                <span class="widget-stats-title">BELUM UJIAN</span>
                                                <span class="widget-stats-amount"><?= $peserta - $ikut ?></span>
                                                
                                            </div>
                                            <div class="widget-stats-indicator widget-stats-indicator-positive align-self-start">
                                                <i class="material-icons"></i> SISWA
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                             <div class="col-xl-3">
                                <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-dark">
											<a role="button" onclick="frames['printabsen'].print()">
                                                <i class="fa fa-print"></i>
												</a>
                                            </div>
                                            <div class="widget-stats-content flex-fill">
                                                <span class="widget-stats-title">CETAK ABSEN</span>
                                                <span class="widget-stats-amount"><?= $peserta ?></span>
                                                
                                            </div>
                                            <div class="widget-stats-indicator widget-stats-indicator-positive align-self-start">
                                                <i class="material-icons"></i> SISWA
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
            

                <iframe name='printabsen' src='status/print_absen.php?id=<?= enkripsi($_GET['id']) ?>' style='display:none'></iframe>
                
          
			
			<?php if($menu['soal']==0){ ?>
			 <div class="col-xl-3">
                                <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-warning">
											 <a href="status/report_excel.php?m=<?= $_GET['id'] ?>">
                                                <i class="fa fa-print"></i>
												</a>
                                            </div>
                                            <div class="widget-stats-content flex-fill">
                                                <span class="widget-stats-title">CETAK NILAI</span>
                                                <span class="widget-stats-amount"><?= $peserta ?></span>
                                                
                                            </div>
                                            <div class="widget-stats-indicator widget-stats-indicator-positive align-self-start">
                                                <i class="material-icons"></i> SISWA
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>		
                        <?php } ?>
		 <?php } ?>
        <div class='col-md-12'>
		<div class="card">
            <div class="card-header">
              <h5 class='card-title'> Status Peserta </h5>
                    <div class='right '>
                        <button type="button" class="btn btn-outline-danger" id="btnselesai">STOP ALL</button>
                     <a class='btn btn-outline-danger' href='?'><i class="fa fa-times-circle"></i>Back</a>
                    </div>
                </div>
                <div class='card-body'>
                    
                    <div id='divstatus'>
                        <?php
                        if (empty($_GET['id'])) {
                            $queryidu = "";
                        } else {
                            $queryidu = "and s.id_ujian='" . $_GET['id'] . "'";
                        }
                        $user = mysqli_fetch_array(mysqli_query($koneksi, "SELECT * FROM users WHERE id_user='$_SESSION[id_user]'"));
                        $tglsekarang = date('Y-m-d');
                        if ($user['level'] == 'admin') {
                            $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");
                        } elseif ($user['level'] == 'pengawas') {
                            $nilaiq = mysqli_query($koneksi, "SELECT * FROM nilai s LEFT JOIN ujian c ON s.id_ujian=c.id_ujian JOIN siswa b ON b.id_siswa=s.id_siswa where c.status='1' and s.id_siswa<>'' and b.ruang='$user[ruang]' " . $queryidu . " GROUP by s.id_nilai DESC");
                        } else {
                            $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<>'' and c.id_guru='$_SESSION[id_user]' " . $queryidu . " GROUP by s.id_nilai DESC");
                        } ?>
                        <div class='table-responsive'>
                            <table id='datatable1' class='table table-hover'>
                                <thead>
                                    <tr>
                                        <th width='5px'>#</th>
                                        <th>NIS</th>
                                        <th>Nama</th>
                                        <th>Kelas</th>
                                        <th>Mapel</th>
                                        <th>Lama Ujian</th>
                                        <th>Skor</th>
                                        <th>Nilai</th>
                                        <th>Ip Address</th>
                                        <th>Status</th>
                                        <th>Aksi</th>
                                    </tr>
                                </thead>
                                <tbody id='divstatus'>
                                    <?php while ($nilai = mysqli_fetch_array($nilaiq)) {
                                        $tglx = strtotime($nilai['ujian_mulai']);
                                        $tgl = date('Y-m-d', $tglx);
                                        if ($tgl == $tglsekarang) {
                                            $no++;
                                            $ket = '';
                                            $lama = $jawaban = $skor = '--';
                                            $siswa = mysqli_fetch_array(mysqli_query($koneksi, "SELECT * FROM siswa WHERE id_siswa='$nilai[id_siswa]' "));
                                            $kelas = mysqli_fetch_array(mysqli_query($koneksi, "SELECT * FROM kelas WHERE id_kelas='$siswa[id_kelas]'"));
                                            $mapel = 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);

                                            if ($nilaiC <> 0) {
                                                $lama = '';
                                                if ($nilai['ujian_mulai'] <> '' and $nilai['ujian_selesai'] <> '') {
                                                    $selisih = strtotime($nilai['ujian_selesai']) - strtotime($nilai['ujian_mulai']);

                                                    $jawaban = "<span class='badge badge-primary'>$nilai[jml_benar] <i class='fa fa-check'></i></span>  <small class='label bg-red'>$nilai[jml_salah] <i class='fa fa-times'></i></span>";
                                                   
													$esai = "<small class='badge badge-primary'>" .number_format($nilai['skor_esai'], 2, '.', ''). "</small>";
                                             $bs ="<small class='badge badge-primary'>" .number_format($nilai['skor_bs'], 2, '.', ''). "</small>";
											 $multi ="<small class='badge badge-primary'>" .number_format($nilai['skor_multi'], 2, '.', ''). "</small>";
											 $urut ="<small class='badge badge-primary'>" .number_format($nilai['skor_urut'], 2, '.', ''). "</small>";
                                            $skor = "<small class='badge badge-primary'>" .number_format($nilai['skor'], 2, '.', ''). "</small>";
                                            $total ="<span class='badge badge-primary'>" .number_format($nilai['total'], 2, '.', '') . "</span>";
													
													
                                                    $ket = "<label class='badge badge-success'>Tes Selesai</label>";
                                                    $btn = "<button data-id='$nilai[id_nilai]' class='ulang btn btn-sm btn-outline-warning'><i class='fa fa-history'></i></button>";
                                                } elseif ($nilai['ujian_mulai'] <> '' and $nilai['ujian_selesai'] == '') {
                                                    $selisih = strtotime($nilai['ujian_berlangsung']) - strtotime($nilai['ujian_mulai']);

                                                    $ket = "<label class='badge badge-danger'><i class='fa fa-spin fa-spinner' title='Sedang ujian'></i>&nbsp;Dikerjakan</label>";

                                                    $btn = "<button data-id='$nilai[id_nilai]' class='hapus btn btn-sm btn-outline-danger'>selesai</button>";
                                                }
                                            }
                                    ?>
                                            <tr>
                                                <td><?= $no ?></td>
                                                <td><?= $siswa['nis'] ?></td>
                                                <td><?= $siswa['nama'] ?></td>
                                                <td><?= $kelas['nama'] ?></td>
                                                <td><small class='label bg-red'><?= $nilai['kode_ujian'] ?></small> <small class='label bg-purple'><?= $mapel['nama'] ?></small> <small class='label bg-blue'><?= $mapel['level'] ?></small></td>
                                                <td><?= lamaujian($selisih) ?></td>
										<td><?= $skor ?>  <?= $multi ?>  <?= $bs ?>  <?= $urut ?>  <?= $esai ?></td>
                                                <td><?= $total ?></td>
                                                <td><?= $nilai['ipaddress'] ?></td>
                                                <td><?= $ket ?></td>
                                                <td><?= $btn ?></td>

                                            </tr>

                                    <?php }
                                    }
                                    ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
<?php endif ?>
<script>
    var autoRefresh = setInterval(
        function() {
            <?php if (isset($_GET['id'])) { ?>
                $('#divstatus').load("status/statusall_ujian.php?idu=<?= $_GET['id'] ?>");
            <?php } else { ?>
                $('#divstatus').load("status/statusall.php");
            <?php } ?>
        }, 3000
    );

    function fullScreen(element) {
        if (element.requestFullScreen) {
            element.requestFullScreen();
        } else if (element.webkitRequestFullScreen) {
            element.webkitRequestFullScreen();
        } else if (element.mozRequestFullScreen) {
            element.mozRequestFullScreen();
        }
    }
    var elem = document.documentElement;
    /* Close fullscreen */
    function closeFullscreen() {
        if (document.exitFullscreen) {
            document.exitFullscreen();
        } else if (document.webkitExitFullscreen) {
            /* Safari */
            document.webkitExitFullscreen();
        } else if (document.msExitFullscreen) {
            /* IE11 */
            document.msExitFullscreen();
        }
    }

    $("#closefull").hide();
    $("#btnfull").click(function() {
        var element = document.getElementById('statusfull');
        fullScreen(element)
        $("#closefull").show();
        $(this).hide();
    });
    $("#closefull").click(function() {
        closeFullscreen();
        $("#btnfull").show();
        $(this).hide();
    });
    $("#btnselesai").click(function() {
        swal({
            title: 'Apa anda yakin?',
            text: "aksi ini akan menyelesaikan secara paksa semua ujian yang sedang berlangsung!",
			type: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: 'Yes!',
            target: document.getElementById("statusfull"),
        }).then((result) => {
            if (result.value) {
                <?php if (isset($_GET['id'])) { ?>
                    var urlx = 'status/ajax_status.php?pg=selesaisemua&id=<?= $_GET['id'] ?>';
                <?php } else { ?>
                    var urlx = 'status/ajax_status.php?pg=selesaisemua';
                <?php } ?>
                $.ajax({
                    url: urlx,
                    method: "POST",
                    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;
        })

    });
    $(document).on('click', '.hapus', function() {
        var id = $(this).data('id');
        console.log(id);
        swal({
            title: 'Apa anda yakin?',
            text: "aksi ini akan menyelesaikan secara paksa ujian yang sedang berlangsung!",
			type: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: 'Yes!',
            target: document.getElementById("statusfull"),
        }).then((result) => {
            if (result.value) {
                $.ajax({
                    url: 'status/ajax_status.php?pg=selesaikan',
                    method: "POST",
                    data: 'id=' + id,
                    success: function(data) {
                        //$('#htmlujianselesai').html('1');
                        toastr.options.target = '#statusfull';
                        toastr.success(data);
                    }
                });
            }
        })
    });

    $(document).on('click', '.ulang', function() {
        var id = $(this).data('id');
        console.log(id);
        swal({
            title: 'Apa anda yakin?',
            text: "Akan Mengulang Ujian Ini ??",
            type: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: 'Yes!',
            target: document.getElementById("statusfull"),
        }).then((result) => {
            if (result.value) {
                $.ajax({
                    url: 'status/ajax_status.php?pg=ulangujian',
                    method: "POST",
                    data: 'id=' + id,
                    success: function(data) {
                      iziToast.info({
						title: 'Sukses!',
						message: 'Data berhasil diulang',
						titleColor: '#FFFF00',
						messageColor: '#fff',
						backgroundColor: 'rgba(0, 0, 0, 0.5)',
						progressBarColor: '#FFFF00',
						position: 'topRight'		    				  
                      });
                        setTimeout(function() {
                            window.location.reload();
                        }, 2000);
                    }
                });
            }
            return false;
        })

    });
</script>