<?php
//nilai
$filenyax = base_url()."/android1212/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:m:s");
//nilai session
$sesiku = $this->session->userdata('sesiku');
$sesinama = $this->session->userdata('sesinama');
$kd6_session = nosql($this->session->userdata('sesiku'));


$target_radius = $radius;

$ceksiswa=$this->db->query("SELECT * FROM web_siswa WHERE absen='$sesiku'");
foreach ($ceksiswa->result() as $rt1) {
	$ku_spesial=$rt1->spesial;
}

//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));
	}


?>


<link rel="stylesheet" href="<?php echo base_url('assets/css/bootstrap.min.css'); ?>">
<link rel="stylesheet" href="<?php echo base_url('assets/css/font-awesome.min.css'); ?>">
<link rel="stylesheet" href="<?php echo base_url('assets/css/AdminLTE.css'); ?>">
<link rel="stylesheet" href="<?php echo base_url('assets/css/skins/skins-biasawae.css'); ?>">
<?php
echo "sesi ".$sesiku."<br>";
//jika pmasuk
if ((isset($_GET['aksi']) && $_GET['aksi'] == 'pmasuk'))
	{
	//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;
		echo "cek jarak ".$jarake."<br>";
		//jika terbatas ////////////////////////////////////////////////////////////////////////////////
		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>";
				//jika belum, entri ya...
				if ($hasilcek1 ==0) {
						echo "cek jam ".$jamini." ".$masuk_batas."<br>";
					if ($jamini <= $masuk_batas) {

						$st=1;
						$awal=explode(':',$masuk_jam);
						$akhir=explode(':',$jamini);
						$dj=$akhir[0]-$awal[0];
						$dm=$akhir[1]-$awal[1];
						$ds=$akhir[2]-$awal[2];
						if ($dj>=0) {
							$telat=($dj>0) ? "$dj jam ":" ";
							$telat.=($dm>0) ? "$dm menit ": " ";
							$telat .=($ds>0) ? "$ds detik ": " ";
						} else {
							$tepat_waktu="selamat pagi, anda datang tepat waktu";
						}
						if ($telat) { $kett="Anda terlambat $telat"; $kett="terlambat $telat";}
						if ($tepat_waktu) { $kett=$tepat_waktu; $kett="tepat waktu";}
						$this->db->query("REPLACE INTO web_absen(tgl,jam,nip,status,komp,ket,lat_x,lat_y) VALUES('$hariini','$jamini','$sesiku','$st','127.0.0.1','$kett','$latx','$laty')");
					?>
					<br>
					<br>
					<div class="col-md-4 col-sm-6 col-xs-12">
					      <div class="info-box">
					        <span class="info-box-icon bg-green"><img src="img/p_masuk.png" height="75" /></span>

					        <div class="info-box-content">
					          <span class="info-box-text">PRESENSI MASUK</span>
					          <span class="info-box-number">
					          	BERHASIL...
					          	</span>
					        </div>
					        <!-- /.info-box-content -->
					      </div>
					      <!-- /.info-box -->


					<?php
					echo "<p>
					$waktu
					</p>

					<p>
					[$latx2]. [$laty2].
					</p>
					<p>
					<i>$nil_alaamt</i>
					</p>";
					?>
				    </div>
					<?php
					}
				}
				else
					{
					?>

					<br>
					<br>
					<div class="col-md-4 col-sm-6 col-xs-12">
					      <div class="info-box">
					        <span class="info-box-icon bg-red"><img src="img/p_masuk.png" height="75" /></span>

					        <div class="info-box-content">
					          <span class="info-box-text">PRESENSI MASUK</span>
					          <span class="info-box-number">
					          	<font color="red">MAAF, ANDA SUDAH MASUK.</font>
					          	</span>
					        </div>
					        <!-- /.info-box-content -->
					      </div>
					      <!-- /.info-box -->


					    </div>
					<?php
					}
				}
			else
				{
				?>
				<br>
				<br>
				<div class="col-md-4 col-sm-6 col-xs-12">
			          <div class="info-box">
			            <span class="info-box-icon bg-red"><i class="fa fa-map-pin"></i></span>

			            <div class="info-box-content">
			              <span class="info-box-text">DILUAR AREA</span>
			              <span class="info-box-number">
				              	<font color="red"><?php echo $jarake;?> Meter.</font>

				              	<p>
				              		PASTIKAN ANDA BERADA DALAM JANGKAUAN RADIUS LOKASI UTAMA.
				              	</p>
			              	</span>
			            </div>
			            <!-- /.info-box-content -->
			          </div>
			          <!-- /.info-box -->
			        </div>
			        <!-- /.col -->

				<?php
				}
			}
		//jika bebas radius . . . . .
		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();
				//jika belum, entri ya...
				if ($hasilcek1 ==0) {
					if ($jamini <= $masuk_batas) {
						$st=1;
						$awal=explode(':',$masuk_jam);
						$akhir=explode(':',$jamini);
						$dj=$akhir[0]-$awal[0];
						$dm=$akhir[1]-$awal[1];
						$ds=$akhir[2]-$awal[2];
						if ($dj>=0) {
							$telat=($dj>0) ? "$dj jam ":" ";
							$telat.=($dm>0) ? "$dm menit ": " ";
							$telat .=($ds>0) ? "$ds detik ": " ";
						} else {
							$tepat_waktu="selamat pagi, anda datang tepat waktu";
						}
						if ($telat) { $kett="Anda terlambat $telat"; $kett="terlambat $telat";}
						if ($tepat_waktu) { $kett=$tepat_waktu; $kett="tepat waktu";}
						$this->db->query("REPLACE INTO web_absen(tgl,jam,nip,status,komp,ket,lat_x,lat_y) VALUES('$hariini','$jamini','$sesiku','$st','127.0.0.1','$kett','$latx','$laty')");


					?>
					<br>
					<br>
					<div class="col-md-4 col-sm-6 col-xs-12">
								<div class="info-box">
									<span class="info-box-icon bg-green"><img src="img/p_masuk.png" height="75" /></span>

									<div class="info-box-content">
										<span class="info-box-text">PRESENSI MASUK</span>
										<span class="info-box-number">
											BERHASIL...
											</span>
									</div>
									<!-- /.info-box-content -->
								</div>
								<!-- /.info-box -->


					<?php
					echo "<p>$waktu</p>
					<p>[$latx2]. [$laty2].</p>
					<p><i>$nil_alaamt</i></p>";
					?>
				    </div>
				<?php
				}

			else
				{
				?>

				<br>
				<br>
				<div class="col-md-4 col-sm-6 col-xs-12">
				      <div class="info-box">
				        <span class="info-box-icon bg-red"><img src="img/p_masuk.png" height="75" /></span>

				        <div class="info-box-content">
				          <span class="info-box-text">PRESENSI MASUK</span>
				          <span class="info-box-number">
				          	<font color="red">MAAF, ANDA SUDAH MASUK.</font>
				          	</span>
				        </div>
				        <!-- /.info-box-content -->
				      </div>
				      <!-- /.info-box -->


				    </div>
				<?php
				}
			}

		}
	else
		{
		?>
		<br>
		<br>
		<div class="col-md-4 col-sm-6 col-xs-12">
	          <div class="info-box">
	            <span class="info-box-icon bg-red"><i class="fa fa-map-pin"></i></span>

	            <div class="info-box-content">
	              <span class="info-box-text">ERROR</span>
	              <span class="info-box-number">
	              	PASTIKAN GPS LOCATION AKTIF DAHULU...
	              	</span>
	            </div>
	            <!-- /.info-box-content -->
	          </div>
	          <!-- /.info-box -->
	        </div>
	        <!-- /.col -->
		<?php
		}
    exit();
	}

//jika ppulang
if ((isset($_GET['aksi']) && $_GET['aksi'] == 'ppulang'))
	{
	//ambil nilai
	$latx = cegah($_GET['latx']);
	$laty = cegah($_GET['laty']);


	$latx2 = balikin($_GET['latx']);
	$laty2 = balikin($_GET['laty']);

	$waktu = $today;





	//jika ada
	if (!empty($latx2))
		{
		//hari ini..
		$hariku = "$tahun-$bulan-$tanggal";
		$xyz = md5("$sesiku$hariku-PULANG");



		//hitung jaraknya //////////////////////////////////////////////////////////////////////////
		$qku = mysqli_query($koneksi, "SELECT * FROM a_profil");
		$rku = mysqli_fetch_assoc($qku);
		$ku_lat_x = balikin($rku['lat_x']);
		$ku_lat_y = balikin($rku['lat_y']);
		$awal_x = $ku_lat_x;
		$awal_y = $ku_lat_y;
		$akhir_x = $latx2;
		$akhir_y = $laty2;


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

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


		//jika terbatas ////////////////////////////////////////////////////////////////////////////////
		if ($ku_spesial == "false")
			{
			//jika dalam radius
			if ($target_radius >= $jarake)
				{
				//deteksi, kalo udah masuk, gak boleh tekan lagi...
				$qcc = mysqli_query($koneksi, "SELECT * FROM orang_lokasi ".
										"WHERE orang_kd = '$sesiku' ".
										"AND status = 'PULANG' ".
										"AND postdate LIKE '$hariku%'");
				$tcc = mysqli_num_rows($qcc);

				//jika belum, entri ya...
				if (empty($tcc))
					{
					//insert
					mysqli_query($koneksi, "INSERT INTO orang_lokasi(kd, orang_kd, orang_kode, orang_nama, ".
									"lat_x, lat_y, status, postdate) VALUES ".
									"('$xyz', '$sesiku', '$ku_nip', '$ku_nama', ".
									"'$latx', '$laty', 'PULANG', '$waktu')");

					?>
					<br>
					<br>
					<div class="col-md-4 col-sm-6 col-xs-12">
					      <div class="info-box">
					        <span class="info-box-icon bg-danger"><img src="img/p_pulang.png" height="75" /></span>

					        <div class="info-box-content">
					          <span class="info-box-text">PRESENSI PULANG</span>
					          <span class="info-box-number">
					          	BERHASIL...
					          	</span>
					        </div>
					        <!-- /.info-box-content -->
					      </div>
					      <!-- /.info-box -->


					<?php
					echo "<p>
					$waktu
					</p>

					<p>
					[$latx2]. [$laty2].
					</p>

					<p>
					<i>$nil_alaamt</i>
					</p>";
					?>



				    </div>
					<?php
					}

				else
					{
					?>

					<br>
					<br>
					<div class="col-md-4 col-sm-6 col-xs-12">
					      <div class="info-box">
					        <span class="info-box-icon bg-red"><img src="img/p_pulang.png" height="75" /></span>

					        <div class="info-box-content">
					          <span class="info-box-text">PRESENSI PULANG</span>
					          <span class="info-box-number">
					          	<font color="red">MAAF, ANDA SUDAH PULANG...</font>
					          	</span>
					        </div>
					        <!-- /.info-box-content -->
					      </div>
					      <!-- /.info-box -->


					    </div>
					<?php
					}
				}

			else
				{
				?>
				<br>
				<br>
				<div class="col-md-4 col-sm-6 col-xs-12">
			          <div class="info-box">
			            <span class="info-box-icon bg-red"><i class="fa fa-map-pin"></i></span>

			            <div class="info-box-content">
			              <span class="info-box-text">DILUAR AREA</span>
			              <span class="info-box-number">
			              	<font color="red"><?php echo $jarake;?> Meter.</font>

			              	<p>
			              		PASTIKAN ANDA BERADA DALAM JANGKAUAN RADIUS LOKASI UTAMA.
			              	</p>
			              	</span>
			            </div>
			            <!-- /.info-box-content -->
			          </div>
			          <!-- /.info-box -->
			        </div>
			        <!-- /.col -->

				<?php
				}
			}


		//jika bebas radius ............
		else
			{
			//deteksi, kalo udah masuk, gak boleh tekan lagi...
			$qcc = mysqli_query($koneksi, "SELECT * FROM orang_lokasi ".
									"WHERE orang_kd = '$sesiku' ".
									"AND status = 'PULANG' ".
									"AND postdate LIKE '$hariku%'");
			$tcc = mysqli_num_rows($qcc);

			//jika belum, entri ya...
			if (empty($tcc))
				{
				//insert
				mysqli_query($koneksi, "INSERT INTO orang_lokasi(kd, orang_kd, orang_kode, orang_nama, ".
								"lat_x, lat_y, status, postdate) VALUES ".
								"('$xyz', '$sesiku', '$ku_nip', '$ku_nama', ".
								"'$latx', '$laty', 'PULANG', '$waktu')");

				?>
				<br>
				<br>
				<div class="col-md-4 col-sm-6 col-xs-12">
				      <div class="info-box">
				        <span class="info-box-icon bg-danger"><img src="img/p_pulang.png" height="75" /></span>

				        <div class="info-box-content">
				          <span class="info-box-text">PRESENSI PULANG</span>
				          <span class="info-box-number">
				          	BERHASIL...
				          	</span>
				        </div>
				        <!-- /.info-box-content -->
				      </div>
				      <!-- /.info-box -->


				<?php
				echo "<p>
				$waktu
				</p>

				<p>
				[$latx2]. [$laty2].
				</p>

				<p>
				<i>$nil_alaamt</i>
				</p>";
				?>



			    </div>
				<?php
				}

			else
				{
				?>

				<br>
				<br>
				<div class="col-md-4 col-sm-6 col-xs-12">
				      <div class="info-box">
				        <span class="info-box-icon bg-red"><img src="img/p_pulang.png" height="75" /></span>

				        <div class="info-box-content">
				          <span class="info-box-text">PRESENSI PULANG</span>
				          <span class="info-box-number">
				          	<font color="red">MAAF, ANDA SUDAH PULANG...</font>
				          	</span>
				        </div>
				        <!-- /.info-box-content -->
				      </div>
				      <!-- /.info-box -->


				    </div>
				<?php
				}
			}

		}
	else
		{
		?>
		<br>
		<br>
		<div class="col-md-4 col-sm-6 col-xs-12">
	          <div class="info-box">
	            <span class="info-box-icon bg-red"><i class="fa fa-map-pin"></i></span>

	            <div class="info-box-content">
	              <span class="info-box-text">ERROR</span>
	              <span class="info-box-number">
	              	PASTIKAN GPS LOCATION AKTIF DAHULU...
	              	</span>
	            </div>
	            <!-- /.info-box-content -->
	          </div>
	          <!-- /.info-box -->
	        </div>
	        <!-- /.col -->

		<?php
		}

    exit();
	}







//jika error
if ((isset($_GET['aksi']) && $_GET['aksi'] == 'error'))
	{
	?>

	<br>
	<br>
	<div class="col-md-4 col-sm-6 col-xs-12">
          <div class="info-box">
            <span class="info-box-icon bg-red"><i class="fa fa-map-pin"></i></span>

            <div class="info-box-content">
              <span class="info-box-text">ERROR</span>
              <span class="info-box-number">
              	PASTIKAN GPS LOCATION AKTIF DAHULU...
              	</span>
            </div>
            <!-- /.info-box-content -->
          </div>
          <!-- /.info-box -->
        </div>
        <!-- /.col -->


    <?php
    exit();
	}


?>
