<?php 
	session_start();
	if($_SESSION['level']==""){
	header("location:./index.php?alert=belum_login");
}?>
<?php 
	include "koneksi.php";   
	include "fungsi_romawi.php";
	
    $bulan = date('n');
    $romawi = getRomawi($bulan);
    $tahun = date ('Y');
    
    
    // membaca kode / nilai tertinggi dari penomoran yang ada didatabase berdasarkan tanggal
    $query = "SELECT *  FROM pengantar WHERE month(berlaku_mulai)= ".$bulan." ORDER BY nomor_surat DESC LIMIT 1 ";
    $hasil = mysqli_query($koneksi,$query);
    $data  = mysqli_fetch_array($hasil);
	if($data != null) {
		$no = $data['kode'];
		}else{
		$no = 0;
		}
	
		//untuk query user
	$username = $_SESSION['username'];
	$queryUser = "SELECT *  FROM user WHERE username = '$username'";
    $hasilUser = mysqli_query($koneksi,$queryUser);
    $dataUser  = mysqli_fetch_array($hasilUser);
	$nomor = "/RT".$dataUser['rt']."RW".$dataUser['rw']."/".$romawi."/".$tahun;
   
    $noUrut= $no + 1;
    
    //membuat Nomor Surat Otomatis dengan awalan depan 0 misalnya , 01,02 
    //jika ingin 003 ,tinggal ganti %03
    $kode =  sprintf("%02s" , $noUrut);
    $nomorbaru = $kode.$nomor;
	
	
?>
<?php

    //jika kita klik cari, maka yang tampil query cari ini
    if(isset($_GET['kata_cari'])) {
     //menampung variabel kata_cari dari form pencarian
     $kata_cari = $_GET['kata_cari'];

     //mencari data dengan menggunakan query
     $query = "SELECT * FROM penduduk WHERE nik like '%".$kata_cari."%' ORDER BY no_kk ASC";
    } else {
     //jika tidak ada pencarian, default yang dijalankan tampa query
     $query = "";
    }
	
	//variabel
	$nik = "";
	$noKK = "";
	$nmlengkp = "";
	$alamat = "";
	$rt = "";
	$rw = "";
	
	if ($query != "") {
		$row = mysqli_fetch_array(mysqli_query($koneksi,$query));
		if($row != NULL) {
			$nik = $row['nik'];
			$noKK = $row['no_kk'];
			$nmlengkp = $row['nama_lengkap'];
			$alamat = $row['alamat'];
			$rt = $row['rt'];
			$rw = $row['rw'];
		}
	}
?>
	
	
<head>
	<title>Pengantar RT</title>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
<!--===============================================================================================-->
	<link rel="icon" type="image/png" href="ContactFrom_v15/images/icons/favicon.ico"/>
<!--===============================================================================================-->
	<link rel="stylesheet" type="text/css" href="ContactFrom_v15/vendor/bootstrap/css/bootstrap.min.css">
<!--===============================================================================================-->
	<link rel="stylesheet" type="text/css" href="ContactFrom_v15/fonts/font-awesome-4.7.0/css/font-awesome.min.css">
<!--===============================================================================================-->
	<link rel="stylesheet" type="text/css" href="ContactFrom_v15/fonts/Linearicons-Free-v1.0.0/icon-font.min.css">
<!--===============================================================================================-->
	<link rel="stylesheet" type="text/css" href="ContactFrom_v15/vendor/animate/animate.css">
<!--===============================================================================================-->
	<link rel="stylesheet" type="text/css" href="ContactFrom_v15/vendor/css-hamburgers/hamburgers.min.css">
<!--===============================================================================================-->
	<link rel="stylesheet" type="text/css" href="ContactFrom_v15/vendor/animsition/css/animsition.min.css">
<!--===============================================================================================-->
	<link rel="stylesheet" type="text/css" href="ContactFrom_v15/vendor/select2/select2.min.css">
<!--===============================================================================================-->
	<link rel="stylesheet" type="text/css" href="ContactFrom_v15/vendor/daterangepicker/daterangepicker.css">
<!--===============================================================================================-->
	<link rel="stylesheet" type="text/css" href="ContactFrom_v15/css/util.css">
	<link rel="stylesheet" type="text/css" href="ContactFrom_v15/css/main.css">
<!--===============================================================================================-->
</head>
<body>


	<div class="container-contact100">
		<div class="contact100-map" id="google_map" data-map-x="110.7999477" data-map-y="-7.5794667" data-pin="images/icons/map-marker.png" data-scrollwhell="0" data-draggable="1"></div>

		<div class="wrap-contact100">
			<div class="contact100-form-title" style="background-image: url(ContactFrom_v15/images/bg-01.jpg);">
				<span class="contact100-form-title-1">
					Pengantar RT
				</span>

				<span class="contact100-form-title-2">
					Mohon Diperiksa dengan seksama
				</span>
			</div>
			<form method="GET" action="surat_pengantar2.php" >
  <label>Pencarian BY NIK:	 </label>
  <input type="text" name="kata_cari" value="<?php if(isset($_GET['kata_cari'])) { echo $_GET['kata_cari']; } ?>"  />
  <button type="submit">Cari</button>
 </form>
			

	  <form class="contact100-form validate-form" action="" method="POST">
		 
				<div class="wrap-input100 validate-input" data-validate="Name is required">
					<span class="label-input100">Nomor Surat:</span>
					<input class="input100" type="text" name="nomor_surat" readonly value="<?php echo $nomorbaru ?>" placeholder="">
					<span class="focus-input100"></span>
				</div>

				<div class="wrap-input100 validate-input" data-validate = "">
					<span class="label-input100">NIK:</span>
					<input class="input100" type="text" name="nik" placeholder="Enter NIK"	value="<?php echo $nik; ?>">
					<span class="focus-input100"></span>
				</div>

				<div class="wrap-input100 validate-input" data-validate="Nomor KK is required">
					<span class="label-input100">Nomor KK:</span>
					<input class="input100" type="text" name="no_kk" placeholder="Enter Nomor KK"	value="<?php echo $noKK; ?>">
					<span class="focus-input100"></span>
				</div>
				
			  <div class="wrap-input100 validate-input" data-validate="Nama is required">
					<span class="label-input100">Nama:</span>
					<input class="input100" type="text" name="nama" placeholder="Enter Nama"	value="<?php echo $nmlengkp; ?>">
					<span class="focus-input100"></span>
				</div>
				
				<div class="wrap-input100 validate-input" data-validate="Alamat is required">
				  <span class="label-input100">Alamat:</span>
				  <input class="input100" type="text" name="alamat" placeholder="Enter Alamat"	value="<?php echo $alamat; ?> Rt <?php echo $rt; ?>/<?php echo $rw; ?>">
					<span class="focus-input100"></span>
				</div>
				
			  <div class="wrap-input100 validate-input" data-validate="Janis Kelamin is required">
					<span class="label-input100">Jenis Kelamin:</span>
				    <td><select name="jenis_kelamin">
						<?php
							if($row['jenis_kelamin'] == "LAKI-LAKI") {
								$manSelected = "selected";
								$girlSelected = "";
							}else if($row['jenis_kelamin'] == "PEREMPUAN"){
								$manSelected = "";
								$girlSelected = "selected";
							}else{
								$manSelected = "";
								$girlSelected = "";
							}
						?>
					  <option value="-">- Jenis Kelamin -</option>
           			  <option value="LAKI-LAKI" <?php echo $manSelected; ?>>Laki-laki</option>
           			  <option value="PEREMPUAN" <?php echo $girlSelected; ?>>Perempuan</option>
						  </select></td>
            	<span class="focus-input100"></span>
	   		    </div>
				
				<div class="wrap-input100 validate-input" data-validate="Janis Kelamin is required">
					<span class="label-input100">Kewarga Negaraan: </span>
				    <select name="kewarganegaraan">
                    <option value="-">- Warga Negara -
						<option value="WNI">WNI</option>
              			<option value="WNA">WNA</option>
						</select>		
            	<span class="focus-input100"></span>
				</div>
				<div class="wrap-input100 validate-input" data-validate="Agama is required">
					<span class="label-input100">Agama: </span>
				    <select name="agama">
                    <option value="-">- Agama -
						<option value="Islam">Islam</option> 
						<option value="Kristen">Kristen</option>
						<option value="Khatolik">Khatolik</option>
              			<option value="Hindu">Hindu</option>
						<option value="Budha">Budha</option>
						</select>		
            	<span class="focus-input100"></span>
				</div>
		 	 <div class="wrap-input100 validate-input" data-validate="Pekerjaan required">
				  <span class="label-input100">Pekerjaan:</span>
				  <input class="input100" type="text" name="pekerjaan" placeholder="Enter Pekerjaan">
					<span class="focus-input100"></span>
				</div>
		  
		   <div class="wrap-input100 validate-input" data-validate="Keperluan is required">
				  <span class="label-input100">Keperluan:</span>
				  <input class="input100" type="text" name="keperluan" placeholder="Enter Keperluan">
					<span class="focus-input100"></span>
				</div>
		  
		   <div class="wrap-input100 validate-input" data-validate="Keterangan lain is required">
				  <span class="label-input100">Keterangan:</span>
				  <input class="input100" type="text" name="keterangan" placeholder="Enter Keterangan">
					<span class="focus-input100"></span>
				</div>
		  
		  
		   <div class="wrap-input100 validate-input" data-validate="Berlaku Mulai is required">
				  <span class="label-input100">Berlaku Mulai:</span>
				  <input class="input100" type="date" name="berlaku_mulai" placeholder="Enter Berlaku Mulai">
					<span class="focus-input100"></span>
				</div>
		  
		  <div class="wrap-input100 validate-input" data-validate="QR">
				  <span class="label-input100">QR:</span>
				  <?php
				$file_name = str_replace("/","-",$nomorbaru).".png";
		include "php-qrcode-library/qrlib.php"; 
		/*create folder*/
		$tempdir="img-qrcode/";
		if (!file_exists($tempdir))
		mkdir($tempdir, 0755);
		//$file_name=date("Ymd").rand().".png";	
		$file_path = $tempdir.$file_name;
		
		QRcode::png("Diverifikasi Oleh ".$dataUser['username']." RT ".$dataUser['rt']." RW ".$dataUser['rw'], $file_path, "H", 6, 4);
		/* param (1)qrcontent,(2)filename,(3)errorcorrectionlevel,(4)pixelwidth,(5)margin */
		
		echo "<p class='result'>Result :</p>";
		echo "<p><img src='".$file_path."' /></p>";
    	?>
					<span class="focus-input100"></span>
				</div>
		  
		  
		  <div class="container-contact100-form-btn">
			  <button class="contact100-form-btn">
						<span>
							
							<input type="submit" name="proses" >
						</span>
					</button>
			  <button class="contact100-form-btn">
						<span>
							<input type="reset" name="reset">
						</span>
					</button>
			  <button class="contact100-form-btn">
						<span>
							<a href="login/logout.php">LOGOUT</a>
						</span>
					</button>
			 
			 
			  
				</div>
			</form>
	
		</div>
	</div>



	<div id="dropDownSelect1"></div>

<!--===============================================================================================-->
	<script src="ContactFrom_v15/vendor/jquery/jquery-3.2.1.min.js"></script>
<!--===============================================================================================-->
	<script src="ContactFrom_v15/vendor/animsition/js/animsition.min.js"></script>
<!--===============================================================================================-->
	<script src="ContactFrom_v15/vendor/bootstrap/js/popper.js"></script>
	<script src="ContactFrom_v15/vendor/bootstrap/js/bootstrap.min.js"></script>
<!--===============================================================================================-->
	<script src="ContactFrom_v15/vendor/select2/select2.min.js"></script>
<!--===============================================================================================-->
	<script src="ContactFrom_v15/vendor/daterangepicker/moment.min.js"></script>
	<script src="ContactFrom_v15/vendor/daterangepicker/daterangepicker.js"></script>
<!--===============================================================================================-->
	<script src="ContactFrom_v15/vendor/countdowntime/countdowntime.js"></script>
<!--===============================================================================================-->
	<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAKFWBqlKAGCeS1rMVoaNlwyayu0e0YRes"></script>
	<script src="ContactFrom_v15/js/map-custom.js"></script>
<!--===============================================================================================-->
	<script src="ContactFrom_v15/js/main.js"></script>

<!-- Global site tag (gtag.js) - Google Analytics -->
	<script async src="https://www.googletagmanager.com/gtag/js?id=UA-23581568-13"></script>
	<script>
	  window.dataLayer = window.dataLayer || [];
	  function gtag(){dataLayer.push(arguments);}
	  gtag('js', new Date());

	  gtag('config', 'UA-23581568-13');
	</script>
		<?php
		$data = mysqli_query($koneksi,"select * from pengantar order by nomor_surat desc");
while($d = mysqli_fetch_array($data)){?>

<?php
};
?>
	<?php
	if(isset($_POST['proses'])){
		
		
		$save = mysqli_query($koneksi,"insert into pengantar set
		kode = '$noUrut',
		nomor_surat = '$_POST[nomor_surat]',
		nik = '$_POST[nik]',
		no_kk = '$_POST[no_kk]',
		nama = '$_POST[nama]',
		alamat = '$_POST[alamat]',
		jenis_kelamin = '$_POST[jenis_kelamin]',
		kewarganegaraan = '$_POST[kewarganegaraan]',
		agama = '$_POST[agama]',
		pekerjaan = '$_POST[pekerjaan]',
		keperluan = '$_POST[keperluan]',
		keterangan = '$_POST[keterangan]',
		berlaku_mulai = '$_POST[berlaku_mulai]',
		barcode = '$file_name'
		");
		if ($save == 1) {
			echo "<script>alert('Data berhasil di tambahkan!');history.go(-1);</script>";
			} else{
		echo "<script>alert('Gagal di tambahkan!');history.go(-1);</script>";
		}
}
	
?>

</body>
</html>
