<html>

<head>
  <title>Cetak - Laporan Absensi Siswa</title>
  <link rel="stylesheet" href="bootstrap/css/printer.css">
</head>

<body>
  <?php
  session_start();
  error_reporting(0);
  include "config/koneksi.php";
  include "config/fungsi_indotgl.php";


  $tgl_mulai = $_GET['tgl_mulai'];
  $tgl_akhir = $_GET['tgl_akhir'];


  $idKelas = $_GET['kelas'];
  if ($_GET['kelas'] != 'all') {
    $kls = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM kelas_siswa WHERE idKelas='$idKelas'"));
    $kelas = 'Kelas ' . $kls['nmKelas'];
  } else {
    $kelas = 'Semua Kelas';
  }
  $idt = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM identitas "));

  $d = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM kelas_siswa where idKelas='$_GET[kelas]'"));
  ?>
  <div class='col-md-12'>
    <div class='box box-info'>
      <div class='box-header with-border'>
        <h3 align='center'>Rekap Data Absensi Siswa </b></h3>
      </div>
      <div class='table-responsive'>

        <div class='col-md-12'>

          <input type='hidden' name='kelas' value='$s[idKelas]'>
          <h4 class='box-title'>Nama Kelas :</th>
            <td><?php echo  $kelas; ?>
          </h4>
          <h4 class='box-title'>Dari Tanggal : <?php echo  tgl_indo($tgl_mulai); ?> Sampai <?php echo   tgl_indo($tgl_akhir); ?>

          </h4>

        </div>

        <?php
        $tgl_mulai = $_GET['tgl_mulai'];
        $tgl_akhir = $_GET['tgl_akhir'];


        $idKelas = $_GET['kelas'];


        $ta = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM tahun_ajaran WHERE idTahunAjaran='$idTahunAjaran'"));
        if ($idKelas != 'all') {
          $kls = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM kelas_siswa WHERE idKelas='$idKelas'"));
          $kelas = 'Kelas ' . $kls['nmKelas'];
        } else {
          $kelas = 'Semua Kelas';
        }


        ?>

      </div><!-- /.box-header -->
      <div class="table-responsive">
        <table id="example" class="table table-bordered table-striped">
          <thead>
            <tr>
              <th>No</th>
              <th>Nama Siswa</th>
              <th>Kelas</th>
              <th>Kode Kehadiran</th>
              <th>Waktu Input</th>
              <th>Alasan</th>
              <th>Lokasi</th>
              <th>Lampiran</th>
              <th>Foto Selfie</th>

            </tr>
           
          </thead>
          <tbody>
            <?php

            if ($idKelas == 'all') {
              $tampil = mysqli_query($conn, "SELECT nmSiswa,nmKelas,waktu_input,nama,latlng,address,keterangan,kode_kehadiran FROM rb_absensi_siswa 
              INNER JOIN view_detil_siswa ON rb_absensi_siswa.nisn = view_detil_siswa.nisnSiswa
              where DATE(tanggal) BETWEEN '$tgl_mulai' AND '$tgl_akhir' and kode_kehadiran!='H' ");
            } else {
              $tampil = mysqli_query($conn, "SELECT  nmSiswa,nmKelas,waktu_input,nama,latlng,address,keterangan,kode_kehadiran FROM rb_absensi_siswa 
              INNER JOIN view_detil_siswa ON rb_absensi_siswa.nisn = view_detil_siswa.nisnSiswa
              where rb_absensi_siswa.idKelas='$idKelas' and DATE(tanggal) BETWEEN '$tgl_mulai' AND '$tgl_akhir' and kode_kehadiran!='H'");
            }

            $no = 1;
            while ($r = mysqli_fetch_array($tampil)) {
             
              $total = mysqli_num_rows(mysqli_query($conn, "SELECT * FROM `rb_absensi_siswa` where DATE(tanggal) BETWEEN '$tgl_mulai' AND '$tgl_akhir' GROUP BY tanggal"));
              if ($total == 'null') {
                $sw = "<center style='padding:60px; color:red'>Maaf Tidak ada data...</center>";
              } else {

                $hadir = mysqli_num_rows(mysqli_query($conn, "SELECT * FROM `rb_absensi_siswa` where DATE(tanggal) BETWEEN '$tgl_mulai' AND '$tgl_akhir' AND kode_kehadiran='H'"));
                $sakit = mysqli_num_rows(mysqli_query($conn, "SELECT * FROM `rb_absensi_siswa` where DATE(tanggal) BETWEEN '$tgl_mulai' AND '$tgl_akhir' AND kode_kehadiran='S'"));
                $izin = mysqli_num_rows(mysqli_query($conn, "SELECT * FROM `rb_absensi_siswa` where  DATE(tanggal) BETWEEN '$tgl_mulai' AND '$tgl_akhir' AND kode_kehadiran='I'"));
                $alpa = mysqli_num_rows(mysqli_query($conn, "SELECT * FROM `rb_absensi_siswa` where  DATE(tanggal) BETWEEN '$tgl_mulai' AND '$tgl_akhir' AND kode_kehadiran='A'"));
                $telat = mysqli_num_rows(mysqli_query($conn, "SELECT * FROM `rb_absensi_siswa` where  DATE(tanggal) BETWEEN '$tgl_mulai' AND '$tgl_akhir' AND kode_kehadiran='T'"));
                //hitung keseluruhan siswa
                if ($idKelas == 'all') {
                    $sakits = mysqli_num_rows(mysqli_query($conn, "SELECT * FROM `rb_absensi_siswa` where DATE(tanggal) BETWEEN '$tgl_mulai' AND '$tgl_akhir' AND kode_kehadiran='S'"));
                  $izins = mysqli_num_rows(mysqli_query($conn, "SELECT * FROM `rb_absensi_siswa` where  DATE(tanggal) BETWEEN '$tgl_mulai' AND '$tgl_akhir'  AND kode_kehadiran='I'"));
                  $alpas = mysqli_num_rows(mysqli_query($conn, "SELECT * FROM `rb_absensi_siswa` where  DATE(tanggal) BETWEEN '$tgl_mulai' AND '$tgl_akhir'  AND kode_kehadiran='A'"));
                  $telats = mysqli_num_rows(mysqli_query($conn, "SELECT * FROM `rb_absensi_siswa` where  DATE(tanggal) BETWEEN '$tgl_mulai' AND '$tgl_akhir' AND kode_kehadiran='T'"));
                  $totals = mysqli_num_rows(mysqli_query($conn, "SELECT * FROM `rb_absensi_siswa` where DATE(tanggal) BETWEEN '$tgl_mulai' AND '$tgl_akhir' and kode_kehadiran!='H' "));
                } else {
                    $sakits = mysqli_num_rows(mysqli_query($conn, "SELECT * FROM `rb_absensi_siswa` where DATE(tanggal) BETWEEN '$tgl_mulai' AND '$tgl_akhir' AND idKelas='$_GET[kelas]' AND kode_kehadiran='S'"));
                  $izins = mysqli_num_rows(mysqli_query($conn, "SELECT * FROM `rb_absensi_siswa` where  DATE(tanggal) BETWEEN '$tgl_mulai' AND '$tgl_akhir' AND idKelas='$_GET[kelas]' AND kode_kehadiran='I'"));
                  $alpas = mysqli_num_rows(mysqli_query($conn, "SELECT * FROM `rb_absensi_siswa` where  DATE(tanggal) BETWEEN '$tgl_mulai' AND '$tgl_akhir' AND idKelas='$_GET[kelas]' AND kode_kehadiran='A'"));
                  $telats = mysqli_num_rows(mysqli_query($conn, "SELECT * FROM `rb_absensi_siswa` where  DATE(tanggal) BETWEEN '$tgl_mulai' AND '$tgl_akhir' AND idKelas='$_GET[kelas]' AND kode_kehadiran='T'"));
                  $totals = mysqli_num_rows(mysqli_query($conn, "SELECT * FROM `rb_absensi_siswa` where DATE(tanggal) BETWEEN '$tgl_mulai' AND '$tgl_akhir' AND idKelas='$_GET[kelas]' and kode_kehadiran!='H'  "));
                }
                $tambah = $hadir + $telat;
                $persen = $tambah / ($total) * 100;
                $data = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM `rb_absensi_siswa` where DATE(tanggal) BETWEEN '$tgl_mulai' AND '$tgl_akhir'"));
                if (!file_exists("foto_absen/$r[nama]") or $r['nama'] == '') {
                  $foto_user = "blank.png";
                } else {
                  $foto_user = $r['nama'];
                }


                echo "<tr bgcolor=$warna>
                <td >$no</td>
              
                <td>$r[nmSiswa]</td>
                <td>$r[nmKelas]</td>
                <td align='center'>$r[kode_kehadiran]</td>
               
                <td>$r[waktu_input]</td>
                <td>$r[keterangan]</td>
                <td id='map' >" . $r['latlng'] . "</td>
                <td align=center><img src=" . './foto_absen/' . $r['address'] . " id='target'  width='100' height='100' class='img-thumbnail img-responsive'>
                </td>
               
               
                <td align=center><img src=" . './foto_absen/' . $foto_user . " id='target'  width='100' height='100' class='img-thumbnail img-responsive'>
                </td>
               ";
                echo " 
    </tr>
    ";
                $no++;
              }
            }
            echo "</tbody></table><tr>
            <td colspan='1'><b>Keterangan</b></td>
            
          </tr>
          <table id='example' class='table table-bordered table-striped' width='40%'>
          
          <tr>
            <td colspan='1'>Siswa Izin</td>
            <td > $izins</td>
          </tr>
          <tr>
            <td colspan='1'>Siswa Sakit</td>
            <td > $sakits</td>
          </tr>
          <tr>
            <td colspan='1'>Siswa Alpha</td>
            <td > $alpas</td>
          </tr>
          <tr>
          <td colspan='1'>Siswa Terlambat</td>
          <td > $telats</td>
        </tr>
          <tr>
          <td colspan='1'><b>Jumlah Total</b></td>
          <td >  $totals</td>
        </tr>
          ";
            echo " ";
            ?>

          </tbody>
        </table>
      </div><!-- /.box-body -->
      <?php

      echo $sw;

      ?>
    </div>
  </div>
  <br>
  <table width="100%">
    <tr>
      <td align="center"></td>
      <td align="center" width="400px">
        Banyuwangi, <?php echo tgl_raport(date("Y-m-d")); ?>
        <br><br><br><br><br><br>
        <b> <?php echo $idt['nmKepsek']; ?></b>

      </td>
    </tr>
  </table>
</body>
<script>
  window.print()
</script>

</html>