<?php
session_start();
error_reporting(0);
include "config/koneksi.php";
include "config/fungsi_indotgl.php";
$abjad = range('A', 'Z');
?>

<head>
  <title>Cetak - Raport Siswa</title>
  <link rel="stylesheet" href="bootstrap/css/printer.css">
  <style type="text/css">
    td {
      line-height: 1.3em;
      font-size: 14px;
      font-family: 'Calibri', 'Gill Sans MT', 'Trebuchet MS', sans-serif;
    }

    h2 {
      font-family: 'Calibri', 'Gill Sans MT', 'Trebuchet MS', sans-serif;
    }
  </style>
</head>

<body>
  <?php
  $idt = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM identitas "));
  $t = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM tahun_ajaran where aktif='Y'"));
  $s = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM view_detil_siswa where nisnSiswa='$_GET[nisn]'"));
  if (substr($_GET[tahun], 4, 5) == '1') {
    $semester = 'Ganjil';
  } else {
    $semester = 'Genap';
  }
  $idt = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM identitas "));
  echo "<table width=100%>
        <tr><td width=140px>NAMA</td> <td> : ".$s['nmSiswa']."</td>       <td width=140px>Madrasah </td>   <td>: ".$idt['nmSekolah']."</td></tr>
        <tr><td>NIS</td>                   <td> : ".$s['nisSiswa']." </td>          <td>Kelas/Semester </td> <td>: ".$s['nmKelas']."/".$semester."</td></tr>
        <tr><td>NISN</td>       <td> : ".$s['nisnSiswa']." </td>           <td>Tahun Pelajaran </td> <td>: ".$t['nmTahunAjaran']."</td></tr>
       
      </table><hr> <h2 align='center' >CAPAIAN HASIL BELAJAR</h2>";
  
  echo "<table class='table table-bordered table-striped'  width=100% id='tablemodul1'>
          <tr>
            <th colspan='2' rowspan='2'>Mata Pelajaran</th>
           
            <th colspan='2' style='text-align:center'>Pengetahuan KI(3)</th>
            <th colspan='2' style='text-align:center'>Keterampilan KI(4)</th>
          </tr>
          <tr>
            <th>Nilai</th>
            <th  width='80px'>Predikat</th>
            <th>Nilai</th>
            <th  width='80px'>Predikat</th>
          </tr>";
  $kelompok = mysqli_query($conn, "SELECT * FROM rb_kelompok_mata_pelajaran ORDER BY id_kelompok_mata_pelajaran ASC");
  while ($k = mysqli_fetch_array($kelompok)) {
    echo "<tr>
            <td colspan='7'><b>".$k['nama_kelompok_mata_pelajaran']."</b></td>
          </tr>";
    $no = 1;
    $mapel = mysqli_query($conn, "SELECT * FROM rb_nilai WHERE id_kelompok_mapel='$k[id_kelompok_mata_pelajaran]' AND kode <> '' ORDER BY kode ASC");
    while ($m = mysqli_fetch_array($mapel)) {

      $grademapel_peng = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM rb_predikat where (" . number_format($m['nilai_peng']) . " >=nilai_a) AND (" . number_format($m['nilai_peng']) . " <= nilai_b) "));
      $grademapel_ket = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM rb_predikat where (" . number_format($m['nilai_ket']) . " >=nilai_a) AND (" . number_format($m['nilai_ket']) . " <= nilai_b) "));
      $detmapel = mysqli_query($conn, "SELECT * FROM rb_nilai WHERE paralel='$m[kode]' ORDER BY nama_mapel ASC");
      $hitdetmapel = mysqli_num_rows($detmapel);
      if ($hitdetmapel == 0){
        echo "<tr>
                <td align=center>".$no."</td>
                <td>".$m["nama_mapel"]."</td>
                <td align=center>" . number_format($m['nilai_peng']) . "</td>
                <td align=center>".$grademapel_peng['grade']."</td>
                <td align=center>" . number_format($m['nilai_ket']) . "</td>
                <td align=center>".$grademapel_ket['grade']."</td>
            </tr>";
      }else{
        echo "<tr>
                <td align=center>".$no."</td>
                <td>".$m["nama_mapel"]."</td>
                <td align=center>" . number_format($m['nilai_peng']) . "</td>
                <td align=center>".$grademapel_peng['grade']."</td>
                <td align=center>" . number_format($m['nilai_ket']) . "</td>
                <td align=center>".$grademapel_ket['grade']."</td>
            </tr>";
        $urut = 0;
        while ($dm = mysqli_fetch_array($detmapel)) {
          $gradedetmapel_peng = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM rb_predikat where (" . number_format($dm['nilai_peng']) . " >=nilai_a) AND (" . number_format($dm['nilai_peng']) . " <= nilai_b) "));
          $gradedetmapel_ket = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM rb_predikat where (" . number_format($dm['nilai_ket']) . " >=nilai_a) AND (" . number_format($dm['nilai_ket']) . " <= nilai_b) "));
      
          echo "<tr>
                <td align=center></td>
                <td>".$abjad[$urut++].". ".$dm['nama_mapel']."</td>
                <td align=center>" . number_format($dm['nilai_peng']) . "</td>
                <td align=center>".$gradedetmapel_peng['grade']."</td>
                <td align=center>" . number_format($dm['nilai_ket']) . "</td>
                <td align=center>".$gradedetmapel_ket['grade']."</td>
            </tr>";

        }
      }
      $no++;
    }
  }

  //rata-rata perkolom
  $ratpen = mysqli_fetch_array(mysqli_query($conn, "SELECT sum((nilai_peng)/1)/count(nisn) as raport FROM rb_nilai where nisn='$_GET[nisn]'"));
  $ratket = mysqli_fetch_array(mysqli_query($conn, "SELECT sum((nilai_ket)/1)/count(nisn) as raport FROM rb_nilai where nisn='$_GET[nisn]'"));    

  echo "</table><br/>";

  $kkm = ($ratpen['raport'] + $ratket['raport']) / 2;
  
  $cekpredikat1 = mysqli_num_rows(mysqli_query($conn, "SELECT * FROM rb_predikat "));
  if ($cekpredikat1 >= 1) {
    $grade = mysqli_query($conn, "SELECT * FROM rb_predikat order by id_predikat DESC");
    $gradea = mysqli_query($conn, "SELECT * FROM rb_predikat order by id_predikat DESC");
    $total = mysqli_num_rows($grade);
  } else {
    $grade = mysqli_query($conn, "SELECT * FROM rb_predikat order by id_predikat DESC");
    $gradea = mysqli_query($conn, "SELECT * FROM rb_predikat order by id_predikat DESC");
    $total = mysqli_num_rows($grade);
  }
  echo "<table width='70%' class='table-bordered table-striped' border='1' >
              <tr>
                  <th rowspan='2' width='160px'>KKM</th>
                  <th colspan='".$total."'>Predikat</th>
              </tr>
              <tr>";
  while ($g = mysqli_fetch_array($grade)) {
    echo "<th width='120px'>".$g['grade']."</th>";
  }
  echo "</tr>
              <tr>
              <td align=center>".number_format($kkm)."</th>";
  while ($p = mysqli_fetch_array($gradea)) {
    echo "<td align=center>".$p['nilai_a']." - ".$p['nilai_b']."</td>";
  }
  echo "</tr>
          </table><br>";
  ?>

  <table border=0 width=100%>
    <tr>
      <td width="520" align="left">Mengetahui <br> Kepala <?php echo $idt['nmSekolah']; ?></td>


      <td width="260" align="left"><?php echo $idt['kabupaten']; ?>, <?php echo tgl_raport(date("Y-m-d")); ?> <br> Wali Kelas</td>
    </tr>
    <tr>
      <td align="left" valign="top"><br /><br /><br /><br /><br />
        <b><?php echo $idt['nmKepsek']; ?><br>
          NIP : <?php echo $idt['nipKepsek']; ?></b>
      </td>



      <td align="left" valign="top"><br /><br /><br /><br /><br />
        <b><?php echo $s['walikelas']; ?><br />
          NIP : <?php echo $s['nip']; ?></b>
      </td>
    </tr>
  </table>
</body>
<script>
		window.print()
	</script>