<?php
$filenyax = base_url()."/android/i_set_lokasi";
$tiga=$this->db->query("SELECT * from web_setting where id='1'");
foreach ($tiga->result() as $rt) {
	$nama_sekolah=$rt->nama;
	$namsek=$rt->nama;
	$logo=$rt->logo;
	$back=$rt->back;
	$tapel=$rt->tapel;
	$kop=$rt->kop;
	$kepsek = balikin($rt->namakepsek);
	$nipkepsek = $rt->nipkepsek;
	$kabupaten = $rt->kabupaten;
	/*$masuk_jam = $rt->jam_masuk;
	$masuk_batas = $rt->batas_jam;
	$pulang_jam = $rt->jam_pulang;
	$pulang_batas = $rt->batas_jampulang;*/
	$radius = $rt->radius;
	$pusatx = $rt->lat_x;
	$pusaty = $rt->lat_y;
}
$dd=$this->input->get('dd');
$mm=$this->input->get('mm');
$yy=$this->input->get('yy');
if (!$dd) $dd=date('d');
if (!$mm) $mm=date('m');
if (!$yy) $yy=date('Y');
$hariini = date("Y-m-d");
$jamini=date("H:i:s");
$harike3=substr(hari($dd,$mm,$yy),0,3);
//nilai session
$sesiku = $this->session->userdata('sesiku');
$sesinama = $this->session->userdata('sesinama');
$kd6_session = nosql($this->session->userdata('sesiku'));
$target_radius = $radius;
//$hasilaksi=$_GET['aksi'];
$ceksiswa=$this->db->query("SELECT * FROM web_siswa WHERE absen='$sesiku'");
foreach ($ceksiswa->result() as $rt1) {
	$ku_spesial=$rt1->spesial;
	$cekhari=$rt1->$harike3;
	$jenispegawai=$rt1->jenispegawai;
}
$cekjam=$this->db->query("SELECT * FROM web_jam WHERE kode='$cekhari'");
foreach ($cekjam->result() as $rsjam) {
	$awal_masuk= $rsjam->awal_masuk;
	$masuk_jam = $rsjam->jam_masuk;
	$masuk_batas = $rsjam->akhir_masuk;
	$pulang_jam = $rsjam->jam_pulang;
	$pulang_batas = $rsjam->akhir_pulang;
}
//echo "ini aksinya".$hasilaksi;
//detail

//fungsi hitung jarak
function getDistanceBetween($latitude1, $longitude1, $latitude2, $longitude2, $unit = 'Km')
	{
	$theta = $longitude1 - $longitude2;
	$distance = (sin(deg2rad($latitude1)) * sin(deg2rad($latitude2)))  + (cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * cos(deg2rad($theta)));
	$distance = acos($distance);
	$distance = rad2deg($distance);
	$distance = $distance * 60 * 1.1515;
	switch($unit)
		{
		case 'Mi': break;
		case 'Km' : $distance = $distance * 1.609344;
		}
	return (round($distance,2));
	}

$ipaddr=get_client_ip();
?>
<?php
if ((isset($_GET['aksi']) && $_GET['aksi'] == 'pmasuk'))
	{
		if ($cekhari==6){
			?>
			<div class="h2 text-center fg-orange m-8">HARI INI LIBUR</div>
			<div class="text-leader2 text-center page-error-box mx-auto"><font color="red">TERIMA KASIH</font></div>
			<a class="button primary" href="cekgps.html">Close</a>
					<?php
		} else {
		//ambil nilai
		$latx = cegah($_GET['latx']);
		$laty = cegah($_GET['laty']);
		$latx2 = balikin($_GET['latx']);
		$laty2 = balikin($_GET['laty']);
		$waktu = $hariini;
	//jika ada
	if (!empty($latx2))
		{
		//hari ini..
		$hariku = "$yy-$mm-$dd";
		$xyz = md5("$sesiku-MASUK");

		$awal_x = $pusatx;
		$awal_y = $pusaty;
		$akhir_x = $latx2;
		$akhir_y = $laty2;

		//jaraknya
		$jaraku = getDistanceBetween($awal_x, $awal_y, $akhir_x, $akhir_y);

		//jadikan mater
		$jarake = $jaraku * 1000;

		if ($ku_spesial == 0)
			{
			//jika dalam radius
			if ($target_radius >= $jarake)
				{
				//deteksi, kalo udah masuk, gak boleh tekan lagi...
				$cek1=$this->db->query("SELECT status,jam,ket FROM web_absen WHERE tgl='$hariini' AND nip='$sesiku' AND status=1");
				$hasilcek1=$cek1->num_rows();
				//echo "ngecek data ".$hasilcek1."<br>";
				if ($hasilcek1 ==0) {
						//echo "cek jam ".$jamini." ".$masuk_batas."<br>";
					if ($jamini <= $masuk_batas) {
						$tepat_waktu='';
						$telat='';
						$st=1;
						if ($jamini > $masuk_jam) {
							$akhir=strtotime($jamini);
							$awal = strtotime($masuk_jam);
							$diff  = $akhir - $awal;
							$jam   = floor($diff / (60 * 60));
							$menit = $diff - ( $jam * (60 * 60) );
							$detik = $diff % 60;
							$tammenit=floor( $menit / 60 );
							$telat=($jam>0) ? "$jam jam ":" ";
							$telat.=($menit>0) ? "$tammenit menit ": " ";
							$telat.=($detik>0) ? "$detik detik ": " ";
						} else {
							$tepat_waktu="selamat pagi, anda datang tepat waktu";
						}
						if ($telat) {$kett="terlambat $telat";}
						if ($tepat_waktu) {$kett="tepat waktu";}
						$this->db->query("REPLACE INTO web_absen(tgl,jam,nip,status,komp,ket,lat_x,lat_y,jenispegawai) VALUES('$hariini','$jamini','$sesiku','$st','$ipaddr','$kett','$latx','$laty','$jenispegawai')");
					?>
					<div class="h2 text-center fg-orange m-8">PRESENSI MASUK</div>
					<div class="text-leader2 text-center page-error-box mx-auto"><font color="red">BERHASIL...</font></div>
					<div class="text-leader2 text-center page-error-box mx-auto"><?php $waktu."<br".$latx2.",".$laty2;?></div>
					<a class="button primary" href="cekgps.html">Close</a>
					<?php
					}
				} else {
					?>
					<div class="h2 text-center fg-orange m-8">HARI INI SUDAH PRESENSI MASUK</div>
					<div class="text-leader2 text-center page-error-box mx-auto"><font color="red">TERIMA KASIH</font></div>
					<a class="button primary" href="cekgps.html">Close</a>
							<?php
				}
			} else {
				?>
				<div class="h2 text-center fg-orange m-8">DILUAR AREA</div>
				<div class="text-leader2 text-center page-error-box mx-auto"><font color="red"><?php echo $jarake;?> METER.</font></div>
				<div class="text-leader2 text-center page-error-box mx-auto">PASTIKAN ANDA BERADA DALAM JANGKAUAN RADIUS LOKASI UTAMA.</div>
				<a class="button primary" href="cekgps.html">Close</a>

				<?php
			}
		} else {
			//deteksi, kalo udah masuk, gak boleh tekan lagi...
			$cek1=$this->db->query("SELECT status,jam,ket FROM web_absen WHERE tgl='$hariini' AND nip='$sesiku' AND status=1");
			$hasilcek1=$cek1->num_rows();
			//echo "ngecek data ".$hasilcek1."<br>";
			if ($hasilcek1 ==0) {
					//echo "cek jam ".$jamini." ".$masuk_batas."<br>";
				if ($jamini <= $masuk_batas) {
					$tepat_waktu='';
					$telat='';
					$st=1;
					if ($jamini > $masuk_jam) {
							$akhir=strtotime($jamini);
							$awal = strtotime($masuk_jam);
							$diff  = $akhir - $awal;
							$jam   = floor($diff / (60 * 60));
							$menit = $diff - ( $jam * (60 * 60) );
							$detik = $diff % 60;
							$tammenit=floor( $menit / 60 );
							$telat=($jam>0) ? "$jam jam ":" ";
							$telat.=($menit>0) ? "$tammenit menit ": " ";
							$telat.=($detik>0) ? "$detik detik ": " ";
					} else {
						$tepat_waktu="selamat pagi, anda datang tepat waktu";
					}
					if ($telat) {$kett="terlambat $telat";}
					if ($tepat_waktu) {$kett="tepat waktu";}
					$this->db->query("REPLACE INTO web_absen(tgl,jam,nip,status,komp,ket,lat_x,lat_y,jenispegawai) VALUES('$hariini','$jamini','$sesiku','$st','$ipaddr','$kett','$latx','$laty','$jenispegawai')");
				?>
				<div class="h2 text-center fg-orange m-8">PRESENSI MASUK</div>
				<div class="text-leader2 text-center page-error-box mx-auto"><font color="red">BERHASIL...</font></div>
				<div class="text-leader2 text-center page-error-box mx-auto"><?php $waktu."<br".$latx2.",".$laty2;?></div>
				<a class="button primary" href="cekgps.html">Close</a>
				<?php
				}
			} else {
				?>
				<div class="h2 text-center fg-orange m-8">HARI INI SUDAH PRESENSI MASUK</div>
				<div class="text-leader2 text-center page-error-box mx-auto"><font color="red">TERIMA KASIH</font></div>
				<a class="button primary" href="cekgps.html">Close</a>
						<?php
			}
		}
	}
}
}

/// cek pulangggg ////
if ((isset($_GET['aksi']) && $_GET['aksi'] == 'ppulang'))
	{
		if ($cekhari==6){
			?>
			<div class="h2 text-center fg-orange m-8">HARI INI LIBUR</div>
			<div class="text-leader2 text-center page-error-box mx-auto"><font color="red">TERIMA KASIH</font></div>
			<a class="button primary" href="cekgps.html">Close</a>
					<?php
		} else {
	//ambil nilai
	$latx = cegah($_GET['latx']);
	$laty = cegah($_GET['laty']);
	$latx2 = balikin($_GET['latx']);
	$laty2 = balikin($_GET['laty']);
	$waktu = $hariini;
	//jika ada
	if (!empty($latx2))
		{
		//hari ini..
		$hariku = "$yy-$mm-$dd";
		$xyz = md5("$sesiku-MASUK");

		$awal_x = $pusatx;
		$awal_y = $pusaty;
		$akhir_x = $latx2;
		$akhir_y = $laty2;

		//jaraknya
		$jaraku = getDistanceBetween($awal_x, $awal_y, $akhir_x, $akhir_y);

		//jadikan mater
		$jarake = $jaraku * 1000;

		if ($ku_spesial == 0)
			{
			//jika dalam radius
			if ($target_radius >= $jarake)
				{
				//deteksi, kalo udah masuk, gak boleh tekan lagi...
				$cek2=$this->db->query("SELECT status,jam,ket FROM web_absen WHERE tgl='$hariini' AND nip='$sesiku' AND status=2");
				$hasilcek2=$cek2->num_rows();
				//echo "ngecek data ".$hasilcek1."<br>";
				if ($hasilcek2 ==0) {
						//echo "cek jam ".$jamini." ".$pulang_batas."<br>";
					if ($jamini <= $pulang_batas) {

						$this->db->query("REPLACE INTO web_absen(tgl,jam,nip,status,komp,ket,lat_x,lat_y,jenispegawai) VALUES('$hariini','$jamini','$sesiku','2','$ipaddr','','$latx','$laty','$jenispegawai')");
					?>
					<div class="h2 text-center fg-orange m-8">PRESENSI PULANG</div>
					<div class="text-leader2 text-center page-error-box mx-auto"><font color="red">BERHASIL...</font></div>
					<div class="text-leader2 text-center page-error-box mx-auto"><?php $waktu."<br".$latx2.",".$laty2;?></div>
					<a class="button primary" href="cekgps.html">Close</a>
					<?php
					}
				} else {
					?>
					<div class="h2 text-center fg-orange m-8">HARI INI SUDAH PRESENSI MASUK</div>
					<div class="text-leader2 text-center page-error-box mx-auto"><font color="red">TERIMA KASIH</font></div>
					<a class="button primary" href="cekgps.html">Close</a>
							<?php
				}
			} else {
				?>
				<div class="text-leader2 text-center page-error-box mx-auto">DILUAR AREA</div>
				<div class="text-leader2 text-center page-error-box mx-auto"><font color="red"><?php echo $jarake;?> METER.</font></div>
				<div class="text-leader2 text-center page-error-box mx-auto">PASTIKAN ANDA BERADA DALAM JANGKAUAN RADIUS LOKASI UTAMA.</div>
				<a class="button primary" href="cekgps.html">Close</a>

				<?php
			}
		} else {
			//deteksi, kalo udah masuk, gak boleh tekan lagi...
			$cek2=$this->db->query("SELECT status,jam,ket FROM web_absen WHERE tgl='$hariini' AND nip='$sesiku' AND status=2");
			$hasilcek2=$cek2->num_rows();
			//echo "ngecek data ".$hasilcek1."<br>";
			if ($hasilcek2 ==0) {
					//echo "cek jam ".$jamini." ".$pulang_batas."<br>";
				if ($jamini <= $pulang_batas) {

					$this->db->query("REPLACE INTO web_absen(tgl,jam,nip,status,komp,ket,lat_x,lat_y,jenispegawai) VALUES('$hariini','$jamini','$sesiku','2','$ipaddr','','$latx','$laty','$jenispegawai')");
				?>
				<div class="h2 text-center fg-orange m-8">PRESENSI PULANG</div>
				<div class="text-leader2 text-center page-error-box mx-auto"><font color="red">BERHASIL...</font></div>
				<div class="text-leader2 text-center page-error-box mx-auto"><?php $waktu."<br".$latx2.",".$laty2;?></div>
				<a class="button primary" href="cekgps.html">Close</a>
				<?php
				}
			} else {
				?>
				<div class="h2 text-center fg-orange m-8">HARI INI SUDAH PRESENSI MASUK</div>
				<div class="text-leader2 text-center page-error-box mx-auto"><font color="red">TERIMA KASIH</font></div>
				<a class="button primary" href="cekgps.html">Close</a>
						<?php
			}
		}
	}
}
}
///akhir cek pulangggg ///
///njajal
/*
if ((isset($_GET['aksi']) && $_GET['aksi'] == 'wfh'))
	{
		if ($cekhari==6){
			?>
			<div class="h2 text-center fg-orange m-8">HARI INI LIBUR</div>
			<div class="text-leader2 text-center page-error-box mx-auto"><font color="red">TERIMA KASIH</font></div>
			<a class="button primary" href="cekgps.html">Close</a>
					<?php
		} else {
			$latx = cegah($_GET['latx']);
			$laty = cegah($_GET['laty']);
			$latx2 = balikin($_GET['latx']);
			$laty2 = balikin($_GET['laty']);
			$waktu = $hariini;
			if (!empty($latx2))
				{
				$hariku = "$yy-$mm-$dd";
				$xyz = md5("$sesiku-WFH");
					$cek1=$this->db->query("SELECT status FROM web_tempijin WHERE absen='$sesiku' AND tgl='$hariini' AND status=7");
					$hasilcek1=$cek1->num_rows();
					if ($hasilcek1 ==0) {
						if ($jamini <= "12:00:00") {
							$st=7;
							$kett="Work From Home";
							$this->db->query("REPLACE INTO `web_tempijin`(`tgl`, `absen`, `status`, `ket`, `lat_x`, `lat_y`) VALUES ('$hariini','$sesiku','$status','$keterangan','$latx','$laty')");
						?>
						<div class="h2 text-center fg-orange m-8">PRESENSI WFH</div>
						<div class="text-leader2 text-center page-error-box mx-auto"><font color="red">BERHASIL...</font></div>
						<div class="text-leader2 text-center page-error-box mx-auto"><?php $waktu."<br".$latx2.",".$laty2;?></div>
						<a class="button primary" href="cekgps.html">Close</a>
						<?php
						}
					} else {
						?>
						<div class="h2 text-center fg-orange m-8">HARI INI SUDAH PRESENSI WFH</div>
						<div class="text-leader2 text-center page-error-box mx-auto"><font color="red">TERIMA KASIH</font></div>
						<a class="button primary" href="cekgps.html">Close</a>
								<?php
					}
			}
		}
}
*/
//akhir njajal
///awal cek WFH ////
if ((isset($_GET['aksi']) && $_GET['aksi'] == 'wfh'))
	{
		if ($cekhari==6){
			?>
			<div class="h2 text-center fg-orange m-8">HARI INI LIBUR</div>
			<div class="text-leader2 text-center page-error-box mx-auto"><font color="red">TERIMA KASIH</font></div>
			<a class="button primary" href="cekgps.html">Close</a>
					<?php
		} else {
	//ambil nilai
	$latx = cegah($_GET['latx']);
	$laty = cegah($_GET['laty']);
	$latx2 = balikin($_GET['latx']);
	$laty2 = balikin($_GET['laty']);
	$waktu = $hariini;
	//jika ada
	if (!empty($latx2))
		{
		//hari ini..
		$hariku = "$yy-$mm-$dd";
		$xyz = md5("$sesiku-WFH");
			//deteksi, kalo udah masuk, gak boleh tekan lagi...
			$cek1=$this->db->query("SELECT status,jam,ket FROM web_absen WHERE tgl='$hariini' AND nip='$sesiku' AND status=7");
			$hasilcek1=$cek1->num_rows();
			//echo "ngecek data ".$hasilcek1."<br>";
			if ($hasilcek1 ==0) {
					//echo "cek jam ".$jamini." ".$masuk_batas."<br>";
				if ($jamini <= "12:00:00") {
					$st=7;
					$kett="Work From Home";
					$this->db->query("REPLACE INTO web_absen(tgl,jam,nip,status,komp,ket,lat_x,lat_y,jenispegawai) VALUES('$hariini','$jamini','$sesiku','$st','$ipaddr','$kett','$latx','$laty','$jenispegawai')");
				?>
				<div class="h2 text-center fg-orange m-8">PRESENSI WFH</div>
				<div class="text-leader2 text-center page-error-box mx-auto"><font color="red">BERHASIL...</font></div>
				<div class="text-leader2 text-center page-error-box mx-auto"><?php $waktu."<br".$latx2.",".$laty2;?></div>
				<a class="button primary" href="cekgps.html">Close</a>
				<?php
				}
			} else {
				?>
				<div class="h2 text-center fg-orange m-8">HARI INI SUDAH PRESENSI WFH</div>
				<div class="text-leader2 text-center page-error-box mx-auto"><font color="red">TERIMA KASIH</font></div>
				<a class="button primary" href="cekgps.html">Close</a>
						<?php
			}
	}
}
}
///akhir cek WFH///
/// awal cek khusus ////
if ((isset($_GET['aksi']) && $_GET['aksi'] == 'absen2'))
	{
		if ($cekhari==6){
			?>
			<div class="h2 text-center fg-orange m-8">HARI INI LIBUR</div>
			<div class="text-leader2 text-center page-error-box mx-auto"><font color="red">TERIMA KASIH</font></div>
			<a class="button primary" href="cekgps.html">Close</a>
					<?php
		} else {
	//ambil nilai
	$latx = cegah($_GET['latx']);
	$laty = cegah($_GET['laty']);
	$latx2 = balikin($_GET['latx']);
	$laty2 = balikin($_GET['laty']);
	$waktu = $hariini;
	//jika ada
	if (!empty($latx2))
		{
		//hari ini..
		$hariku = "$yy-$mm-$dd";
		$xyz = md5("$sesiku-ABSEN2");

		$awal_x = $pusatx;
		$awal_y = $pusaty;
		$akhir_x = $latx2;
		$akhir_y = $laty2;

		//jaraknya
		$jaraku = getDistanceBetween($awal_x, $awal_y, $akhir_x, $akhir_y);

		//jadikan mater
		$jarake = 3000;

		if ($ku_spesial == 0)
			{
			//jika dalam radius
			if ($target_radius >= $jarake)
				{
				//deteksi, kalo udah masuk, gak boleh tekan lagi...
				$cek2=$this->db->query("SELECT status,jam,ket FROM web_absen WHERE tgl='$hariini' AND nip='$sesiku' AND status=212");
				$hasilcek2=$cek2->num_rows();
				//echo "ngecek data ".$hasilcek1."<br>";
				if ($hasilcek2 ==0) {
						//echo "cek jam ".$jamini." ".$pulang_batas."<br>";
					if (($jamini <= $jam_khusus) && ($jamini <= "11:45:00")){

						$this->db->query("REPLACE INTO web_absen(tgl,jam,nip,status,komp,ket,lat_x,lat_y,jenispegawai) VALUES('$hariini','$jamini','$sesiku','212','$ipaddr','','$latx','$laty','$jenispegawai')");
					?>
					<div class="h2 text-center fg-orange m-8">PRESENSI KHUSUS</div>
					<div class="text-leader2 text-center page-error-box mx-auto"><font color="red">BERHASIL...</font></div>
					<div class="text-leader2 text-center page-error-box mx-auto"><?php $waktu."<br".$latx2.",".$laty2;?></div>
					<a class="button primary" href="cekgps.html">Close</a>
					<?php
					}
				} else {
					?>
					<div class="h2 text-center fg-orange m-8">HARI INI SUDAH PRESENSI KHUSUS</div>
					<div class="text-leader2 text-center page-error-box mx-auto"><font color="red">TERIMA KASIH</font></div>
					<a class="button primary" href="cekgps.html">Close</a>
							<?php
				}
			} else {
				?>
				<div class="text-leader2 text-center page-error-box mx-auto">DILUAR AREA</div>
				<div class="text-leader2 text-center page-error-box mx-auto"><font color="red"><?php echo $jarake;?> METER.</font></div>
				<div class="text-leader2 text-center page-error-box mx-auto">PASTIKAN ANDA BERADA DALAM JANGKAUAN RADIUS LOKASI UTAMA.</div>
				<a class="button primary" href="cekgps.html">Close</a>

				<?php
			}
		} else {
			//deteksi, kalo udah masuk, gak boleh tekan lagi...
			$cek2=$this->db->query("SELECT status,jam,ket FROM web_absen WHERE tgl='$hariini' AND nip='$sesiku' AND status=2");
			$hasilcek2=$cek2->num_rows();
			//echo "ngecek data ".$hasilcek1."<br>";
			if ($hasilcek2 ==0) {
					//echo "cek jam ".$jamini." ".$pulang_batas."<br>";
				if (($jamini <= $jam_khusus) && ($jamini <= "11:45:00")) {

					$this->db->query("REPLACE INTO web_absen(tgl,jam,nip,status,komp,ket,lat_x,lat_y,jenispegawai) VALUES('$hariini','$jamini','$sesiku','212','$ipaddr','','$latx','$laty','$jenispegawai')");
				?>
				<div class="h2 text-center fg-orange m-8">PRESENSI KHUSUS</div>
				<div class="text-leader2 text-center page-error-box mx-auto"><font color="red">BERHASIL...</font></div>
				<div class="text-leader2 text-center page-error-box mx-auto"><?php $waktu."<br".$latx2.",".$laty2;?></div>
				<a class="button primary" href="cekgps.html">Close</a>
				<?php
				}
			} else {
				?>
				<div class="h2 text-center fg-orange m-8">HARI INI SUDAH PRESENSI KHUSUS</div>
				<div class="text-leader2 text-center page-error-box mx-auto"><font color="red">TERIMA KASIH</font></div>
				<a class="button primary" href="cekgps.html">Close</a>
						<?php
			}
		}
	}
}
}
///akhir cek pulangggg ///
?>
