<?php if ($_GET[act] == '') {
  if (isset($_GET[gettanggal])) {
    $filtertgl = $_GET[gettanggal];
  } else {
    if (isset($_POST[tgl])) {
      $tgl = $_POST[tgl];
    } else {
      $tgl = date("d");
    }
    if (isset($_POST[bln])) {
      $bln = $_POST[bln];
    } else {
      $bln = date("m");
    }
    if (isset($_POST[thn])) {
      $thn = $_POST[thn];
    } else {
      $thn = date("Y");
    }
    $lebartgl = strlen($tgl);
    $lebarbln = strlen($bln);

    switch ($lebartgl) {
      case 1: {
          $tglc = "0" . $tgl;
          break;
        }
      case 2: {
          $tglc = $tgl;
          break;
        }
    }

    switch ($lebarbln) {
      case 1: {
          $blnc = "0" . $bln;
          break;
        }
      case 2: {
          $blnc = $bln;
          break;
        }
    }
    $filtertgl = $thn . "-" . $blnc . "-" . $tglc;
  }
  $ex = explode('-', $filtertgl);
  $tahun = $ex[0];
  $bulane = $ex[1];
  $tanggal = $ex[2];
  if (substr($tanggal, 0, 1) == '0') {
    $tgle = substr($tanggal, 1, 1);
  } else {
    $tgle = substr($tanggal, 0, 2);
  }
  if (substr($bulane, 0, 1) == '0') {
    $blnee = substr($bulane, 1, 1);
  } else {
    $blnee = substr($bulane, 0, 2);
  }
?>
  <div class="col-xs-12">
    <div class="box">
      <div class="box-header">
        <h3 class="box-title"><?php if (isset($_GET[kelas]) and isset($_GET[tahun])) {
                                echo "Absensi siswa";
                              } else {
                                echo "Absensi Siswa Pada Tahun " . date('Y');
                              } ?></h3>
        <form style='margin-right:5px; margin-top:0px' class='pull-right' action='' method='GET'>
          <input type="hidden" name='view' value='absensiswa'>
          <?php
          $tahun = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM tahun_ajaran where aktif='Y'"));
          $ta = $tahun['idTahunAjaran'];
          ?>

          <input type="hidden" class="form-control" name="tahun" value="<?php echo $ta; ?>">

          <select name='kelas' style='padding:4px'>
            <?php
            echo "<option value=''>- Pilih Kelas -</option>";
            $kelas = mysqli_query($conn, "SELECT * FROM kelas_siswa");
            while ($k = mysqli_fetch_array($kelas)) {
              if ($_GET[kelas] == $k[idKelas]) {
                echo "<option value='$k[idKelas]' selected>$k[idKelas] - $k[nmKelas]</option>";
              } else {
                echo "<option value='$k[idKelas]'>$k[idKelas] - $k[nmKelas]</option>";
              }
            }
            ?>
          </select>
          <input type="submit" style='margin-top:-4px' class='btn btn-success btn-sm' value='Lihat'>
        </form>

      </div><!-- /.box-header -->

      <div class="table-responsive">
        <table id="example" class="table table-bordered table-striped">
          <thead>
            <tr>
              <th style='width:20px'>No</th>
              <th>Nama Siswa</th>

              <th>Kelas</th>

              <th>Action</th>

            </tr>
          </thead>
          <tbody>
            <?php
            if (isset($_GET[kelas]) and isset($_GET[tahun])) {
              $tampil = mysqli_query($conn, "SELECT a.*, e.nmKelas FROM siswa a 
                                           
                                           
                                                  JOIN kelas_siswa e ON a.idKelas =e.idKelas 
                                                  where a.idKelas ='$_GET[kelas]' 
                                                   
                                                       ORDER BY nisnSiswa DESC");
            }
            $no = 1;
            while ($r = mysqli_fetch_array($tampil)) {
              if (isset($_GET[gettanggal])) {
                $sekarangabsen = $_GET[gettanggal];
              } else {
                if (isset($_POST[lihat])) {
                  $sekarangabsen = $thn . "-" . $blnc . "-" . $tglc;
                } else {
                  $sekarangabsen = date("Y-m-d");
                }
              }
              echo "<tr><td>$no</td>
							
                              <td>$r[nmSiswa]</td>
                              <td>$r[nmKelas]</td>
                            
                   
                              <form method='POST' class='form-horizontal' action='' enctype='multipart/form-data'>
                              <input type='hidden' name='tgla' value='$tglc'>
                              <input type='hidden' name='blna' value='$blnc'>
                              <input type='hidden' name='thna' value='$thn'>
                              <input type='hidden' name='kelas[$no]' value='$_GET[kelas]'>
                              <input type='hidden' value='$r[nisnSiswa]' name='nisn[$no]'>
                              <td><select style='width:100px;' name='a[$no]' class='form-control'>";
              $a = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM rb_absensi_siswa where nisn='$r[nisnSiswa]' AND tanggal='$sekarangabsen' "));

              $kehadiran = mysqli_query($conn, "SELECT * FROM rb_kehadiran");
              while ($k = mysqli_fetch_array($kehadiran)) {
                if ($a[kode_kehadiran] == $k[kode_kehadiran]) {
                  echo "<option value='$k[kode_kehadiran]' selected>* $k[nama_kehadiran]</option>";
                } else {
                  echo "<option value='$k[kode_kehadiran]'>$k[nama_kehadiran]</option>";
                }
              }



              $no++;
            }

            ?>
            <?php
            if ($_GET[tahun] == '' & $_GET[kelas] == '') {
              echo "<center style='padding:60px; color:red'>Silahkan klik lihat Terlebih dahulu...</center>";
            }
            ?>
          </tbody>
        </table>
      </div><!-- /.box-body -->

      <div class='box-footer'>
        <?php
        echo "<input type='hidden' name='filtertgl' value='$filtertgl'>";
        ?>
        <button type='submit' name='simpann' class='btn btn-info pull-right'>Simpan Absensi</button>
      </div>

      </form>
    </div><!-- /.box -->
  </div>
  <?php
  if (isset($_POST[simpann])) {
    $jml_data = count($_POST[nisn]);
    $nisn = $_POST[nisn];
    $a = $_POST[a];
    $kelas = $_POST[kelas];
    $e    = $_POST[thna];
    $f   = $_POST[blna];
    $g    = $_POST[tgla];



    for ($i = 1; $i <= $jml_data; $i++) {
      $cek = mysqli_query($conn, "SELECT * FROM rb_absensi_siswa where  nisn='" . $nisn[$i] . "' AND tanggal='" . $e . "-" . $f . "-" . $g . "'");
      $total = mysqli_num_rows($cek);

      if ($total >= 1) {
        mysqli_query($conn, "UPDATE rb_absensi_siswa SET kode_kehadiran = '" . $a[$i] . "' where nisn='" . $nisn[$i] . "' ");
        $lst_siswa = mysqli_query($conn, "SELECT * FROM view_detil_siswa where nisnSiswa='" . $nisn[$i] . "'");
        while ($cs = mysqli_fetch_array($lst_siswa)) {
          $cek = mysqli_query($conn, "SELECT * FROM rb_absensi_siswa where  nisn='" . $nisn[$i] . "' AND tanggal='" . $e . "-" . $f . "-" . $g . "' and kode_kehadiran!='H'");
          while ($cik = mysqli_fetch_array($cek)) {

            $idt = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM identitas"));
            $link = $idt['link_one_sender'];
            $links = $idt['token'];
            $wa = $idt['wa'];
            $ortu = $cs['nmOrtu'];
            $phone = $cs['noHpOrtu'];
            $cak = $cik['kode_kehadiran'];
            $waktu = tgl_indo($cik['tanggal']);
            $waktus = hari_ini($cik['tanggal']);

            if ($cak == 'A') {
              $statush = 'Alpa';
            } elseif ($cak == 'S') {
              $statush = 'Sakit';
            } elseif ($cak == 'I') {
              $statush = 'Izin';
            } elseif ($cak == 'T') {
              $statush = 'Terlambat';
            } else {
              $statush = 'Hadir';
            }
            $isi_pesan = "Diberitahukan kepada Yth Bpk/Ibk *$ortu*, Bahwa;

Nama Siswa : *$cs[nmSiswa]*
kelas: *$cs[nmKelas]* 
Hari ini : $waktus, $waktu 
Status : _*$statush*_

Terimakasih

*Admin $idt[nmSekolah]*";

            $data = [
              "api_key" => $links,
              "sender" => $wa,
              "number" => $phone,
              "message" => $isi_pesan
            ];
            $curl = curl_init();
            curl_setopt_array($curl, array(
              CURLOPT_URL => $idt['link_one_sender'],
              CURLOPT_RETURNTRANSFER => true,
              CURLOPT_SSL_VERIFYHOST => false,
              CURLOPT_SSL_VERIFYPEER => false,
              CURLOPT_MAXREDIRS => 10,
              CURLOPT_FOLLOWLOCATION => true,
              CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
              CURLOPT_CUSTOMREQUEST => 'POST',
              CURLOPT_POSTFIELDS => json_encode($data),
              CURLOPT_HTTPHEADER => array(
                'Content-Type: application/json',
              ),
            ));

            $response = curl_exec($curl);

            curl_close($curl);



            if ($cs[noHpOrtu] != '') {
              mysqli_query($conn, "INSERT INTO rb_whatsapp VALUES('','$cs[noHpOrtu]','$isi_pesan')");
            } elseif ($cs[no_telpon_ibu] != '') {
              mysqli_query($conn, "INSERT INTO rb_whatsapp VALUES('','$cs[noHpOrtu]','$isi_pesan')");
            }
          }
        }
      } else {
        mysqli_query($conn, "INSERT INTO rb_absensi_siswa VALUES('','" . $nisn[$i] . "','$kelas[$i]','" . $a[$i] . "','" . $e . "-" . $f . "-" . $g . "','" . date('Y-m-d H:i:s') . "','','','')");
        $lst_siswa = mysqli_query($conn, "SELECT * FROM view_detil_siswa where nisnSiswa='" . $nisn[$i] . "'");
        while ($cs = mysqli_fetch_array($lst_siswa)) {
          $cek = mysqli_query($conn, "SELECT * FROM rb_absensi_siswa where  nisn='" . $nisn[$i] . "' AND tanggal='" . $e . "-" . $f . "-" . $g . "'and kode_kehadiran!='H'");
          while ($cik = mysqli_fetch_array($cek)) {

            $idt = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM identitas"));
            $link = $idt['link_one_sender'];
            $links = $idt['token'];
            $wa = $idt['wa'];
            $ortu = $cs['nmOrtu'];
            $phone = $cs['noHpOrtu'];
            $cak = $cik['kode_kehadiran'];
            $waktu = tgl_indo($cik['tanggal']);
            $waktus = hari_ini($cik['tanggal']);
            if ($cak == 'A') {
              $statush = 'Alpa';
            } elseif ($cak == 'S') {
              $statush = 'Sakit';
            } elseif ($cak == 'I') {
              $statush = 'Izin';
            } elseif ($cak == 'T') {
              $statush = 'Terlambat';
            } else {
              $statush = 'Hadir';
            }
            $isi_pesan = "Diberitahukan kepada Yth Bpk/Ibk *$ortu*, Bahwa;

Nama Siswa : *$cs[nmSiswa]*
kelas: *$cs[nmKelas]* 
Hari ini : $waktus, $waktu 
Status : _*$statush*_

Terimakasih

*Admin $idt[nmSekolah]*";

            $data = [
              "api_key" => $links,
              "sender" => $wa,
              "number" => $phone,
              "message" => $isi_pesan
            ];
            $curl = curl_init();
            curl_setopt_array($curl, array(
              CURLOPT_URL => $idt['link_one_sender'],
              CURLOPT_RETURNTRANSFER => true,
              CURLOPT_SSL_VERIFYHOST => false,
              CURLOPT_SSL_VERIFYPEER => false,
              CURLOPT_MAXREDIRS => 10,
              CURLOPT_FOLLOWLOCATION => true,
              CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
              CURLOPT_CUSTOMREQUEST => 'POST',
              CURLOPT_POSTFIELDS => json_encode($data),
              CURLOPT_HTTPHEADER => array(
                'Content-Type: application/json',
              ),
            ));

            $response = curl_exec($curl);

            curl_close($curl);


            if ($cs[noHpOrtu] != '') {
              mysqli_query($conn, "INSERT INTO rb_whatsapp VALUES('','$cs[noHpOrtu]','$isi_pesan')");
            } elseif ($cs[no_telpon_ibu] != '') {
              mysqli_query($conn, "INSERT INTO rb_whatsapp VALUES('','$cs[noHpOrtu]','$isi_pesan')");
            }
          }
        }
      }
    }
    echo "<script>document.location='?view=absensiswa&tahun=" . $_GET[tahun] . "&kelas=" . $_GET[kelas] . "';</script>";
  }
} elseif ($_GET[act] == 'detailabsenguru') { ?>
  <div class="col-xs-12">
    <div class="box">
      <div class="box-header">
        <h3 class="box-title"><?php if (isset($_GET[tahun])) {
                                echo "Absensi Siswa";
                              } else {
                                echo "Absensi Siswa Pada " . date('Y');
                              } ?></h3>
        <form style='margin-right:5px; margin-top:0px' class='pull-right' action='' method='GET'>
          <input type="hidden" name='view' value='absensiswa'>
          <input type="hidden" name='act' value='detailabsenguru'>
          <select name='tahun' style='padding:4px'>
            <?php
            echo "<option value=''>- Pilih Tahun Akademik -</option>";
            $tahun = mysqli_query($conn, "SELECT * FROM rb_tahun_akademik");
            while ($k = mysqli_fetch_array($tahun)) {
              if ($_GET[tahun] == $k[id_tahun_akademik]) {
                echo "<option value='$k[id_tahun_akademik]' selected>$k[nama_tahun]</option>";
              } else {
                echo "<option value='$k[id_tahun_akademik]'>$k[nama_tahun]</option>";
              }
            }
            ?>
          </select>
          <input type="submit" style='margin-top:-4px' class='btn btn-success btn-sm' value='Lihat'>
        </form>

      </div><!-- /.box-header -->
      <div class="table-responsive">
        <table id="example1" class="table table-bordered table-striped">
          <thead>
            <tr>
              <th style='width:20px'>No</th>
              <th>Jadwal Pelajaran</th>
              <th>Kelas</th>
              <th>Guru</th>
              <th>Hari</th>
              <th>Mulai</th>
              <th>Semester</th>
              <th>Action</th>
            </tr>
          </thead>
          <tbody>
            <?php
            if (isset($_GET[tahun])) {
              $tampil = mysqli_query($conn, "SELECT a.*, e.nama_kelas, b.namamatapelajaran, b.kode_pelajaran, c.nama_guru FROM rb_jadwal_pelajaran a 
                                            JOIN rb_mata_pelajaran b ON a.kode_pelajaran=b.kode_pelajaran
                                              JOIN rb_guru c ON a.nip=c.nip 
                                                
                                                  JOIN rb_kelas e ON a.kode_kelas=e.kode_kelas 
                                                  where a.nip='$_SESSION[id]' AND a.id_tahun_akademik='$_GET[tahun]' ORDER BY a.hari DESC");
            } else {
              $tampil = mysqli_query($conn, "SELECT a.*, e.nama_kelas, b.namamatapelajaran, b.kode_pelajaran, c.nama_guru FROM rb_jadwal_pelajaran a 
                                            JOIN rb_mata_pelajaran b ON a.kode_pelajaran=b.kode_pelajaran
                                              JOIN rb_guru c ON a.nip=c.nip 
                                             
                                                JOIN rb_kelas e ON a.kode_kelas=e.kode_kelas 
                                                  where a.nip='$_SESSION[id]' AND a.id_tahun_akademik LIKE '" . date('Y') . "%' ORDER BY a.hari DESC");
            }
            $no = 1;
            while ($r = mysqli_fetch_array($tampil)) {
              echo "<tr><td>$no</td>
                              <td>$r[namamatapelajaran]</td>
                              <td>$r[nama_kelas]</td>
                              <td>$r[nama_guru]</td>
                              <td>$r[hari]</td>
                              <td>$r[jam_ke]</td>
                              <td>$r[id_tahun_akademik]</td>
                              <td><a class='btn btn-success btn-xs' title='Tampil List Absensi' href='?view=absensiswa&act=tampilabsen&id=$r[kode_kelas]&kd=$r[kode_pelajaran]&jdwl=$r[kodejdwl]'><span class='glyphicon glyphicon-th'></span> Tampilk Absensi</a></td>
                          </tr>";
              $no++;
            }
            ?>
          </tbody>
        </table>
      </div><!-- /.box-body -->
    </div>
  </div>
  </div>

<?php
} elseif ($_GET[act] == 'detailabsensiswa') {
  echo "<div class='col-xs-12'>  
              <div class='box'>
                <div class='box-header'>
                  <h3 class='box-title'>Data Absensi Siswa untuk Mata Pelajaran yang di Ampu</h3>
                </div>
                <div class='box-body'>
                <b class='semester'>SEMESTER 1</b>
                <table class='table table-bordered table-striped'>
                      <tr>
                        <th style='width:40px'>No</th>
                        <th>Kode Pelajaran</th>
                        <th>Nama Pelajaran</th>
                        <th>Kelas</th>
                        <th>Hari</th>
                        <th>Jam Mulai</th>
                        <th>Jam Selesai</th>
                        <th>Action</th>
                      </tr>";
  $tampil = mysqli_query($conn, "SELECT * FROM rb_jadwal_pelajaran a 
                                            JOIN rb_mata_pelajaran b ON a.kodepelajaran=b.kodepelajaran
                                              JOIN rb_guru c ON a.nip=c.nip 
                                                JOIN rb_kelas d ON a.kodekelas=d.kodekelas where a.kodekelas='$iden[kodekelas]' AND a.semester='1' ORDER BY a.hari DESC");
  $no = 1;
  while ($r = mysqli_fetch_array($tampil)) {
    echo "<tr><td>$no</td>
                              <td>$r[kodepelajaran]</td>
                              <td>$r[namamatapelajaran]</td>
                              <td>$r[kelas]</td>
                              <td>$r[hari]</td>
                              <td>$r[jam_mulai] WIB</td>
                              <td>$r[jam_selesai] WIB</td>
                              <td style='width:70px !important'><center>
                                <a class='btn btn-success btn-xs' title='Tampil List Absensi' href='?view=absensiswa&act=tampilabsen&id=$r[kodekelas]&kd=$r[kodepelajaran]&jdwl=$r[kodejdwl]'><span class='glyphicon glyphicon-th'></span> Tampilkan Absensi</a>
                              </center></td>";
    echo "</tr>";
    $no++;
  }

  echo "</table><br>
                  
                  <b class='semester'>SEMESTER 2</b>
                  <table class='table table-bordered table-striped'>
                      <tr>
                        <th style='width:40px'>No</th>
                        <th>Kode Pelajaran</th>
                        <th>Nama Pelajaran</th>
                        <th>Kelas</th>
                        <th>Hari</th>
                        <th>Jam Mulai</th>
                        <th>Jam Selesai</th>
                        <th>Action</th>
                      </tr>";
  $tampil = mysqli_query($conn, "SELECT * FROM rb_jadwal_pelajaran a 
                                            JOIN rb_mata_pelajaran b ON a.kodepelajaran=b.kodepelajaran
                                              JOIN rb_guru c ON a.nip=c.nip 
                                                JOIN rb_kelas d ON a.kodekelas=d.kodekelas where a.kodekelas='$iden[kodekelas]' AND a.semester='2' ORDER BY a.hari DESC");
  $no = 1;
  while ($r = mysqli_fetch_array($tampil)) {

    echo "<tr><td>$no</td>
                              <td>$r[kodepelajaran]</td>
                              <td>$r[namamatapelajaran]</td>
                              <td>$r[kelas]</td>
                              <td>$r[hari]</td>
                              <td>$r[jam_mulai] WIB</td>
                              <td>$r[jam_selesai] WIB</td>
                              <td style='width:70px !important'><center>
                                <a class='btn btn-success btn-xs' title='Tampil List Absensi' href='?view=absensiswa&act=tampilabsen&id=$r[kodekelas]&kd=$r[kodepelajaran]'><span class='glyphicon glyphicon-th'></span> Tampilkan Absensi</a>
                              </center></td>";
    echo "</tr>";
    $no++;
  }

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