<?php ob_start();
error_reporting(0);
require("../../config/koneksi.php");
require("../../config/function.php");
require("../../config/crud.php");
session_start();
if (!isset($_SESSION['id_user'])) {
    die('Anda tidak diijinkan mengakses langsung');
}
$tahun=date('Y');
$ids=$_GET['ids'];

$siswa = fetch($koneksi, 'siswa', ['id_siswa' => $ids]);
$klas=$siswa['id_kelas'];
$tingkat=$siswa['level'];

	if($setting['semester']=='1'){
{$smt="(Satu)";}
}elseif($setting['semester']=='2'){
{$smt="(Dua)";}
}

?>
<!DOCTYPE html>
<html>

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

    <title>Raport_<?= $siswa['nama'] ?></title>
<link rel="stylesheet" href="../../plugins/bootstrap/dist/css/bootstrap.min.css">
</head>

<body style="font-size: 12px;">
    <center>
          <h5>LAPORAN  HASIL PENILAIAN TENGAH SEMESTER</h5>
    </center>
    <br>
    <br>
    <div class="col-md-14">
        <table style="margin-left: 10px;margin-right:10px"  width="100%">
            <tr>
			 <td width="18%">Nama Peserta Didik</td>
                <td width="1%">:</td>
				<td width="45%"><?= $siswa['nama'] ?></td>
				<td></td>
                
				<td width="17%">Kelas</td>
                <td width="1%">:</td>
				<td width="20%"><?= $siswa['id_kelas'] ?></td>
            </tr>
            <tr>
			    <td>N I S</td>
                <td>:</td>
				<td><?= $siswa['nis'] ?></td>
				<td></td>
				<td>Fase</td>
                <td>:</td>
				<td><?= $siswa['fase'] ?></td>
            </tr>
			<tr>
				<td>Sekolah</td>
                <td>:</td>
				<td><?= $setting['sekolah'] ?></td>
				<td></td>
				<td>Semester</td>
                <td>:</td>
				<td><?= $setting['semester'] ?> <?= $smt ?></td>
            </tr>
			<tr>
                <td>Alamat</td>
                <td>:</td>
				<td><?= $setting['alamat'] ?></td>
				<td></td>
				<td>Tahun Pelajaran</td>
                <td>:</td>
				<td><?= $setting['tp'] ?></td>
            </tr>
        </table>
       
        <br>
            <table style="margin-left: 10px;margin-right:10px;" border ="1" width="100%">
                <thead>
                    <tr>
                       <th width="2%"><center>No</center></th>
                        <th width="32%"><center>Muatan Pelajaran</center></th>
					   <th width="7%"><center>Nilai Akhir</center></th>
					   <th><center>Capaian Kompetensi</center></th>
                    </tr>
                </thead>
                <tbody>
                    
                        <?php
                        $q1 = mysqli_query($koneksi, "SELECT * FROM jadwal_mapel WHERE tingkat='$tingkat' GROUP BY tingkat order by urut");
                        $no = 0;
                        while ($kelompok = mysqli_fetch_array($q1)) {

                            $query = mysqli_query($koneksi, "SELECT * FROM jadwal_mapel WHERE tingkat='$siswa[level]' GROUP BY kode order by urut");
                          
                        ?>
                           
							<?php
							while ($mapel = mysqli_fetch_array($query)) {
		
								
					$n1 = mysqli_query($koneksi, "SELECT AVG(nilai) AS rata FROM nilai_formatif 
					JOIN jadwal_mapel ON jadwal_mapel.kode=nilai_formatif.mapel WHERE  jadwal_mapel.kelas='$klas' AND nilai_formatif.mapel= '$mapel[kode]' AND nilai_formatif.idsiswa='$siswa[id_siswa]' AND nilai_formatif.nilai>'0' ");
                        while ($kl = mysqli_fetch_array($n1)) {
						$sum = mysqli_fetch_array(mysqli_query($koneksi, "SELECT AVG(nilai) AS rs FROM nilai_sumatif  WHERE mapel='$mapel[kode]' AND idsiswa='$siswa[id_siswa]'  "));
						$rerata= round($sum['rs']);

			
			$queryx = mysqli_query($koneksi, "select mapel,idsiswa,MIN(nilai) as mini,MAX(nilai) as mak
				 from nilai_formatif WHERE idsiswa='$siswa[id_siswa]'  AND mapel= '$mapel[kode]'");				
				while ($ade = mysqli_fetch_array($queryx)) {			
  
$max   = $ade['mak'];
$min   = $ade['mini'];		

	$pos = fetch($koneksi, 'nilai_formatif', ['nilai' => $max,'mapel' => $mapel['kode'],'idsiswa' =>$siswa['id_siswa']]);	
	$posisi=$pos['ket'];
		$desmax = fetch($koneksi, 'tujuan', ['tp' => $posisi,'idjadwal' => $mapel['id_jadwal']]);	
		
		$pose = fetch($koneksi, 'nilai_formatif', ['nilai' => $min,'mapel' => $mapel['kode'],'idsiswa' =>$siswa['id_siswa']]);
	$posisin=$pose['ket'];
		$desmin = fetch($koneksi, 'tujuan', ['tp' => $posisin,'idjadwal' => $mapel['id_jadwal']]);	
		 
 
							 $no++;
		?>
		
                                <tr>
                                     <td> <center><?= $no ?></center></td>
                  <td>&nbsp;<?= $mapel['mapel'] ?> </td>
				  <td><center><?= round($rerata) ?></center> </td>
				  <td style="text-align:justify;font-size:10px">
				  <?php if($rerata==0){ ?><?php }else{ ?>
				  <?= $siswa['nama'] ?> mampu menunjukan pemahaman dalam <?= $desmax['tujuan'] ?> dan membutuhkan bimbingan dalam <?= $desmin['tujuan'] ?>
				  <?php } ?>
				  </td>
				  </tr>
				
						<?php } } }}?>	
                        
                   
                </tbody>
            </table>
            <br>
      
      <br>  <br> <br>  
            <br>
      
			
            <table style="margin-left: 10px;margin-right:10px;" border ="1" width="100%">
                <thead>
                    <tr>
                       <th width="2%"><center>No</center></th>
                        <th width="32%"><center>Kegiatan Ekstrakurikuler</center></th>
					   <th><center>Keterangan</center></th>
					
                    </tr>
                </thead>
                <tbody>
				 <?php
                 $queryx = mysqli_query($koneksi, "select * from m_eskul");
                 $no = 0;
                 while ($eskul = mysqli_fetch_array($queryx)) {
					$esk=fetch($koneksi,'siswa_eskul',['eskul'=>$eskul['ekstra'],'idsiswa'=>$siswa['id_siswa']]);
                  $no++;
                  ?>
				<tr>
				<td><center><?= $no ?></center> </td>
				<td>&nbsp;<?= $eskul['ekstra'] ?> </td>
				<td style="text-align:justify;font-size:10px">&nbsp;
				<?php if($esk['eskul']== NULL){ ?><?php }else{ ?>
				
               <?php				
				if($esk['nilai']=='A'){
				{$grades="Sangat Baik";}
				}elseif($esk['nilai']=='B'){
				{$grades="Baik";}
				}elseif($esk['nilai']=='C'){
				{$grades="Cukup Baik";}
				}elseif($esk['nilai']=='D'){
				{$grades="Kurang";}
				}
				?>
				<?= $grades ?> dalam mengikuti kegiatan Ekstrakurikuler <?= $esk['eskul'] ?>, <?= $esk['ket'] ?>  
				<?php } ?>
				</td>
				</tr>
				 <?php } ?>
				
	</tbody>
            </table>
            <br>
	
            <table style="margin-left: 10px;margin-right:10px;" border ="1" width="60%">
                <tr>
				<td colspan="2" style="text-align:center"><b>Ketidakhadiran</b> </td>
				</tr>
				<tr>
				<td width="50%">&nbsp;Sakit </td>
				<td style="text-align:center"> <?= $siswa['sakit'] ?> hari</td>
				</tr>
				<tr>
				<td>&nbsp;Izin </td>
				<td style="text-align:center"> <?= $siswa['izin'] ?> hari</td>
				</tr>
				<tr>
				<td>&nbsp;Tanpa Keterangan </td>
				<td style="text-align:center"> <?= $siswa['alpha'] ?> hari</td>
				</tr>
	
            </table>
            <br>
			 <table style="margin-left: 80px;" width="90%">
		<tr>
               <td style="text-align: center" width="33.3%"></td>
                 <td style="text-align: center" width="33.3%"></td>
                <td width="33.3%"><?= $setting['kecamatan'] ?>, <?= $setting['tanggal_rapor'] ?></td>
            </tr>
			</table>
			<table style="margin-left: 80px;" width="90%">
            <tr>
                <td>Mengetahui  :</td>
			<td></td>
				 <td>Wali Kelas <?= $siswa['id_kelas'] ?></td>
            </tr>
			
		<tr>
               <td width="33.3%">Orang Tua/Wali</td>
                 <td  width="33.3%"> </td>
                <td  width="33.3%"></td>
            </tr>
			</table>
			
			<br><br><br>
			
			<table style="margin-left: 80px;" width="90%">
		<tr>
               <td width="33.3%">____________________</td>
                 <td width="33.3%"> </td>
                <td  width="33.3%"><b><?= $walas['nama'] ?></b></td>
            </tr>
			<tr>
               <td style="text-align: center" width="33.3%"></td>
                 <td width="33.3%"> </td>
				 
                <td  width="33.3%">NIP. <?= $walas['nip'] ?></td>
				 
            </tr>
			
			</table>
			
			 
			</table>
			<table style="margin-left: 80px;" width="100%">
            <tr>
                <td></td>
			<td>Mengetahui  :</td>
				 <td></td>
            </tr>
			
		      <tr>
               <td width="33.3%"></td>
                 <td  width="33.3%">
				<?php if($setting['jenis']=='REGULER'): ?>
                    <p>Kepala Sekolah,</p>
					<?php else: ?>
					<p>Kepala PKBM,</p>
					<?php endif; ?>
				 </td>
                <td  width="33.3%"></td>
            </tr>
			</table>
			
			<br><br><br>
			
			<table style="margin-left: 80px;" width="100%">
		<tr>
               <td width="33.3%"></td>
                 <td width="33.3%"><?= $setting['kepsek'] ?> </td>
                <td  width="33.3%"><b></b></td>
            </tr>
			<tr>
               <td style="text-align: center" width="33.3%"></td>
                 <td width="33.3%">NIP. <?= $setting['nip'] ?> </td>
				 
                <td  width="33.3%"></td>
				 
            </tr>
			
			</table>
		
       
    </div>
</body>

</html>
<?php

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

use Dompdf\Dompdf;

$dompdf = new Dompdf();
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$dompdf->stream("Raport_" . $siswa['nama'] . ".pdf", array("Attachment" => false));
exit(0);
?>