<?php if ($_GET[act] == '') { ?>
  <div class="col-xs-12">
    <div class="box">
      <div class="box-header">

        <form method="GET">

          <input type="hidden" name="view" value="<?= $_GET[view] ?>">
          <div class="row">
            <div class="col-md-2">
              <label>Dari Tanggal</label>
              <div class="input-group date date-picker" data-date="" data-date-format="yyyy-mm-dd">
                <span class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></span>
                <input class="form-control" type="text" name="tgl_mulai" readonly="readonly" placeholder="Tanggal Awal" value="<?= $_GET['tgl_mulai'] ?>">
              </div>
            </div>
            <div class="col-md-2">
              <label>Sampai Tanggal</label>
              <div class="input-group date date-picker" data-date="" data-date-format="yyyy-mm-dd">
                <span class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></span>
                <input class="form-control" type="text" name="tgl_akhir" readonly="readonly" placeholder="Sampai Tanggal" value="<?= $_GET['tgl_akhir'] ?>">
              </div>
            </div>



            <input type="hidden" name="kelas" id="idKelas" value="<?= $_SESSION[kls] ?>">



            <div class="col-md-2">
              <div style="margin-top:25px;">
                <button type="submit" class="btn btn-primary"><i class="fa fa-search"></i>Cari</button>
              </div>
            </div>
          </div>
          <br>

        </form>
      <?php } ?>
      <?php if (isset($_GET['tgl_mulai']) && isset($_GET['tgl_akhir'])  && isset($_GET['kelas'])) {  ?>


        <?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>Waktu Input</th>
              <th>Tempat Absen</th>
              <th>Alasan</th>

              <th>Lokasi</th>
              <th>Foto</th>

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

            if ($idKelas == 'all') {
              $tampil = mysqli_query($conn, "SELECT * FROM siswa  ORDER BY idSiswa");
            } else {
              $tampil = mysqli_query($conn, "SELECT  nmSiswa,nmKelas,waktu_input,nama,latlng,address,keterangan 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 rb_absensi_siswa.nisn='$_SESSION[ids]' ");
            }

            $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' AND nisn='$_SESSION[ids]' 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='$_SESSION[ids]' 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='$_SESSION[ids]' 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='$_SESSION[ids]' 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='$_SESSION[ids]' 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='$_SESSION[ids]' 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 nisn='$_SESSION[ids]' 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 nisn='$_SESSION[ids]' 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 nisn='$_SESSION[ids]'  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 nisn='$_SESSION[ids]'  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 nisn='$_SESSION[ids]' 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 nisn='$_SESSION[ids]' "));
                } 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 nisn='$_SESSION[ids]' 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 nisn='$_SESSION[ids]' 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 nisn='$_SESSION[ids]'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 nisn='$_SESSION[ids]' 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 nisn='$_SESSION[ids]' 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 nisn='$_SESSION[ids]' "));
                }
                $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' AND nisn='$_SESSION[ids]' AND kode_kehadiran='I'"));
                if (!file_exists("images/$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>$r[waktu_input]</td>
                            <td>$r[address]</td>
                            <td>$r[keterangan]</td>
                            
                            <td id='map'>" . $r['latlng'] . "</td>
                           
                            <td align=center><img src=" . './images/' . $foto_user . " id='target'  width='100' height='100' class='img-thumbnail img-responsive'>
                            </td>
                           ";
                echo " 
                </tr>
                ";
                $no++;
              }
            }
            echo "</tbody></table>
            <table id='example' class='table table-bordered table-striped' width='40%'>
          <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> Total Pertemuan</b></td>
          <td >  $totals</td>
        </tr>
          ";
            echo " </tbody>
            </table><h3 class='box-title'></h3> <a class='pull-right btn btn-warning btn-sm' style='margin-right:5px' target='_blank' href='cetak_absens.php?tgl_mulai=$_GET[tgl_mulai]&tgl_akhir=$_GET[tgl_akhir]&kelas=$_GET[kelas]'><i class='fa fa-print'></i> Cetak Rekap Absen</a>
            <h3 class='box-title'></h3> <a class='pull-right btn btn-success btn-sm' style='margin-right:5px' target='_blank' href='excel_absens.php?tgl_mulai=$_GET[tgl_mulai]&tgl_akhir=$_GET[tgl_akhir]&kelas=$_GET[kelas]'><i class='fa fa-file-excel-o'></i> Excel Rekap Absen</a>
           <br><br> ";
            ?>


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

        echo $sw;

      ?>
    </div>
  </div>
<?php
      } elseif ($_GET[act] == 'tampilabsen') {
        $d = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM kelas_siswa where idKelas='$_GET[id]'"));
        $m = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM rb_mata_pelajaran where kode_pelajaran='$_GET[kd]'"));
        $n = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM rb_jadwal_pelajaran where "));
        echo "<div class='col-md-12'>
              <div class='box box-info'>
                <div class='box-header with-border'>
                  <h3 class='box-title'>Rekap Data Absensi Siswa </b></h3> <a class='pull-right btn btn-success btn-sm' style='margin-right:5px' target='_blank' href='cetak_absen.php?id=$_GET[id]&kd=$_GET[kd]&jdwl=$_GET[jdwl]&tahun=$_GET[tahun]'><i class='fa fa-print'></i> Cetak Rekap Absen</a>
                </div>
              <div class='table-responsive'>

              <div class='col-md-12'>
              <table class='table table-condensed table-hover'>
                  <tbody>
                    <input type='hidden' name='id' value='$s[idKelas]'>
                   
                    <tr><th width='120px' scope='row'>Nama Kelas</th> <td>$d[nmKelas]</td></tr>
                    <tr><th scope='row'>Mata Pelajaran</th>           <td>$m[namamatapelajaran]</td></tr>
                  </tbody>
              </table>
              </div>

              <form method='POST' class='form-horizontal' action='' enctype='multipart/form-data'>
                <div class='col-md-12'>
                  <table class='table table-condensed table-bordered table-striped'>
                      <thead>
                      <tr>
                        <th>No</th>
                        <th>NIPD</th>
                        <th>Nama Siswa</th>
                        <th>Pertemuan</th>
                        <th>Hadir</th>
                        <th>Sakit</th>
                        <th>Izin</th>
                        <th>Alpa</th>
                        <th><center>% Kehadiran</center></th>
                      </tr>
                    </thead>
                    <tbody>";

        $no = 1;
        $tampil = mysqli_query($conn, "SELECT * FROM siswa a where a.idKelas='$_GET[id]' ORDER BY a.idSiswa");
        while ($r = mysqli_fetch_array($tampil)) {
          $total = mysqli_num_rows(mysqli_query($conn, "SELECT * FROM `rb_absensi_siswa` where  GROUP BY tanggal"));
          $hadir = mysqli_num_rows(mysqli_query($conn, "SELECT * FROM `rb_absensi_siswa` where  AND nisn='$_SESSION[ids]' AND kode_kehadiran='H'"));
          $sakit = mysqli_num_rows(mysqli_query($conn, "SELECT * FROM `rb_absensi_siswa` where  AND nisn='$_SESSION[ids]' AND kode_kehadiran='S'"));
          $izin = mysqli_num_rows(mysqli_query($conn, "SELECT * FROM `rb_absensi_siswa` where  AND nisn='$_SESSION[ids]' AND kode_kehadiran='I'"));
          $alpa = mysqli_num_rows(mysqli_query($conn, "SELECT * FROM `rb_absensi_siswa` where  AND nisn='$_SESSION[ids]' AND kode_kehadiran='A'"));
          $persen = $hadir / ($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=right>" . number_format($persen, 2) . " %</td>";
          echo "</tr>";
          $no++;
        }

        echo "</tbody>
                  </table>
                </div>
              </div>
            </div>";
      }
?>