<?php
//nilai
$filenya = base_url()."/android/i_tiket";
$filenyax = base_url()."/android/i_tiket";
$judul = "Pengajuan Ijin";
$juduli = $judul;
//nilai session
$sesiku = $this->session->userdata('sesiku');
$sesinama = $this->session->userdata('sesinama');

//PROSES ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//jika simpan
if ((isset($_GET['aksi']) && $_GET['aksi'] == 'simpan'))
	{
	//ambil nilai
	$keterangan = $this->input->post("keterangan");
	$status = $this->input->post("status");
	$latx = $this->input->post("latx");
	$laty = $this->input->post("laty");

	//cek
	//nek null
	if ((empty($keterangan)) OR (empty($status)))
		{
		//pesan
		echo "<font color=red><b>Input Tidak Lengkap. Harap Diulangi...!!</b></font>";
		exit();
		}
	elseif (empty($_FILES['fileijin']['name']))
	{
		echo "<font color=red><b>Input Tidak Lengkap. Harap Diulangi...!!</b></font>";
		exit();
	}
	else
		{
			
			$hariini = date("Y-m-d");
			$cekabsen=$this->db->query("SELECT * FROM web_absen WHERE nip='$sesiku' AND tgl='$hariini'");
			$hasilcekabsen=$cekabsen->num_rows();
			if ($hasilcekabsen ==0) {
				$cekijin=$this->db->query("SELECT * FROM web_ijin WHERE absen='$sesiku' AND tgl='$hariini'");
				$hasilijin=$cekijin->num_rows();
				if ($hasilijin ==0) {
					$ceksiswa=$this->db->query("SELECT * FROM web_tempijin WHERE absen='$sesiku' AND tgl='$hariini'");
					$hasilcek=$ceksiswa->num_rows();

					//cek
					if ($hasilcek == 0)
						{
							//perintah SQL
							$imgFilelogo = $_FILES['fileijin']['name'];
							$tmp_dirlogo = $_FILES['fileijin']['tmp_name'];
							if(empty($imgFilelogo))

							{

								$logopic='';

							} else {
								$upload_dir = './upload/ijin/'; // upload directory
							$imgExt = strtolower(pathinfo($imgFilelogo,PATHINFO_EXTENSION));
							$valid_extensions = array('jpeg', 'jpg', 'png', 'gif');
							$logopic = rand(1000,1000000).".".$imgExt;
							move_uploaded_file($tmp_dirlogo,$upload_dir.$logopic);
							}
							$this->db->query("INSERT INTO `web_tempijin`(`tgl`, `absen`, `status`, `ket`, `file`, `lat_x`, `lat_y`) VALUES ('$hariini','$sesiku','$status','$keterangan','$logopic','$latx','$laty')");

							//pesan
							echo "<font color=red><b>BERHASIL PENGAJUAN IJIN</b></font>";
							exit();
						}
						else
						{
							//pesan
							echo "<font color=red><b>HARI INI ANDA SUDAH MELAKUKAN PENGAJUAN IJIN TUNGGU DI ACC ADMIN</b></font>";
							exit();
						}
				} else {
					echo "<font color=red><b>SUDAH DI IJINKAN ADMIN JADI TIDAK BISA PENGAJUAN IJIN</b></font>";
					exit();
				}
			} else {
				echo "<font color=red><b>ANDA SUDAH MELAKUKAN ABSEN DI HARI INI JADI TIDAK BISA PENGAJUAN IJIN</b></font>";
				exit();
			}
		}
			
	exit();
	}

//jika form
if ((isset($_GET['aksi']) && $_GET['aksi'] == 'form'))
	{
		$latx = cegah($_GET['latx']);
		$laty = cegah($_GET['laty']);
		//echo $latx."<br>";
		//echo $laty."<br>";
	?>


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

		$("#btnKRM").on('click', function(){
			$("#formx2").submit(function(){
				var formData = new FormData($('#formx2')[0]);
		    $.ajax({
		        url: "<?php echo $filenyax;?>?aksi=simpan",
		        type: 'POST',
		        data: formData,
		        success: function (data) {
		            $("#ihasil").html(data);
		        },
		        cache: false,
		        contentType: false,
		        processData: false
		    });

				return false;
			});


		});


	});

	</script>
	<div class="row m-3">
	    <div class="cell-md-4 text-center text-left-md">
	        <h3 class="dashboard-section-title">PENGAJUAN TIKET</h3>

	    </div>
	</div>
	<div class="login-box">
	<form class="bg-white p-4" name="formx2" id="formx2" action="javascript:"
              data-role="validator"
              data-clear-invalid="2000"
              data-on-error-form="invalidForm">
		<div id="ihasil"></div>
		<div class="form-group">
		<select name="status" id="status" class="form-control">
		<option value="">--Select Tujuan--</option>
		<?php
		$dep=$this->db->query("SELECT * FROM web_dep");
		foreach ($dep->result() as $row1) {
			$kode_dep=$row1->kode;
			$nama_dep=$row1->nama_dep;
			echo "<option value=".$kode_dep.">".$nama_dep."</option>";
		}
		?>
		</select>
		</div>
		<div class="form-group">
		<select name="status" id="status" class="form-control">
		<option value="">--Select Ruangan--</option>
		<?php
		$ruang=$this->db->query("SELECT * FROM web_ruang");
		foreach ($ruang->result() as $row2) {
			$kode_ruang=$row2->kode;
			$nama_ruang=$row2->nama_ruang;
			echo "<option value=".$kode_ruang.">".$nama_ruang."</option>";
		}
		?>
		</select>
		</div>
		<div class="form-group">
			<input type="text" name="keterangan" id="keterangan" data-role="input" placeholder="Keterangan" data-append="<span class='mif-pencil'>" data-validate="required">
			<span class="invalid_feedback">Masukkan Keterangan</span>
		</div>

	<div class="form-group">
		<input type="file" name="fileijin" id="fileijin" data-role="input" placeholder="Cari file ijin" data-validate="required">
		<span class="invalid_feedback">Masukan file ijin</span>
	</div>
		<input type="hidden" name="latx" id="latx" value="<?php echo $latx;?>" readonly>
    <input type="hidden" name="laty" id="laty" value="<?php echo $laty;?>" readonly>
		<br>
		<div class="form-group d-flex flex-align-center flex-justify-between">
			<button class="button primary" name="btnKRM" id="btnKRM" type="submit">Simpan</button>
			<a class="button alert" href="utama.html">Batal</a>
		</div>
	</form>

</div>

	<?php

	exit();
	}
//jika lihat
if ((isset($_GET['aksi']) && $_GET['aksi'] == 'lihat'))
	{
	?>
	<div class="row m-3">
	    <div class="cell-md-4 text-center text-left-md">
	        <h3 class="dashboard-section-title">LIHAT TIKET</h3>

	    </div>
	</div>
	<div class="login-box">
	
	</div>

	<?php

	exit();
	}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
?>
