<?php 
require("config/koneksi.php");
require("config/function.php");
require("config/crud.php");

	$sql = mysqli_query($koneksi, "select * from status");
	$data = mysqli_fetch_array($sql);
	$mode_absen = $data['mode'];
	$mode = "";
	if($mode_absen==1)
		$mode = "Masuk";	
	else if($mode_absen==2)
		$mode = "Pulang";
	
	$baca_kartu = mysqli_query($koneksi, "select * from tmprfid");
	$data_kartu = mysqli_fetch_array($baca_kartu);
	$nokartu    = $data_kartu['nokartu'];
	$rf = mysqli_fetch_array(mysqli_query($koneksi, "SELECT * FROM datarfid WHERE nokartu='$nokartu'"));
	$rfsiswa = mysqli_fetch_array(mysqli_query($koneksi, "SELECT * FROM siswa_ptm WHERE id_siswa='$rf[idsiswa]'"));
	$nowa = $rfsiswa['hp'];
?>

<div class='row'>
        <div class='col-md-12'>
            <div class='panel panel-default'>
               <div class="panel-heading" style="height:45px">
			   
                  <h4 class='box-title'><i class="fas fa-camera"></i> Absen RFID</h4></div>    
			  <div class='box-header with-border'>
			   <div class='box-tools pull-right '>
			   
			   <table>
			   <tr>
			   <td>
			   <div class="form-group-sm">
			  <select name='jenjang' class='form-control'  required='true' >
              <option value=""><?= $setting['sekolah'] ?></option>
               </select>
			   </div>
				</td>
			   <td>
			   <div class="form-group-sm">
			  <select name='jenjang' class='form-control'  required='true' >
              <option value=""><?= $setting['jenjang'] ?></option>
               </select>
			   </div>
				</td>
				<td>
				<div class="form-group-sm">
			  <select name='jenjang' class='form-control'  required='true'>
               <option value=""><?= $setting['kota'] ?></option>
               </select>
			   </div>
				</td>
				<td>
				<div class="form-group-sm">
			  <select name='jenjang' class='form-control'  required='true' >
               <option value=""><?= $setting['kecamatan'] ?></option>
               </select>
			   </div>
				</td>
				</tr>					
			</table>
			</div>
			  <h4 class='box-title'></h4>
			  </div>
			  <div class="box-body">
        <div class="box-body no-padding " style="text-align: center;">

	<?php if($nokartu=="") { ?>
<img src="<?= $homeurl . '/' . $setting['logo'] ?>" height="100px">
<h5><?= $setting['sekolah'] ?></h5>
<hr>
	<h3>Absen : <?php echo $mode; ?> </h3>
	<h3>Silahkan Tempelkan Kartu RFID Anda</h3>
	<img src="<?= $homeurl ?>/dist/img/rfid.png" style="width: 150px"> <br>
	<img src="<?= $homeurl ?>/dist/img/animasi2.gif">
   <hr>
	<?php } else {
		
		$cari = mysqli_query($koneksi, "select * from datarfid where nokartu='$nokartu'");
		$jumlah_data = mysqli_num_rows($cari);

		if($jumlah_data==0)
			echo "
		 <script type='text/javascript'>
		setTimeout(function () { 	
			swal({
				title: 'GAGAL',
				text:  'KARTU TIDAK DIKENAL',
				type: 'error',
				timer: 1000,
				showConfirmButton: true
			});		
		},10);	
		
	  </script>";
		else
		{
			
			$data = mysqli_fetch_array($cari);
			$nama = $data['nama'];

			$tanggal = date('Y-m-d');
			$jam     = date('H:i:s');
            $blth    = date('m-Y');
			$cari_absen = mysqli_query($koneksi, "select * from absensi where nokartu='$nokartu' and tanggal='$tanggal'");
			
			$jumlah_absen = mysqli_num_rows($cari_absen);
			if($jumlah_absen == 0)
			{
				
				mysqli_query($koneksi, "insert into absensi(nokartu, tanggal, jam_masuk, ket, blth)values('$nokartu', '$tanggal', '$jam','H', '$blth')");
			echo "
		 <script type='text/javascript'>
		iziToast.info(
            {
                title: '$nama',
                message: 'Berasil Melakukan Absen Masuk',
				titleColor: '#FFFF00',
				messageColor: '#fff',
				backgroundColor: 'rgba(0, 0, 0, 0.5)',
				 progressBarColor: '#FFFF00',
				timeout: 1000,
                  position: 'topRight'
		  });
	  </script>";
	  if($setting['kirimabsen']=='1' AND $rf['idpeg']<>''):
	$pesan="Kami informasikan bahwa ".$nama." hari ini tanggal ".date('d-m-Y H:i:s')." HADIR pada Kegiatan Belajar Mengajar menggunakan absensi RFID. Layanan Informasi ini Otomatis dikirim oleh Server ".$setting['sekolah'];
$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => $setting['url_api'].'/send-message',
  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_POSTFIELDS => array('message' => $pesan,'number' => $setting['nowaks'])
));
$response = curl_exec($curl);
curl_close($curl);

 elseif($setting['kirimabsen']=='1' AND $rf['idsiswa']<>'' AND $nowa<>''):
	$pesan="Kami informasikan bahwa ananda ".$nama." hari ini tanggal ".date('d-m-Y H:i:s')." HADIR pada Kegiatan Belajar Mengajar menggunakan absensi RFID. Layanan Informasi ini Otomatis dikirim oleh Server ".$setting['sekolah']." untuk memberitahukan kehadiran kepada Orang Tua Siswa";
$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => $setting['url_api'].'/send-message',
  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_POSTFIELDS => array('message' => $pesan,'number' => $nowa)
));
$response = curl_exec($curl);
curl_close($curl);
	
	endif;
			}elseif($jumlah_absen == 1 AND $mode_absen == 1){
				echo "
			 <script type='text/javascript'>
		  iziToast.info(
            {
                title: '$nama',
                message: 'Gagal Absen Sudah Tercatat',
				titleColor: '#FFFF00',
				messageColor: '#fff',
				backgroundColor: 'rgba(0, 0, 0, 0.5)',
				 progressBarColor: '#FFFF00',
				timeout: 1000,
                  position: 'topRight'
		  });
	  </script>";	
			}
				if($mode_absen == 2)
				{
					
					mysqli_query($koneksi, "update absensi set jam_pulang='$jam' where nokartu='$nokartu' and tanggal='$tanggal'");
				echo "
		 <script type='text/javascript'>
		  iziToast.info(
            {
                title: '$nama',
                message: 'Melakukan Absen Pulang',
				titleColor: '#FFFF00',
				messageColor: '#fff',
				backgroundColor: 'rgba(0, 0, 0, 0.5)',
				 progressBarColor: '#FFFF00',
				timeout: 1000,
                  position: 'topRight'
		  });
	  </script>";
	   if($setting['kirimabsen']=='1' AND $rf['idpeg']<>''):
	$pesan="Kami informasikan bahwa ".$nama." hari ini tanggal ".date('d-m-Y H:i:s')." Telah selesai melakukan Kegiatan Belajar Mengajar,Absen Pulang menggunakan absensi RFID. Layanan Informasi ini Otomatis dikirim oleh Server ".$setting['sekolah'];
$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => $setting['url_api'].'/send-message',
  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_POSTFIELDS => array('message' => $pesan,'number' => $setting['nowaks'])
));
$response = curl_exec($curl);
curl_close($curl);
 elseif($setting['kirimabsen']=='1' AND $rf['idsiswa']<>'' AND $nowa<>''):
	$pesan="Kami informasikan bahwa ".$nama." hari ini tanggal ".date('d-m-Y H:i:s')." Telah selesai melakukan Kegiatan Belajar Mengajar, Absen Pulang menggunakan absensi RFID. Layanan Informasi ini Otomatis dikirim oleh Server ".$setting['sekolah'];
$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => $setting['url_api'].'/send-message',
  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_POSTFIELDS => array('message' => $pesan,'number' => $nowa)
));
$response = curl_exec($curl);
curl_close($curl);
endif;
				}
			}
		

		
		mysqli_query($koneksi, "delete from tmprfid");
	} ?>

</div>
</div>
</div>
</div>
</div>