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

    if (empty($_GET['kelas'])) {
        $id_kelas = "";
    } else {
        $id_kelas = $_GET['kelas'];
    }
    if (empty($_GET['id'])) {
        $id_bank = "";
        $mapel = mysqli_fetch_array(mysqli_query($koneksi, "SELECT * FROM banksoal where id_bank='$id_bank' "));
    } else {
        $id_bank = $_GET['id'];
        $mapel = mysqli_fetch_array(mysqli_query($koneksi, "SELECT * FROM banksoal where id_bank='$id_bank' "));
        $kelas = implode("','", unserialize($mapel['kelas']));
        if ($_GET['kelas']) {
            $peserta = mysqli_query($koneksi, "SELECT * FROM siswa where id_kelas='$_GET[kelas]'");
        } else {
            if ($kelas == "semua") {
                $peserta = mysqli_query($koneksi, "SELECT * FROM siswa ");
            } else {
                $peserta = mysqli_query($koneksi, "SELECT * FROM siswa where id_kelas IN ('" . $kelas . "')");
            }
        }
    }



    ?>
    <div class='row'>
        <div class='col-md-12'>
           <div class="card">
               <div class="card-header">
                    <h5 class='card-title'><i class="fas fa-th-list"></i> Nilai <?= $mapel['nama'] ?></h5>
                    <div class='right'>
                        <button class='btn btn-sm btn-outline-primary' onclick="frames['frameresult'].print()" data-placement="top" data-toggle="tooltip" data-original-title="Cetak Data"><i class='fa fa-print'></i> </button>
                        <iframe name='frameresult' src='sandik_ujian/nilaicbt.php?m=<?= $id_bank ?>&k=<?= $id_kelas ?>' style='display:none'></iframe>
                        <a class='btn btn-sm btn-outline-success' href='sandik_ujian/prosescbt.php?m=<?= $id_bank ?>&k=<?= $id_kelas ?>' data-placement="top" data-toggle="tooltip" data-original-title="Excel"><i class='fa fa-download'></i> </a>
                        <a class='btn btn-sm btn-outline-danger' href='?' data-placement="top" data-toggle="tooltip" data-original-title="Kembali"><i class='fa fa-times'></i> </a>
                    
					</div>
                </div>
                <div class='card-body'>
                    <div class="row" style="padding-bottom: 10px;">


                        <div class="col-md-3">
                            <select id="pilihmapel" class="form-select ujian">
                                <?php
                                if ($user['level'] == 'admin') {
                                    $ujian = mysqli_query($koneksi, "select * FROM banksoal a join nilai b ON a.id_bank=b.id_bank WHERE a.groupsoal='CBT' group by a.id_bank");
                                } else {
                                    $ujian = mysqli_query($koneksi, "select * FROM banksoal a join nilai b ON a.id_bank=b.id_bank where a.idguru='$id_user' AND a.groupsoal='CBT' group by a.id_bank");
                                }
                                ?>
                                <option> Pilih Mata Pelajaran</option>
                                <?php while ($uj = mysqli_fetch_array($ujian)) : ?>
                                    <option <?php if ($id_bank == $uj['id_bank']) {
                                                echo "selected";
                                            } else {
                                            } ?> value="<?= $uj['id_bank'] ?>"><?= $uj['id_bank'] ?> - <?= $uj['kode'] ?> - <?= $uj['nama'] ?></option>
                                <?php endwhile; ?>
                            </select>
                        </div>
                        <div class="col-md-3">
                            <select id="pilihkelas" class="form-select kelas">

                            </select>
                        </div>
                        <div class="col-md-3">

                            <button id="cari_nilai" class="btn btn-outline-success"><i class="fas fa-search"></i> Cari Nilai</button>
                            <script type="text/javascript">
                                $('#cari_nilai').click(function() {
                                    var kelas = $('.kelas').val();
                                    var ujian = $('.ujian').val();
                                    location.replace("?pg=nilaicbt&kelas=" + kelas + "&id=" + ujian);
                                }); //ke url
                            </script>

                        </div>
       
                    </div>
                    <div id="tablenilai" class='table-responsive'>
                        <table id="datatable1" class='table table-hover'>
                            <thead>
                                <tr>
                                    <th width='5px'>#</th>
                                    <th>No Peserta</th>
                                    <th>Nama</th>
                                    <th>Kelas</th>
                                    <th>Lama Ujian</th>
                                    <th>Analisis</th>
                                    <th>Nilai PG</th>
                                    <th>Essai</th>
                                    <th>Total</th>
                                    <th>Jawaban</th>
                                </tr>
                            </thead>
                            <tbody>

                                <?php if (!empty($_GET['id'])) { ?>
                                    <?php while ($siswa = mysqli_fetch_array($peserta)) : ?>
                                        <?php
                                        $no++;
                                        $ket = '';
                                        $esai =  $jawaban = $skor = $total = '--';
                                        $selisih = 0;
                                       
                                        $nilaiQ = mysqli_query($koneksi, "SELECT * FROM nilai WHERE id_bank='$id_bank' AND id_siswa='$siswa[id_siswa]'");
                                        $nilaiC = mysqli_num_rows($nilaiQ);
                                        $nilai = mysqli_fetch_array($nilaiQ);
                                        if ($nilaiC <> 0) :
                                            $selisih = '';
                                            if ($nilai['ujian_mulai'] <> '' and $nilai['ujian_selesai'] <> '') :
                                                $selisih = strtotime($nilai['ujian_selesai']) - strtotime($nilai['ujian_mulai']);
                                                $esai = "$nilai[nilai_esai]";
                                                $jawaban = "<small class='badge badge-primary'>$nilai[jml_benar] <i class='fa fa-check'></i></small>  <small class='badge badge-danger'>$nilai[jml_salah] <i class='fa fa-times'></i></small>";
                                                $skor = number_format($nilai['skor'], 2, '.', '');
                                                $total = "<b>" . number_format($nilai['total'], 2, '.', '') . "</b>";
                                                $ket = "";

                                            elseif ($nilai['ujian_mulai'] <> '' and $nilai['ujian_selesai'] == '') :
                                                $selisih = strtotime($nilai['ujian_berlangsung']) - strtotime($nilai['ujian_mulai']);

                                                $ket = "<i class='fa fa-spin fa-spinner' title='Sedang ujian'></i>";
                                                $skor = $total = '--';
                                            endif;
                                        endif;
                                        ?>
                                        <tr>
                                            <td><?= $no ?></td>
                                            <td><?= $siswa['no_peserta'] ?></td>
                                            <td><?= $siswa['nama'] ?></td>
                                            <td><?= $siswa['id_kelas'] ?></td>
                                            <td><?= $ket . " " . lamaujian($selisih) ?></td>
                                            <td><?= $jawaban ?></td>
                                            <td><?= $skor ?></td>
                                            <td><?= $esai ?></td>
                                            <td><?= $total ?></td>
                                            <td>
                                                <?php if ($nilai['skor'] <> "") : ?>
                                                    <?php
                                                    if ($nilai['jawaban'] <> "") :
                                                        $ket = '';
                                                        $link = "?pg=" . $pg . "&ac=esai&id=" . $_GET['id'] . "&ids=" . $siswa['id_siswa'];
                                                        $link2 = "?pg=" . $pg . "&ac=jawaban&id=" . $_GET['id'] .  "&ids=" . $siswa['id_siswa'];
                                                    else :
                                                        $ket = 'style="display:none"';
                                                        $link = '#';
                                                        $link2 = '#';
                                                    endif;
                                                    ?>
                                                   
                                                    <a href='<?= $link2 ?>' class='btn btn-sm btn-outline-warning' <?= $ket ?> data-placement="top" data-toggle="tooltip" data-original-title="Lihat Jawaban"><i class='fa fa-eye'></i> </a>
                                                   <a href="sandik_ujian/prosesjawabcbt.php?idnilai=<?= $nilai['id_nilai'] ?>&ids=<?= $siswa['id_siswa'] ?>" class='btn btn-sm btn-outline-primary' <?= $ket ?>><i class='fa fa-download'></i></a>
                                               
												   <?php if($user['level']=="admin") { ?>
													<button class='ulangnilai btn btn-sm btn-outline-danger' data-id="<?= $nilai['id_nilai'] ?>" <?= $ket ?> data-placement="top" data-toggle="tooltip" data-original-title="Ulang Ujian"><i class='fa fa-recycle'></i> </button>
													
													<?php } ?>
                                                    <?php if ($nilai['jawaban_esai'] <> null) { ?>
                                                        <button type="button" class="btn btn-sm btn-outline-info " data-bs-toggle="modal" data-bs-target="#modelId<?= $nilai['id_nilai'] ?>" >
                                                            <i class="fas fa-edit"></i> </button>
                                                        
                                                        <div class="modal fade" id="modelId<?= $nilai['id_nilai'] ?>" tabindex="-1" role="dialog" aria-labelledby="modelTitleId" aria-hidden="true">
                                                            <div class="modal-dialog modal-lg">
														   <div class="modal-content">
															  <div class="modal-header">
																<h5 class="modal-title" id="tambahjadwal">Nilai Esai</h5>
																   <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
																		</div>
																		  <div class="modal-body">
                                                                        <form id='formnesai<?= $nilai['id_nilai'] ?>'>
                                                                            <table id="datatable" class='table table-hover'>

                                                                                <tbody>
                                                                                    <?php $noX = 0;
                                                                                    $jawabanesai = unserialize($nilai['jawaban_esai']);
                                                                                    $nesai = unserialize($nilai['nilai_esai2']); ?>
                                                                                    <?php foreach ($jawabanesai as $key2 => $value2) : ?>
                                                                                        <?php
                                                                                        $noX++;
                                                                                        $soal = mysqli_fetch_array(mysqli_query($koneksi, "SELECT * FROM soal WHERE id_soal='$key2'"));

                                                                                        ?>
                                                                                        <tr>
                                                                                            <td width="5%"><?= $noX ?></td>
                                                                                            <td><?= $soal['soal'] ?>
                                                                                                <p><b>JAWAB :</b> <?= $value2 ?></p>
                                                                                            </td>
                                                                                            <td width="10%">
                                                                                                <input type="hidden" class="form-control" name="id" value="<?= $nilai['id_nilai'] ?>">
                                                                                                <input style="width: 100px" type="text" class="form-control" name="nesai<?= $nilai['id_nilai'] ?>[<?= $key2 ?>]" value="<?= $nesai[$key2] ?>">
                                                                                            </td>
                                                                                        </tr>
                                                                                    <?php endforeach; ?>
                                                                                </tbody>
                                                                            </table>
                                                                    </div>
                                                                    <div class="modal-footer">
                                                                       
                                                                        <button type="submit" id="simpanesai<?= $nilai['id_nilai'] ?>" class="btn btn-sm btn-outline-primary">Simpan</button>
                                                                    </div>
                                                                    </form>
                                                                </div>
                                                            </div>
                                                        </div>
                                                        <script>
                                                            $("#formnesai<?= $nilai['id_nilai'] ?>").submit(function(e) {
                                                                e.preventDefault();
                                                                var id = '<?= $nilai['id_nilai'] ?>';
                                                                $.ajax({
                                                                    type: "POST",
                                                                    url: "sandik_ujian/simpanesai.php",
                                                                    data: $(this).serialize(),
                                                                    success: function(result) {
                                                                        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 } ?>
                                                <?php else : ?>
                                                    <?php if ($nilai['ujian_mulai'] <> '' and $nilai['ujian_selesai'] == '') { ?>
                                                        <button data-id='<?= $nilai['id_nilai'] ?>' class='hapus btn btn-xs btn-danger'>Bantu Selesai</button>
                                                    <?php  } ?>
                                                <?php endif; ?>
                                            </td>
                                        </tr>
                                    <?php endwhile; ?>
                                <?php } ?>
                            </tbody>
                        </table>

                    </div>
                </div>
            </div>
        </div>
    </div>
    <script>
        $("#pilihmapel").change(function() {
            var mapel_id = $(this).val();
            console.log(mapel_id);
            $.ajax({
                type: "POST", 
                url: "sandik_ujian/crud_nilai.php?pg=ambil_kelas",
                data: "mapel_id=" + mapel_id, 
                success: function(response) { 
                    $("#pilihkelas").html(response);
                    console.log(response);
                },
                error: function(xhr, status, error) {
                    console.log(error);
                }
            });
        });
    </script>
<?php elseif ($ac == 'jawaban') : ?>
    <?php
    $idmapel = $_GET['id'];
    $id_siswa = $_GET['ids'];
    $nilai = mysqli_fetch_array(mysqli_query($koneksi, "SELECT * FROM nilai WHERE id_siswa='$id_siswa' and id_bank='$idmapel'"));
    $mapel = mysqli_fetch_array(mysqli_query($koneksi, "SELECT * FROM banksoal where id_bank='$idmapel'"));
    $siswa = mysqli_fetch_array(mysqli_query($koneksi, "SELECT * FROM siswa WHERE id_siswa='$id_siswa'"));
    ?>
    <div class='row'>
        <div class='col-md-12'>
           <div class="card">
               <div class="card-header">
                    <h5 class='card-title'>Data Hasil Ujian</h5>
                   
                </div>
                <div class='card-body'>
                    <table class='table table-hover'>
                        <tr>
                            <th width='150'>No Induk</th>
                            <td width='10'>:</td>
                            <td><?= $siswa['nis'] ?></td>
                            <td style="text-align:center; width:150">Nilai PG</td>
                            <td style="text-align:center; width:150">Nilai Esai</td>
                            <td style="text-align:center; width:150">Total Nilai</td>
                        </tr>
                        <tr>
                            <th>Nama</th>
                            <td width='10'>:</td>
                            <td><?= $siswa['nama'] ?></td>
                            <td rowspan='3' style='font-size:30px; text-align:center; width:150'><?= $nilai['skor'] ?></td>
							<?php if($nilai['nilai_esai']<>''){ ?>
                            <td rowspan='3' style='font-size:30px; text-align:center; width:150'><?= $nilai['nilai_esai'] ?></td>
							<?php }else{ ?>
							 <td rowspan='3' style='font-size:30px; text-align:center; width:150'><?= $nilai['skor_esai'] ?></td>
							<?php } ?>
                            <td rowspan='3' style='font-size:30px; color:blue; text-align:center; width:150'><?= $nilai['total'] ?></td>
                        </tr>
                        <tr>
                            <th>Kelas</th>
                            <td width='10'>:</td>
                            <td><?= $siswa['id_kelas'] ?></td>
                        </tr>
                        <tr>
                            <th>Mata Pelajaran</th>
                            <td width='10'>:</td>
                            <td><?= $mapel['kode'] ?></td>
                        </tr>
                    </table>
                    <br>
                    <div class='table-responsive'>
                        <table class='table table-hover'>
                            <thead>
                                <tr>
                                    <th width='5px'>#</th>
                                    <th>Soal PG</th>
                                    <th style='text-align:center'>Jawab</th>
                                    <th style='text-align:center'>Hasil</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php $jawaban = unserialize($nilai['jawaban']); ?>
                                <?php foreach ($jawaban as $key => $value) : ?>
                                    <?php
                                    $no++;
                                    $soal = mysqli_fetch_array(mysqli_query($koneksi, "SELECT * FROM soal WHERE id_soal='$key'"));
                                    if ($value == $soal['jawaban']) :
                                        $status = "<span class='badge badge-primary'><i class='fa fa-check'></i></span>";
                                    else :
                                        $status = "<span class='badge badge-primary'><i class='fa fa-times'></i></span>";
                                    endif;
                                    ?>
                                    <tr>
                                        <td><?= $no ?></td>
                                        <td><?= $soal['soal'] ?></td>
                                        <td style='text-align:center'><?= $value ?></td>
                                        <td style='text-align:center'><?= $status ?></td>
                                    </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                        <?php if ($nilai['jawaban_esai'] <> null) { ?>
                            <table class='table table-hover'>
                                <thead>
                                    <tr>
                                        <th width='5px'>#</th>
                                        <th>Soal ESAI</th>
                                        <th>Jawab</th>

                                    </tr>
                                </thead>
                                <tbody>
                                    <?php $noX = 0;
                                    $jawabanesai = unserialize($nilai['jawaban_esai']); ?>

                                    <?php foreach ($jawabanesai as $key2 => $value2) : ?>
                                        <?php
                                        $noX++;
                                        $soal = mysqli_fetch_array(mysqli_query($koneksi, "SELECT * FROM soal WHERE id_soal='$key2'"));

                                        ?>
                                        <tr>
                                            <td><?= $noX ?></td>
                                            <td style='text-align:justify'><?= $soal['soal'] ?></td>
                                            <td><?= $value2 ?></td>

                                        </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        <?php } ?>
                        <br>
                        <!-- <table class='table table-bordered table-striped'>
							<thead>
								<tr>
									<th width='5px'>#</th>
									<th>Soal Esai</th>
									<th style='text-align:center'>Hasil</th>
								</tr>
							</thead>
							<tbody>
								<?php $nilaiex = mysqli_query($koneksi, "SELECT * FROM hasil_jawaban WHERE id_siswa='$id_siswa' and id_bank='$idmapel' and jenis='2' and id_ujian='$nilai[id_ujian]' ");
                                $nox = 0; ?>
								<?php while ($jawabane = mysqli_fetch_array($nilaiex)) : ?>
									<?php
                                    $soal = mysqli_fetch_array(mysqli_query($koneksi, "SELECT * FROM soal WHERE id_soal='$jawabane[id_soal]'"));
                                    $nox++;
                                    ?>
									<tr>
										<td><?= $nox ?></td>
										<td><?= $soal['soal'] ?>
											<p><b>jawab : </b><?= $jawabane['esai'] ?></p>
										</td>
										<td style='text-align:center'><?= $jawabane['nilai_esai'] ?></td>
									</tr>
								<?php endwhile; ?>
							</tbody>
						</table> -->
                    </div>
                </div>
            </div>
        </div>
    </div>
<?php endif; ?>
<script>
 
    $(document).on('click', '.ulangnilai', function() {
        var id = $(this).data('id');
        console.log(id);
        swal({
            title: 'Apa anda yakin?',
            text: " Akan Mengulang Ujian Ini ??",
            showCancelButton: true,
			type:'warning',
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: 'Yes!'
        }).then((result) => {
            if (result.value) {
                $.ajax({
                    url: 'sandik_ujian/ulangujian.php',
                    method: "POST",
                    data: 'id=' + id,
                    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>