<?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">Data Absensi Guru Pada : <b style='color:red'><?php echo tgl_indo("$filtertgl") . "</b>"; ?> </h3>

        <div style="clear:both"></div>

        <?php
        echo "<div class='col-md-7 pull-right' style='margin:5px -14px 5px 0px'>
                 </div>";
        ?>
      </div><!-- /.box-header -->
      <form action='' method='POST'>
        <div class="box-body">
          <table id="example" class="table table-bordered table-striped">
            <thead>
              <tr>
                <th style='width:20px'>No</th>
                <th>Guru</th>
                <th>Hari</th>
                <th width='90px'>Kehadiran</th>
              </tr>
            </thead>
            <tbody>
              <?php
              if (isset($_GET[tahun])) {
                $tampil = mysqli_query($conn, "SELECT * FROM rb_guru  
                                                 
                                                       ORDER BY nama_guru ASC");
              }
              $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");
                  }
                }

                $a = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM rb_absensi_guru where nip='$r[nip]' AND tanggal='$sekarangabsen' "));
                echo "<tr><td>$no</td>
                              
                              <td>$r[nama_guru]</td>
                              <td>$hari_ini</td>
                             
                              <input type='hidden' value='$r[nip]' name='nip[$no]'>
                             
                              <td><select style='width:100px;' name='a[$no]' class='form-control'>";
                $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>";
                  }
                }
                echo "</select></td>
                                    
                              </tr>";
                $no++;
              }
              ?>
            </tbody>
          </table>
        </div><!-- /.box-body -->
        <?php
        if ($_GET[tahun] == '') {
          echo "<center style='padding:60px; color:red'>Silahkan klik lihat Terlebih dahulu...</center>";
        }
        ?>
        <div class='box-footer'>
          <?php
          echo "<input type='hidden' name='filtertgl' value='$filtertgl'>";
          ?>
          <button type='submit' name='simpan' class='btn btn-info pull-right'>Simpan Absensi</button>
        </div>

      </form>
    </div><!-- /.box -->
  </div>
<?php
  if (isset($_POST[simpan])) {
    $jml_data = count($_POST[nip]);
    $nip = $_POST[nip];
    $a = $_POST[a];

    $tglabsen = $_POST[filtertgl];
    for ($i = 1; $i <= $jml_data; $i++) {
      $cek = mysqli_query($conn, "SELECT * FROM rb_absensi_guru where nip='" . $nip[$i] . "' AND tanggal='$tglabsen'");
      $total = mysqli_num_rows($cek);
      if ($total >= 1) {
        mysqli_query($conn, "UPDATE rb_absensi_guru SET kode_kehadiran = '" . $a[$i] . "' where nip='" . $nip[$i] . "' AND tanggal='$tglabsen'");
        $lst_siswa = mysqli_query($conn, "SELECT * FROM rb_guru where nip='" . $nip[$i] . "'");
        while ($cs = mysqli_fetch_array($lst_siswa)) {
          $cek = mysqli_query($conn, "SELECT * FROM rb_absensi_guru where  AND nip='" . $nip[$i] . "' AND tanggal='$tglabsen' and kode_kehadiran!='H'");
          while ($cik = mysqli_fetch_array($cek)) {

           
            $idts = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM users where level='kepsek'"));
            $idt = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM identitas LIMIT 1"));
            $link_send = $idt['link_one_sender'];
            $token_send = $idt['token'];

            $phones = $idts['no_telp'];
            $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';
            } else {
              $statush = 'Hadir';
            }
            $isi_pesan = 'Diberitahukan Bahwa Bpk/Ibu Guru dengan nama *' . $cs['nama_guru'] . '*, absensi Hari ini ' . $waktus . ', ' . $waktu . ' : _*' . $statush . '*_';

            // par
            $phone = $phones;
            $apikey = $token_send;
            $tujuan = $phone;
            $pesan = $isi_pesan;

            $curl = curl_init();

            curl_setopt_array($curl, array(
              CURLOPT_URL => 'https://starsender.online/api/sendText?message=' . rawurlencode($pesan) . '&tujuan=' . rawurlencode($tujuan . '@s.whatsapp.net'),
              CURLOPT_RETURNTRANSFER => true,
              CURLOPT_ENCODING => '',
              CURLOPT_MAXREDIRS => 10,
              CURLOPT_TIMEOUT => 0,
              CURLOPT_FOLLOWLOCATION => true,
              CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
              CURLOPT_CUSTOMREQUEST => 'POST',
              CURLOPT_HTTPHEADER => array(
                'apikey: ' . $apikey
              ),
            ));

            $response = curl_exec($curl);

            curl_close($curl);
          }
        }
      } else {
        mysqli_query($conn, "INSERT INTO rb_absensi_guru VALUES ('','" . $nip[$i] . "',
                                                              '" . $a[$i] . "',
                                                              '$tglabsen',
                                                              '" . date('Y-m-d H:i:s') . "','','','','','','')");
        $lst_siswa = mysqli_query($conn, "SELECT * FROM rb_guru where nip='" . $nip[$i] . "'");
        while ($cs = mysqli_fetch_array($lst_siswa)) {
          $cek = mysqli_query($conn, "SELECT * FROM rb_absensi_guru where nip='" . $nip[$i] . "' AND tanggal='$tglabsen' and kode_kehadiran!='H'");
          while ($cik = mysqli_fetch_array($cek)) {

            
            $idts = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM users where level='kepsek'"));

            $idt = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM identitas LIMIT 1"));
              $link_send = $idt['link_one_sender'];
              $token_send = $idt['token'];

            $phones = $idts['no_telp'];
            $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';
            } else {
              $statush = 'Hadir';
            }
            $isi_pesan = "Diberitahukan Bahwa Bpk/Ibu Guru dengan nama *$cs[nama_guru]*, absensi Hari ini $waktus, $waktu : _*$statush*_";

             // par
             $phone = $phones;
             $apikey = $token_send;
             $tujuan = $phone;
             $pesan = $isi_pesan;
 
             $curl = curl_init();
 
             curl_setopt_array($curl, array(
               CURLOPT_URL => 'https://starsender.online/api/sendText?message=' . rawurlencode($pesan) . '&tujuan=' . rawurlencode($tujuan . '@s.whatsapp.net'),
               CURLOPT_RETURNTRANSFER => true,
               CURLOPT_ENCODING => '',
               CURLOPT_MAXREDIRS => 10,
               CURLOPT_TIMEOUT => 0,
               CURLOPT_FOLLOWLOCATION => true,
               CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
               CURLOPT_CUSTOMREQUEST => 'POST',
               CURLOPT_HTTPHEADER => array(
                 'apikey: ' . $apikey
               ),
             ));
 
             $response = curl_exec($curl);
 
             curl_close($curl);
          }
        }
      }
    }
    echo "<script>document.location='?view=absenguru&tahun=" . $_GET[tahun] . "';</script>";
  }
}
?>