<?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="card">
             <div class="card-header">
                  <h5 class='card-title'><i class="fas fa-graduation-cap"></i> Nilai PTS - <?php if($setting['semester']=='1'){ ?> PAS <?php }else{ ?> PAT <?php } ?> Pengetahuan (KI-3)</h5>
                </div>
			
            <div class='card-body'>
                   <div class='table-responsive'>
                        <table style="font-size: 12px" id='example1' class='table  table-hover'>
                            <thead>
                            <tr>
                              <th width="5%" class="text-center">
                                    #
                                </th>
                                <th width="10%">Kelas</th>
                                 <th>Nama Mapel</th>
							<th width="25%" style="text-align:center;">Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
							$tahun=date('Y');
                            $query = mysqli_query($koneksi, "select * FROM jadwal_mapel	JOIN kelas ON kelas.id_kelas=jadwal_mapel.kelas					
							WHERE guru='$_SESSION[id_user]' AND kelas.kurikulum='K-13'");
                            $no = 0;
                            while ($mapel = mysqli_fetch_array($query)) {
							$jumlah = mysqli_num_rows(mysqli_query($koneksi, "SELECT * FROM nilai_rapor WHERE mapel='$mapel[kode]' AND kelas='$mapel[kelas]' AND ki='KI3' AND smt='$setting[semester]' AND tahun='$tahun' "));
							
                                $no++;
                            ?>
                                <tr>
                                    <td><?= $no; ?></td>
									<td><?= $mapel['kelas'] ?></td>
                                    <td><?= $mapel['mapel'] ?></td>
									
									
                                    <td>
									<a href="?pg=nr&ac=input&mapel=<?= $mapel['kode'] ?>&kelas=<?= $mapel['kelas'] ?>&smt=<?= $setting['semester'] ?>" class="btn btn-sm btn-outline-primary" >
                                          <i class="fas fa-plus" data-bs-placement="top" data-bs-toggle="tooltip" title="Input Manual"></i></button></a>
									<?php if($jumlah==0){ ?>
									<a href="sandik_rapor/proses_ki3.php?id=<?= $mapel['id_jadwal'] ?>" class="btn btn-sm btn-outline-warning" >
                                          <i class="fas fa-download" data-bs-placement="top" data-bs-toggle="tooltip" title="Download Format"></i></button></a>
										<?php }else{ ?>
										 <button type="button" class="btn btn-sm btn-default" disabled>
                                          <i class="fas fa-lock" ></i>
                                        </button>
										<?php } ?>
										 <?php if($jumlah==0){ ?>
                                       <a href="?pg=nr&ac=upload&id=<?= $mapel['id_jadwal'] ?>" class="btn btn-sm btn-outline-success" data-bs-placement="top" data-bs-toggle="tooltip" title="Upload">
                                          <i class="fas fa-upload"></i>
                                        </a>
										<?php }else{ ?>
										 <button type="button" class="btn btn-sm btn-default" disabled>
                                          <i class="fas fa-lock" ></i>
                                        </button>
										<?php } ?>
										   
									<a href="?pg=nr&ac=view3&id=<?= $mapel['id_jadwal'] ?>" class="btn btn-sm btn-outline-info" >
                                          <i class="fas fa-search" title="Lihat"></i></button></a>
										</td>
										   
                                </tr>
                            <?php }
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>


<?php } elseif ($ac == 'upload') { ?>
<?php $mapel=fetch($koneksi,'jadwal_mapel',['id_jadwal'=>$_GET['id']]); ?>
<div class='row'>
    <div class='col-md-12'>
        <div class="card">
             <div class="card-header">
					<h5 class='card-title'><i class="fas fa-users-cog"></i> Import Nilai <?= $mapel['kode'] ?> (KI-3)</h5>
					</div>   
            
                    <div class='card-body'>
                                <form id='formsiswa' enctype='multipart/form-data'>
                                    <div class='col-md-4'>
                                        <label>Pilih File</label>
                                        <input type='file' name='file' class='form-control' required='true' />
										<input type="hidden" name="mapel" value="<?= $mapel['kode'] ?>" >
										<input type="hidden" name="kelas" value="<?= $mapel['kelas'] ?>" >
                                    </div>
                                    <div class='col-md-4'>
                                        <label>&nbsp;</label><br>
                                        <button type='submit' name='submit' class='btn btn-success right'><i class='fa fa-upload'></i> Import</button>
                                    </div>
                                </form>
                            </div>
                        </div>
						
                        <?= $info ?>
                        
                        <div id='progressbox'></div>
                        <div id='hasilimport'></div>
                    </div>
                </div>
            </div>
           
        </div>
    </div>
</div>
<script>
    $('#formsiswa').on('submit', function(e) {
        e.preventDefault();
        $.ajax({
            type: 'post',
            url: 'sandik_rapor/crud_nilai3.php',
            data: new FormData(this),
            processData: false,
            contentType: false,
            cache: false,
            beforeSend: function() {
                $('#progressbox').html('<div><img src="<?= $homeurl ?>/dist/img/animasi1.gif" style="margin-left:100px"></div>');
                $('.progress-bar').animate({
                    width: "30%"
                }, 100);
            },
            success: function(response) {
                setTimeout(function() {
                    $('.progress-bar').css({
                        width: "100%"
                    });
                    setTimeout(function() {
                        $('#hasilimport').html(response);
						});
						setTimeout(function() {
                    window.location.replace('?pg=nr');
                    }, 2000);
                }, 2000);
            }
        });
    });
</script>

<?php } elseif ($ac == 'view3') { ?>
<?php
$id=$_GET['id'];
$jadwal=fetch($koneksi,'jadwal_mapel',['id_jadwal' =>$id]);
$mapel=$jadwal['mapel'];
$kode=$jadwal['kode'];
$klas=$jadwal['kelas'];
$ki='KI3';
?>

    <div class='row'>
        <div class='col-md-12'>
               <div class="card">
             <div class="card-header">
			 <div class='right'>
					 <a href="?pg=hapus_nilai3&idmap=<?php echo $kode ?>&kelas=<?php echo $klas ?>&ki=<?php echo $ki ?>&smt=<?php echo $setting['semester'] ?>&tahun=<?php echo date('Y'); ?>"  class='btn btn-sm btn-outline-danger' id="edis"><i class='fa fa-trash'></i>Reset</a>
				      <a href="?pg=nr&ac=edit&idmap=<?php echo $kode ?>&kelas=<?php echo $klas ?>&smt=<?php echo $setting['semester'] ?>&tahun=<?php echo date('Y'); ?>"  class='btn btn-sm btn-outline-warning'><i class='fa fa-edit'></i></i>Edit</a>
					  <a target="_blank" href="sandik_rapor/cetaknilai3.php?idmap=<?php echo $id ?>&kelas=<?php echo $klas ?>"  class='btn btn-sm btn-outline-success'><i class='fa fa-print'></i></i>Cetak</a>
		   <a href="?pg=nr" class="btn btn-sm btn-outline-primary" ><i class="fas fa-home"></i>Kembali</a>
		  </div>
                  <h5 class='card-title'><i class="fas fa-graduation-cap"></i> Nilai Rapor <?= strtoupper($mapel) ?> (KI-3)</h5>
                </div>
			
            <div class='card-body'>
                   <div class='table-responsive'>
                        <table style="font-size: 11px" id='tabeldeskrip' class='table table-hover'>
                            <thead>
                            <tr>
                             <th width="5%" class="text-center">
                                    #
                                </th>
                                <th width="20%">NIS</th>
							<th>Nama</th>
							<th width="5%">Kelas</th>
							<th width="5%">KKM</th>
							<th width="5%">NR</th>
							<th width="5%">Pred</th>
						
                            </tr>
                        </thead>
                        <tbody>
                            <?php
							$tahun=date('Y');
                           $query = mysqli_query($koneksi, "select  AVG(nilai_rapor.nilai) as rata,siswa.nis,siswa.nama,siswa.id_kelas from nilai_rapor
			               LEFT JOIN siswa ON siswa.nis=nilai_rapor.nis
                           WHERE  nilai_rapor.ki='KI3' AND nilai_rapor.mapel='$kode' AND nilai_rapor.kelas='$klas' AND nilai_rapor.smt='$setting[semester]' AND nilai_rapor.tahun='$tahun' GROUP BY nilai_rapor.nis");
                            $no = 0;
                            while ($siswa = mysqli_fetch_array($query)) {
								$rata=round($siswa['rata']);
							$rentang=round(100-$jadwal['kkm'])/3;		
							$predD=round($jadwal['kkm']-1);
                            $nilC1=round($jadwal['kkm']);
							 $nilC2=round($rentang+$jadwal['kkm']);
							 
							$nilB1=round($nilC2+1);
							$nilB2=round($nilC2+$rentang);
			
							$nilA1=round($nilB2+1);
							$nilA2=round($nilB2+$rentang);
							
							
if($rata<=$predD){
{$grade="D";}
}elseif($rata>=$nilC1 && $rata<=$nilC2){
{$grade="C";}
}elseif($rata>=$nilB1 && $rata<=$nilB2){
{$grade="B";}
}elseif($rata>=$nilA1 && $rata<=$nilA2){
{$grade="A";}
}elseif($rata==100){
{$grade="A";}
}	
								
								
                                $no++;
                            ?>
                                <tr>
                                    <td><?= $no; ?></td>
                                    <td><?= $siswa['nis'] ?></td>
                                    <td><?= $siswa['nama'] ?></td>
									<td><?= $siswa['id_kelas'] ?></td>
									<td><?= $jadwal['kkm'] ?></td>
									<td><?= round($siswa['rata'] )?></td>
									<td><?= $grade ?></td>
							          
                                </tr>
								
							
                            <?php }
                            ?>



                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>


<?php } elseif ($ac == 'input') { ?>
<div class='row'>
 <div class='col-md-12'>
  <div class="card">
             <div class="card-header">
                  <h5 class='card-title'><i class="fas fa-graduation-cap"></i> Nilai Pengetahuan (KI-3) - <?= $_GET['mapel'] ?> - <?= $_GET['kelas'] ?></h5>
                </div>
            <div class='card-body'>
			<form id="formtujuan" action="?pg=nr&ac=input3" method="POST">
			 <div class="table-responsive">							
                     <table style="font-size: 12px" id='example1' class='table table-hover'>
                        <thead>						
                            <tr>
                                <th width='5px'>
                                    #
                                </th>
                                <th>Kode Mapel</th>
								<th width="5%">Kelas</th>
                              <th width="15%">Pilih Nilai</th>
                                 <th>Nilai</th>
                            </tr>
						
                        </thead>
                        <tbody>
                                <tr>
                                    <td>1</td>
                                    <td><?= $_GET['mapel'] ?>
									<input type="hidden" name="mapel" value="<?= $_GET['mapel'] ?>" >
									</td>									
									 <td><?= $_GET['kelas'] ?>
									 <input type="hidden" name="kelas" value="<?= $_GET['kelas'] ?>" >
									 </td>
                                    <td>
									<select  name="ket"  class="form-select"  style="width:100px;" required="true">     
									 <?php 
									   $sql=mysqli_query($koneksi,"SELECT * FROM  deskripsi WHERE mapel='$_GET[mapel]' AND kelas='$_GET[kelas]' AND ki='KI3'");
									   while ($data=mysqli_fetch_array($sql)) { ?>
									<option value="PH<?= $data['urut'] ?>">PH<?= $data['urut'] ?></option> 
									  <?php }
									  ?>
									  <option value="PTS">PTS</option> 
									  <?php if($setting['semester']=='1'){ ?>
									  <option value="PTS">PAS</option> 
									  <?php }else{ ?>
									   <option value="PTS">PAT</option> 
									  <?php } ?>
									</select>	
									</td>
                                    <td>
                                    <button type='submit'  class='btn  btn-outline-success'><i class='fa fa-check-circle'></i> Input</button>
									   </td>
											 </tr>
												</tbody>
													</table>
												
															 </form>	

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

<?php } elseif ($ac == 'input3') { ?>

 <div class='row'>
 <div class='col-md-12'>
  <div class="card">
             <div class="card-header">
					<h5 class='card-title'><i class="fas fa-graduation-cap"></i> Input Nilai Pengetahuan - <?= $_POST['mapel'] ?>/<?= $_POST['kelas'] ?>/<?= $_POST['ket'] ?></h5>
                </div>
            <div class='card-body'>
			<form id="formtujuan" action="" >
			
		  <div class="table-responsive">							
                     <table style="font-size: 12px" id='mapel' class='table table-hover'>
                        <thead>						
                            <tr>
                                <th width='5px'>
                                    #
                                </th>
                                <th>Nama</th>
								<th width="5%">Kelas</th>
                                 <th>Nilai</th>
                            </tr>
						
                        </thead>
                        <tbody>									
						<?php
					$query = mysqli_query($koneksi, "select * from siswa WHERE id_kelas='$_POST[kelas]'");
                            $no = 0; ?>
                           <?php while ($siswa = mysqli_fetch_array($query)) {								
                                $no++;                              
                            ?>
                          <tr>
                                    <td><?= $no; ?></td>
                                    <td><?= $siswa['nama'] ?></td>
									 <td><?= $siswa['id_kelas'] ?>
									 <input type="hidden" name="nis[]" value="<?php echo $siswa['nis'] ?>">
									<input type="hidden" name="kelas[]" value="<?php echo $_POST['kelas'] ?>">
									<input type="hidden" name="mapel[]" value="<?php echo $_POST['mapel'] ?>">
									<input type="hidden" name="ket[]" value="<?php echo $_POST['ket'] ?>">
									<input type="hidden" name="ki[]" value="KI3">
									<input type="hidden" name="smt[]" value="<?= $setting['semester'] ?>">
									<input type="hidden" name="tahun[]" value="<?php echo date('Y') ?>">
									 </td>
									 <td><input type="number" name="nilai[]" value="0" class="form-control" required="true" ></td>
									</tr>
																<?php } ?>
																
															</tbody>
														</table>
														
											<div class='right '>
													<button type='submit'  class='btn btn-success'><i class='fa fa-check-circle'></i>Simpan</button>
																</div>
															 </form>
													</div>
												</div>
						
						</div>
						
	<script>
	 $('#formtujuan').submit(function(e) {
        e.preventDefault();
        var data = new FormData(this);
      
        $.ajax({
            type: 'POST',
            url: 'sandik_rapor/crud_nilai.php?pg=tambah',
            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.replace('?pg=nr');
                }, 2000);

            }
        });
        return false;
    });
	</script>
	
	
<?php } elseif ($ac == 'edit') { ?>
<?php

$mapel=$_GET['idmap'];
$klas=$_GET['kelas'];
$ki='KI3';
?>

    <div class='row'>
        <div class='col-md-12'>
             <div class="card">
             <div class="card-header">
			 <div class="right">
			 <a href="?pg=nr" class="btn btn-sm btn-outline-danger"><i class="fa fa-times-circle"></i>Kembali</a>
			 </div>
					<h5 class='card-title'><i class="fas fa-graduation-cap"></i> EDIT NILAI <?= strtoupper($mapel) ?> (KI-3)</h5>
                </div>
			
                <div class="card-body">
			<form action='' method='POST'>
					<div class='form-group'>
              <label class="bold">Pilih Nilai</label>
				<div class="input-group">
                <select name='ket' id="ket" class='form-select' required='true'>
                   <option value=''></option>
                    <?php $qt = mysqli_query($koneksi, "SELECT * FROM nilai_rapor WHERE mapel='$mapel' AND kelas='$klas' AND ki='KI3' GROUP BY ket"); ?>
                        <?php while ($mt = mysqli_fetch_array($qt)) : ?>
                            <option value="<?= $mt['ket'] ?>"><?= $mt['ket'] ?></option>
                             <?php endwhile ?>
                              </select>  
					<span class="input-group-btn">
              <button type='submit' name="cari" class='btn btn-success'  ><i class='fa fa-search'></i> Cari</button>	
 	       </span>
				</div>	
					<div>
					  
					   </form>
		  
		           <p>
                   <div class='table-responsive'>
                        <table style="font-size: 11px" id='tabeldeskrip' class='table table-hover'>
                            <thead>
                            <tr>
                             <th width="5%" class="text-center">
                                    #
                                </th>
                                <th width="20%">NIS</th>
							<th>Nama</th>
							<th width="5%">Kelas</th>
						   <th width="5%">Ket</th>
							<th>Nilai</th>
							
                            </tr>
                        </thead>
                        <tbody>
						<form id="form-edit">
                            <?php
							 if (isset($_POST['cari'])) {
							$ket=$_POST['ket'];
							 }
                           $query = mysqli_query($koneksi, "select * from nilai_rapor		
                           WHERE  ki='KI3' AND mapel='$mapel' AND kelas='$klas' AND smt='$_GET[smt]' AND tahun='$_GET[tahun]' AND ket='$ket'");
                            $no = 0;
                            while ($siswa = mysqli_fetch_array($query)) {
								$nama=fetch($koneksi,'siswa',['nis'=>$siswa['nis']]);
                                $no++;
                            ?>
                                <tr>
                                    <td><?= $no; ?></td>
                                    <td><?= $siswa['nis'] ?></td>
                                    <td><?= $nama['nama'] ?></td>
									<td><?= $siswa['kelas'] ?></td>
									<td><?= $siswa['ket'] ?></td>
									<td>
									<input type="number" name="nilai[]" class="form-control" value="<?= $siswa['nilai'] ?>" >
									<input type="hidden" name="id[]" class="form-control" value="<?= $siswa['id'] ?>" >
									</td>
									 
                                </tr>
								
							
							 <?php }
                            ?>



                        </tbody>
						
				   </table>
				   <hr>
				   <div class="footer">
				   <?php if($no==''){ ?>
				   
				   <?php }else{ ?>
						 <button type='submit' class='btn btn-success right'><i class='fa fa-check-circle'></i>Simpan</button>	
				   <?php } ?> 
					   </div>
					</form>
                </div>
            </div>
        </div>
    </div>
</div>

	<script>
				  $('#form-edit').submit(function(e) {
        e.preventDefault();
        var data = new FormData(this);
      
        $.ajax({
            type: 'POST',
            url: 'sandik_rapor/crud_edit.php',
            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;
    });
</script>
	
<?php } ?>