<?php
//nilai
$filenya = base_url()."/android12/i_upload.php";
$filenyax = base_url()."/android12/i_upload.php";
$judul = "Upload Foto Barang";
$juduli = $judul;

//nilai session
$ku_hkode=$_GET['hkode'];
$tiga=$this->db->query("SELECT * from web_login where hkode='$ku_hkode'");
foreach ($tiga->result() as $rt) {
	$sesiku=$rt->sesiku;
	$sesinama=$rt->sesinama;
	$passx=$rt->passx;
	$departemen=$rt->departemen;
	$kd6_session=$rt->sesiku;
	$kodeabsen = $rt->sesiku;
	
}


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


//detail

//simpan
if ((isset($_GET['aksi']) && $_GET['aksi'] == 'simpan'))
	{
	//nilai
	$e_status = cegah($_GET["e_status"]);
	//$new_image_name = strtolower($_FILES['file']['name']);
	$new_image_name = "$sesiku-$tahun$bulan$tanggal$jam$menit$detik.jpg";


	//ketahui koordinat dan alamat lokasi...

	//copy...
	move_uploaded_file($_FILES["file"]["tmp_name"], "../filebox/barang/".$new_image_name);



	//kd
	$xyz = md5("$sesiku$new_image_name");
	$filexnya = cegah($new_image_name);


	//masukin ke database...
	mysqli_query($koneksi,"INSERT INTO barang_lokasi(kd, orang_kd, orang_kode, orang_nama, filex, postdate) VALUES ".
					"('$xyz', '$sesiku', '$ku_nip', '$ku_nama', '$filexnya', '$today')");



	exit();
	}









//jika simpan2
if ((isset($_GET['aksi']) && $_GET['aksi'] == 'simpan2'))
	{
	//ambil nilai
	$e_kd = cegah($_GET['kd']);
	$e_isi = cegah($_GET['e_isi']);


	//empty
	if (empty($e_isi))
		{
		echo '<b>
		<font color="red">GAGAL. SILAHKAN ULANGI LAGI...!!</font>
		</b>';
		}
	else
		{
		//update
		$this->db->query("UPDATE barang_lokasi SET status = '$e_isi' ".
						"WHERE kd = '$e_kd'");

		?>

		<script language='javascript'>
		//membuat document jquery
		$(document).ready(function(){

			$("#iform").hide();

		});

		</script>

		<?php
		echo '<b>
		<font color="green">Terima Kasih, Telah Memberikan Laporan.</font>
		</b>';
		}


	exit();
	}













//form
if ((isset($_GET['aksi']) && $_GET['aksi'] == 'form'))
	{
	//munculkan image terakhir
		$yuk_filex="";
	$ceksiswa1=$this->db->query("SELECT * FROM barang_lokasi WHERE orang_kd='$sesiku'");
	foreach ($ceksiswa1->result() as $rt2) {
		$yuk_kd = nosql($rt2->kd);
		$yuk_filex = balikin($rt2->filex);
	}

	?>

	<script language='javascript'>
	//membuat document jquery
	$(document).ready(function(){

		$("#btnKRM").on('click', function(){
			$("#formx2").submit(function(){
				$.ajax({
					url: "<?php echo $filenyax;?>?aksi=simpan2&kd=<?php echo $yuk_kd;?>",
					type:$(this).attr("method"),
					data:$(this).serialize(),
					success:function(data){
						$("#ihasil").html(data);
						}
					});
				return false;
			});


		});


	});

	</script>




	<?php
	echo '<table width="100%" border="0" cellpadding="5" cellspacing="5">
	<tr align="center">

	<td width="10">&nbsp;</td>
	<td valign="top">

	<hr>
	<div id="ihasil"></div>

	<img src="'.base_url().'/upload/profil/'.$yuk_filex.'" width="100%" />
	<div id="iform">
		<form name="formx2" id="formx2">
		<p>
		Keluhan/Saran/Catatan :
		<br>
		<textarea cols="10" name="e_isi" id="e_isi" wrap="yes" rows="5" class="btn-block btn-success"></textarea>
		</p>

		<p>
		<input type="submit" name="btnKRM" id="btnKRM" value="KIRIM >" class="btn btn-block btn-danger">
		</p>


		</form>
	</div>

	</td>

	<td width="10">&nbsp;</td>
	</tr>
	</table>

	<br>
	<br>
	<br>';


	exit();
	}








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


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



	//jadikan alamat ///////////////////////////////////////////////////////////////////////////////////////////////
	$lat = $latx2;
	$long = $laty2;

	$new_image_name = "$sesiku-$tahun$bulan$tanggal.jpg";


	//copy...
	move_uploaded_file($_FILES["file"]["tmp_name"], "./upload/profil/".$new_image_name);

	//kd
	$xyz = md5("$sesiku$new_image_name");
	$filexnya = cegah($new_image_name);

	function geo2address2($lat,$long,$keyku) {

	    $url = "https://maps.googleapis.com/maps/api/geocode/json?key=$keyku&latlng=$lat,$long&sensor=false";
	    $curlData=file_get_contents(    $url);
	    $address = json_decode($curlData);
	    $a=$address->results[0];
	    return explode(",",$a->formatted_address);
	}




	$nilku = geo2address2($lat,$long,$keyku);


	$nil1 = $nilku[0];
	$nil2 = $nilku[1];
	$nil3 = $nilku[2];
	$nil4 = $nilku[3];
	$nil5 = $nilku[4];
	$nil6 = $nilku[5];
	$nil7 = $nilku[6];
	$nil_tujuan = cegah("$nil1, $nil2, $nil3, $nil4, $nil5, $nil6, $nil7");
	$nil_tujuan2 = balikin("$nil1, $nil2, $nil3, $nil4, $nil5, $nil6, $nil7");





	//munculkan terakhir
	$qyuk = mysqli_query($koneksi,"SELECT * FROM barang_lokasi ".
							"WHERE orang_kd = '$sesiku' ".
							"ORDER BY postdate DESC");
	$ryuk = mysqli_fetch_assoc($qyuk);
	$yuk_kd = nosql($ryuk['kd']);


	//update...
	mysqli_query($koneksi,"UPDATE barang_lokasi SET filex ='$new_image_name',lat_x = '$latx', ".
					"lat_y = '$laty', ".
					"alamat = '$nil_tujuan' ".
					"WHERE kd = '$yuk_kd'");



	echo "<p>
	Barang Berada di :
	<br>
	<i>$nil_tujuan2</i>
	<br>
	[$latx2]. [$laty2].
	</p>";

    exit();
	}









//jika error
if ((isset($_GET['aksi']) && $_GET['aksi'] == 'error'))
	{
	echo "<b>
	<font color=red>
	GPS LOCATION Belum Diaktifkan...!!
	</font>
	</b>";


    exit();
	}











exit();
?>
