<?php
session_start();
error_reporting(0);
include "config/koneksi.php";
include "config/library.php";
include "config/fungsi_indotgl.php";
include "config/fungsi_seo.php";
if (isset($_SESSION[id])) {
  if ($_SESSION['level'] == 'admin') {
    $iden = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM users where username='$_SESSION[id]'"));
    $nama =  $iden['nama_lengkap'];
    $level = 'Administrator';
    $foto = 'dist/img/user.png';
  }

  $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';
  }
  $d = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM kelas_siswa where idKelas='$_GET[kelas]'"));



  header("Content-type: application/vnd-ms-excel");
  header("Content-Disposition: attachment; filename=laporan_absen_" . date('dmyHis') . ".xls");
?>
  <?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';
  }


  ?>
  <table border="1">

    <h3 align='center'>Rekap Data Absensi Siswa </b></h3>

    <h3></h3>
    <h4 class='box-title'>Nama Kelas : <?php echo  $kelas; ?>

    </h4>
    <h4 class='box-title'>Dari Tanggal : <?php echo  tgl_indo($tgl_mulai); ?> Sampai <?php echo   tgl_indo($tgl_akhir); ?>

    </h4>


    <thead>
      <tr>
        <th>No</th>
        <th>Kelas</th>
        <th>Nama Siswa</th>
        <th>Pertemuan</th>
        <th>Hadir</th>
        <th>Sakit</th>
        <th>Izin</th>
        <th>Alpa</th>
        <th>Terlambat</th>
        <th>
          <center>% Kehadiran</center>
        </th>
      </tr>
    </thead>
    <tbody>
      <?php

      if ($idKelas == 'all') {
        $tampil = mysqli_query($conn, "SELECT * FROM siswa  ORDER BY idSiswa");
      } else {
        $tampil = mysqli_query($conn, "SELECT * FROM siswa  where idKelas='$idKelas' ORDER BY idSiswa");
      }

      $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 nisn='$r[nisnSiswa]' 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  nisn='$r[nisnSiswa]' 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 nisn='$r[nisnSiswa]' 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 nisn='$r[nisnSiswa]' 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 nisn='$r[nisnSiswa]' AND kode_kehadiran='T'"));
          //hitung keseluruhan siswa
          if ($idKelas == 'all') {
            $hadirs = mysqli_num_rows(mysqli_query($conn, "SELECT * FROM `rb_absensi_siswa` where DATE(tanggal) BETWEEN '$tgl_mulai' AND '$tgl_akhir'  AND kode_kehadiran='H'"));
            $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'  "));
          } else {
            $hadirs = 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'"));
            $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]' "));
          }
          $tambah = $hadir + $telat;
          $persen = $tambah / ($total) * 100;
          echo "<tr bgcolor=$warna>
                <td>$no</td>
                <td>$r[nisnSiswa]</td>
                <td>$r[nmSiswa]</td>
                <td align=center>$total</td>
                <td align=center>$hadir</td>
                <td align=center>$sakit</td>
                <td align=center>$izin</td>
                <td align=center>$alpa</td>
                <td align=center>$telat</td>
                <td align=right>" . number_format($persen, 2) . " %</td>";
          echo " 
    </tr>
    ";
          $no++;
        }
      }
      echo "</tbody></table><table  border='1'> <tr>
<td colspan='1'><b>Keterangan</b></td>

</tr>
<tr>
<td colspan='1'>Siswa Hadir</td>
<td > $hadirs</td>
</tr>
<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>
<?php
} else {
  include "login.php";
}
?>