
<?php
//nilai
$filenya = base_url()."/android/i_jurnal";
$filenyax = base_url()."/android/i_jurnal";
$judul = "Pengisian Jurnal";
$juduli = $judul;
//nilai session
$sesiku = $this->session->userdata('sesiku');
$sesinama = $this->session->userdata('sesinama');
$ceksiswa=$this->db->query("SELECT * FROM web_siswa WHERE absen='$sesiku'");
foreach ($ceksiswa->result() as $rt1) {
	$kdkelas=$rt1->KD_JENIS_KETENAGAAN;
	$filex1=$rt1->filex1;
	$jenispegawai=$rt1->jenispegawai;
}
//PROSES ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//jika simpan
if ((isset($_GET['aksi']) && $_GET['aksi'] == 'simpan'))
	{
	//ambil nilai
	$keterangan = $this->input->post("keterangan");
	//$tglhari = $this->input->post("tglhari");
	$jamawal = $this->input->post("jamawal");
	$jamakhir = $this->input->post("jamakhir");
	$kegiatan = $this->input->post("kegiatan");
	$kelas = $this->input->post("kelas");
	$jmlsiswa = $this->input->post("jmlsiswa");
	$latx = $this->input->post("latx");
	$laty = $this->input->post("laty");
	if ($jenispegawai==2) {
		if ((empty($keterangan)) OR (empty($jamawal)) OR (empty($jamakhir)) OR (empty($kegiatan)) OR (empty($kelas)))
			{
			//pesan
			echo "<font color=red><b>Input Tidak Lengkap. Harap Diulangi...!!</b></font>";
			exit();
			}
		else 
			{
			
			$hariini = date("Y-m-d");
			$cekjurnalhsl=0;
			
			for ($x = $jamawal; $x <= $jamakhir; $x++) {
				$cekjurnal=$this->db->query("SELECT * FROM web_kegiatan WHERE absen='$sesiku' AND tgl='$hariini' AND jam='$x'");
				$hasiljurnal=$cekjurnal->num_rows();
				$cekjurnalhsl=$cekjurnalhsl+$hasiljurnal;
			
				
			
			} 
			
			if ($cekjurnalhsl == 0)
			{
				for ($x = $jamawal; $x <= $jamakhir; $x++) {
					$this->db->query("INSERT INTO `web_kegiatan`(`absen`, `tgl`, `jam`, `kegiatan`, `materi`, `id_kelas`, `tidakhadir`, `longx`, `longy`) VALUES 
					('$sesiku','$hariini','$x','$kegiatan','$keterangan','$kelas','$jmlsiswa','$latx','$laty')");
				}
				//pesan
				echo "<font color=red><b>BERHASIL ISI JURNAL</b></font>";
				exit();
			}
			else
			{
				//pesan
				echo "<font color=red><b>JAM JURNAL ADA YANG SAMA MOHON DI CEK</b></font>";
				exit();
			}
			}
	} else {
		if ((empty($keterangan))) {
			echo "<font color=red><b>Input Tidak Lengkap. Harap Diulangi...!!</b></font>";
			exit();
		} else {
			$hariini = date("Y-m-d");
			$cekjurnalhsl=0;
			for ($x = $jamawal; $x <= $jamakhir; $x++) {
				$cekjurnal=$this->db->query("SELECT * FROM web_kegiatan WHERE absen='$sesiku' AND tgl='$hariini' AND jam='$x'");
				$hasiljurnal=$cekjurnal->num_rows();
				$cekjurnalhsl=$cekjurnalhsl+$hasiljurnal;
			} 
			if ($cekjurnalhsl == 0)
			{
				for ($x = $jamawal; $x <= $jamakhir; $x++) {
					$this->db->query("INSERT INTO `web_kegiatan`
					(`absen`, `tgl`, `jam` `materi`, `longx`, `longy`) VALUES 
					('$sesiku','$hariini','$x','$keterangan','$latx','$laty')");
				}
				//pesan
				echo "<font color=red><b>BERHASIL ISI JURNAL</b></font>";
				exit();
			} else{
				//pesan
				echo "<font color=red><b>JAM JURNAL ADA YANG SAMA MOHON DI CEK</b></font>";
				exit();
			}
		}
	}
				
exit();
}

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


	<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>
	<link rel="stylesheet" href="//code.jquery.com/ui/1.13.0/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script src="https://code.jquery.com/jquery-3.6.0.js"></script>
  <script src="https://code.jquery.com/ui/1.13.0/jquery-ui.js"></script>
  <script>
  $( function() {
    $( "#datepicker" ).datepicker();
  } );
  </script>
	<div class="row m-3">
	    <div class="cell-md-4 text-center text-left-md">
	        <h3 class="dashboard-section-title">JURNAL KEGIATAN</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="jamawal" id="jamawal" class="form-control">
		<option value="">--Select Jam Awal--</option>
		<?php
		for ($x = 1; $x <= 12; $x++) {
		  echo "<option value=".$x.">".$x."</option>";
		}
		?>
		</select>
		</div>
		<div class="form-group">
		<select name="jamakhir" id="jamakhir" class="form-control">
		<option value="">--Select Jam Akhir--</option>
		<?php
		for ($y = 1; $y <= 12; $y++) {
		  echo "<option value=".$y.">".$y."</option>";
		}
		?>
		</select>
		</div>
		<?php if ($jenispegawai==2) {?>
		<div class="form-group">
		<select name="kegiatan" id="kegiatan" class="form-control">
		<option value="">--Select Kegiatan--</option>
		<?php
		$kegiatan=$this->db->query("SELECT * FROM web_dftrkeg");
		foreach ($kegiatan->result() as $row2) {
			$nama_kegiatan='"'.$row2->nama_kegiatan.'"';
			$nama_kegiatan1=$row2->nama_kegiatan;
			echo "<option value=".$nama_kegiatan.">".$nama_kegiatan1."</option>";
		}
		?>
		</select>
		</div>
		<div class="form-group">
		<select name="kelas" id="kelas" class="form-control">
		<option value="">--Select Kelas--</option>
		<?php
		$mapil=$this->db->query("SELECT * FROM web_kelas");
		foreach ($mapil->result() as $row1) {
			$kode_mapel=$row1->nama;
			$nama_mapel='"'.$row1->nama.'"';
			echo "<option value=".$nama_mapel.">".$kode_mapel."</option>";
		}
		?>
		</select>
		</div>
		<div class="form-group">
			<input type="text" name="jmlsiswa" id="jmlsiswa" data-role="input" placeholder="Jumlah siswa tidak hadir" data-append="<span class='mif-pencil'>" data-validate="required">
			<span class="invalid_feedback">Masukkan Jumlah siswa yang tidak hadir</span>
		</div>
		<?php }?>
		<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>

		<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>
	DAFTAR JURNAL YANG SUDAH DI INPUT
	<table style="border:1px solid black;border-collapse: collapse" width="100%">
	<tr bgcolor="#CCCCCC" height="40">
	<th width="15%" style="font-size: small;text-align: center;border:1px solid black">TGL</th>
	<th style="font-size: small;text-align: center;border:1px solid black">JURNAL</th>
	</tr>
	<?php 
	$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');
	$tot_hari=jml_hari($mm,$yy);
	for ($i=1;$i<=$tot_hari;$i++) {
		$aa=str_pad($i,2,'0',STR_PAD_LEFT);
		?>
		<tr>
			<td style='font-size: small; border:1px solid black' align='center'>&nbsp;<?php echo $tg="$aa";?></td>
			<td>
				<table style="border:1px solid black;border-collapse: collapse" width="100%">
				<tr>
					<th>JAM</th><th>Kegiatan</th><th>Kelas</th><th>Keterangan</th>
				</tr>
				
				<?php
				$skrg="$yy-$mm-$aa";
				$kegiatan1=$this->db->query("SELECT * FROM web_kegiatan where tgl='$skrg' and absen='$sesiku'");
				foreach ($kegiatan1->result() as $row2) {
					$giat=$row2->kegiatan;
					$jam=$row2->jam;
					$kelas=$row2->id_kelas;
					$keterangan=$row2->materi;
					?>
					<tr>
					<td><?php echo $jam;?></td><td><?php echo $giat;?></td><td><?php echo $kelas;?></td><td><?php echo $keterangan;?></td>
					</tr>
				<?php } ?>
				</table>
			</td>
		</tr>
	<?php } ?>
	</table>
</div>

	<?php

	exit();
	}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
?>
