<!-- basic form start -->
<div class="col-12 mt-5">
	<div class="card">
		<div class="card-body">
			<h4 class="header-title">Tambah Data Pengguna</h4>
			<form method="post" enctype="multipart/form-data">
				<div class="form-group">
					<?php
					$arrLevel	= array("Kasir","Admin");

				# KETIKA DIKLIK
					if(isset($_POST['btnSave']))
					{
				# PERIKSA VARIABLE
						$txtNama		= $_POST['txtNama'];
						$txtUser		= $_POST['txtUser'];
						$txtPassword	= $_POST['txtPassword'];
						$cmbLevel		= $_POST['cmbLevel'];
						$file_foto 		= $_FILES['namaFile']['name'];

				# SAMPAIKAN PESAN ERROR
						$pesanError = array();
						if(trim($_POST['txtNama'])=="") 
						{
							$pesanError[] = "Data <b>NAMA</b> tidak boleh kosong, harus diisi !";		
						}	
						if(trim($_POST['txtUser'])=="") 
						{
							$pesanError[] = "Data <b>USER ID</b> tidak boleh kosong, harus diisi !";		
						}
						if(trim($_POST['txtPassword'])=="") 
						{
							$pesanError[] = "Data <b>PASSWORD</b> tidak boleh kosong, harus diisi !";		
						}
						if(trim($_POST['cmbLevel'])=="") 
						{
							$pesanError[] = "Data <b>LEVEL</b> tidak boleh kosong, harus diisi !";		
						}
				# VALIDASI USERNAME, JIKA SUDAH ADA DITOLAK
						$cekSql	= "SELECT userid FROM user WHERE userid='$txtUser'";
						$cekQry	= mysql_query($cekSql, $koneksidb) or die ("Eror Query".mysql_error()); 
						if(mysql_num_rows($cekQry)>=1)
						{
							$pesanError[] = "Maaf, User ID <b> $txtUser </b> sudah ada, ganti dengan yang lain";
						}	

				# JIKA ADA PESAN ERROR MAKA TAMPILKAN ERROR
						if (count($pesanError)>=1 )
						{
							echo "<div class='mssgBox'>";
							// echo "<img src='images/attention.png'> <br><hr>";
							$noPesan=0;
							foreach ($pesanError as $indeks=>$pesan_tampil) 
							{ 
								$noPesan++;
								echo "<div class='alert alert-danger alert-dismissible fade show' role='alert'>";
								echo "&nbsp;&nbsp; $noPesan. $pesan_tampil<br>";
								echo "<button type='button' class='close' data-dismiss='alert' aria-label='Close'>";
								echo "<span class='fa fa-times'></span>
								</button>
								</div>";	
							} 
							echo "</div> <br>"; 
						}
						else
						{
				# COPY FILE GAMBAR KE FOLDER FOTO
							if (!empty($_FILES['namaFile']['tmp_name'])) 
							{
								$gambar = $file_foto;
								copy($_FILES['namaFile']['tmp_name'],"../foto/".$gambar);
							}
				
					# MASUKKAN DATA KE SQL			
							$mySql	= "INSERT INTO user ( nama, userid, password, level, foto) 
							VALUES ( '$txtNama', '$txtUser', '".md5($txtPassword)."', '$cmbLevel', '$file_foto')";

							$myQry	= mysql_query($mySql, $koneksidb) or die ("Gagal query".mysql_error());
							if($myQry)
							{
								echo "<meta http-equiv='refresh' content='0; url=?page=Data-User&Act=Sucsses'>";
							}
							exit;
						}
					}		

					?> 

				</div>
				<div class="form-row">
					<div class="col-md-3 mb-3">
						<label for="exampleInputEmail1"><b>User ID</b></label>
						<input type="text" class="form-control" name="txtUser" placeholder="Masukan User ID">
					</div>
					<div class="col-md-6 mb-3">
						<label for="exampleInputPassword1"><b>Nama Lengkap</b></label>
						<input type="text" class="form-control" name="txtNama" placeholder="Masukan Nama Pengguna">
					</div>
					<div class="col-md-3 mb-3">
						<label for="exampleInputPassword1"><b>Password</b></label>
						<input type="password" class="form-control" name="txtPassword" placeholder="Masukan Password">
					</div>
				</div>

				<div class="form-row">
					<div class="col-md-3 mb-3">
						<label class="col-form-label"><b>Level</b></label>
						<select class="form-control form-control-lg" name="cmbLevel">
							<!-- <option value="BLANK">--Pilih--</option> -->
							<?php
							foreach ($arrLevel as $index => $value) 
							{
								if ($value==$dataLevel) 
								{
									$cek="selected";
								} else { $cek = ""; }
								echo "<option value='$value' $cek>$value</option>";
							}
							?>
						</select>
					</div>
					<div class="col-md-9 mb-3">
						<label class="col-form-label"><b>Foto</b></label>
						<div class="custom-file">
							<input type="file" class="custom-file-input" name="namaFile">
							<label class="custom-file-label" for="inputGroupFile01">Choose file</label>
						</div>
					</div>
				</div>
				<button type="submit" class="btn btn-primary col-12" name="btnSave"><b>Simpan Data</b></button>
			</form>
		</div>
	</div>
</div>
<!-- basic form end -->