<?php ob_start();
error_reporting(0);
require("../../config/koneksi.php");
require("../../config/function.php");
require("../../config/crud.php");
session_start();
$user = mysqli_fetch_array(mysqli_query($koneksi, "select * from users WHERE jabatan='$_GET[kelas]'"));

$kelas=$_GET['kelas'];
$jmpl = mysqli_num_rows(mysqli_query($koneksi, "SELECT * FROM jadwal_mapel WHERE kelas='$kelas' "));
$bl = date('m');
$bulane = fetch ($koneksi, 'bulan', ['bln' =>$bl]);
?>
<!DOCTYPE html>
<html>

<head>
    <meta charset='utf-8'>

     <title>KI-4_<?= $kelas ?></title>
   <link rel="stylesheet" href="../../plugins/bootstrap/dist/css/bootstrap.min.css">


</head>
<?php
  
	if($setting['semester']=='1'){
{$smt="(Satu)";}
}elseif($setting['semester']=='2'){
{$smt="(Dua)";}
}
  ?>
<body style="font-size: 12px;">
    
   
    <center>
        <h5>DAFTAR KUMPULAN NILAI</h5>
		<h5>(KETERAMPILAN)</h5>
    </center>
    <br>
    
    <div class="col-md-14">
	
        <table style="margin-left: 80px;margin-right:60px"  width="100%" >
            <tr>
                <td width="15%">Satuan Pendidikan</td>
                <td width="1%">:</td>
				<td width="40%"><?= $setting['sekolah'] ?></td>
				<td></td>
				<td width="17%">Kelas</td>
                <td width="1%">:</td>
				<td width="20%"><?= $kelas ?></td>
            </tr>
            <tr>
                <td >Alamat</td>
                <td>:</td>
				<td><?= $ms['alamat'] ?> Kec. <?= $setting['kecamatan'] ?></td>
				<td></td>
				<td>Semester</td>
                <td>:</td>
				<td> <?= $setting['semester'] ?> <?= $smt ?></td>
            </tr>
			<tr>
                <td>Wali Kelas</td>
                <td>:</td>
				<td><?= $user['nama'] ?></td>
				<td></td>
				<td>Tahun Pelajaran</td>
                <td>:</td>
				<td><?= $setting['tp'] ?></td>
            </tr>
			
        </table>
       
        <br>
 <table style="margin-left: 10px;margin-right:10px"  width="100%" border='1'>
            <tr style="text-align: center">
			<td width="3%" rowspan='2' >No</td>
			<td width="8%" rowspan='2'>NIS</td>
			<td rowspan='2'>NAMA LENGKAP</td>
			<td width="3%" rowspan='2'>JK</td>
			<td colspan='<?= $jmpl ?>'>Keterampilan (KI-4)</td>
			<td width="5%"  rowspan='2'>JML NILAI</td>
			</tr>
			<tr style="text-align: center">
			<?php
             $queryx = mysqli_query($koneksi, "select * from jadwal_mapel WHERE kelas='$kelas' order by urut");
              while ($mapel = mysqli_fetch_array($queryx)) { ?>
                 <td width="4%"><?= $mapel['kode'] ?></td>
                                <?php } ?>
			
			</tr>
			 <?php
               $query = mysqli_query($koneksi, "select * from siswa where id_kelas='$kelas' ");
                            $no = 0;
                            while ($siswa = mysqli_fetch_array($query)) {
                                $no++;
                            ?>
                                <tr>
                                    <td style="text-align: center"><?= $no; ?></td>
                                    <td style="text-align: center"><?= $siswa['nis'] ?></td>
                                    <td>&nbsp;<?= ucwords(strtolower($siswa['nama'])) ?></td>
			                       <td style="text-align: center"><?= substr($siswa['jenis_kelamin'],0,1) ?></td>
								    <?php
								  $tahun=date('Y');
                                    $queryx = mysqli_query($koneksi, "select * from jadwal_mapel WHERE kelas='$kelas' order by urut");
                                    while ($mapel = mysqli_fetch_array($queryx)) {
                                       $n1 = mysqli_query($koneksi, "SELECT AVG(nilai) AS rata FROM nilai_rapor WHERE mapel='$mapel[kode]' AND nis='$siswa[nis]'  AND ki='KI4' AND smt='$setting[semester]' AND tahun='$tahun' ");
                                    while ($kl = mysqli_fetch_array($n1)) {
						           $rerata=$kl['rata'];
								  
								   ?>
                                  <td style="text-align: center"><?= round($rerata) ?></td>
								   <?php } }?>
									<?php
									 $n2 = mysqli_query($koneksi, "SELECT SUM(nilai) AS ratax,COUNT(nilai) AS bagi FROM nilai_rapor WHERE nis='$siswa[nis]'  AND ki='KI4' AND smt='$setting[semester]' AND tahun='$tahun' ");
                                    while ($kl2 = mysqli_fetch_array($n2)) {
										$n3 = mysqli_query($koneksi, "SELECT COUNT(jadwal_mapel.kode) AS kali FROM jadwal_mapel WHERE EXISTS(SELECT mapel FROM nilai_rapor
										WHERE jadwal_mapel.kode=nilai_rapor.mapel AND jadwal_mapel.kelas='$kelas' AND nilai_rapor.ki='KI4' )");
										 $kl3 = mysqli_fetch_array($n3);
										$jumlah=($kl2['ratax']/$kl2['bagi'])*$kl3['kali'];
									
										 ?>
		                            <td style="text-align: center"><?= round($jumlah,0) ?></td>   
									<?php } ?>
							</tr>
			<?php } ?>
		</table>	
		<br/>
	<table border='0' style="margin-left: 80px;width:850">
					<tr>
					
						<td>
							Mengetahui, <br/>
							Kepala Sekolah <br/>
							<br/>
							<br/>
							<br/>
							
							<u><?= $setting['kepsek'] ?></u><br/>
							NIP. <?= $setting['nip'] ?>
						</td>
						<td width='400px'></td>
						<td>
							<?= $setting['kecamatan'] ?>, <?php echo date('d'); ?> <?= $bulane['ket'] ?> <?= date('Y') ?><br/>
							Wali Kelas <?= $kelas ?><br/>
							<br/>
							<br/>
							<br/>
							
							<u><?= $user['nama'] ?></u><br/>
							NIP. <?= $user['nip'] ?>
						</td>
					</tr>
				</table>
</body>

</html>
<?php

$html = ob_get_clean();
require_once '../../vendors/autoload.php';

use Dompdf\Dompdf;

$dompdf = new Dompdf();
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'landscape');
$dompdf->render();
$dompdf->stream("Leger_KI-4.pdf", array("Attachment" => false));
exit(0);
?>