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

# KETIKA DIKLIK
					if(isset($_POST['btnSave']))
					{
	# PERIKSA VARIABLE
						$txtnis				= $_POST['txtnis'];
						$txtnamasiswa		= $_POST['txtnamasiswa'];
						$txtstatus			= $_POST['txtstatus'];
						$txtkdkelas		= $_POST['txtkdkelas'];
						$file_foto 			= $_FILES['namaFile']['name'];

	# SAMPAIKAN PESAN ERROR
						$pesanError = array();
						if(trim($_POST['txtnis'])=="") 
						{
							$pesanError[] = "<b>NIS</b> tidak boleh kosong, harus diisi !";		
						}	
						if(trim($_POST['txtnamasiswa'])=="") 
						{
							$pesanError[] = "<b>NAMA SISWA</b> tidak boleh kosong, harus diisi !";		
						}
						if(trim($_POST['txtkdkelas'])=="") 
						{
							$pesanError[] = "<b>KODE KELAS</b> tidak boleh kosong, harus diisi !";		
						}
						if(trim($_POST['txtstatus'])=="" || trim($_POST['txtstatus'])=="BLANK") 
						{
							$pesanError[] = "<b>STATUS</b> tidak boleh kosong, harus diisi !";		
						}

	# VALIDASI SISWA, JIKA SUDAH ADA DITOLAK
						$cekSql	= "SELECT nis FROM siswa WHERE nis='$txtnis'";
						$cekQry	= mysql_query($cekSql, $koneksidb) or die ("Eror Query".mysql_error()); 
						if(mysql_num_rows($cekQry)>=1)
						{
							$pesanError[] = "Maaf, NIS <b> $txtnis </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 "&nbsp;&nbsp; $noPesan. $pesan_tampil<br>";
								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>"; 


						}
						else
						{

		# COPY FILE GAMBAR KE FOLDER FOTO
							if (! empty($_FILES['namaFile']['tmp_name'])) 
							{
								$gambar = $file_foto;
								copy($_FILES['namaFile']['tmp_name'],"../foto_siswa/".$gambar);
							}

		# MASUKKAN DATA KE SQL			
							$mySql	= "INSERT INTO siswa (nis, nama_siswa, status, foto) 
							VALUES ( '$txtnis', '$txtnamasiswa', '$txtstatus', '$file_foto')";

							$myQry	= mysql_query($mySql, $koneksidb) or die ("Gagal query".mysql_error());
							if($myQry)
							{
								$mySql2	= "INSERT INTO kelas_siswa (nis, kdkelas) 
								VALUES ( '$txtnis', '$txtkdkelas')";

								$myQry2	= mysql_query($mySql2, $koneksidb) or die ("Gagal query".mysql_error());
								echo "<meta http-equiv='refresh' content='0; url=?page=Data-Siswa&Act=Sucsses'>";
							}
							exit;
						}
					}		
					?> 
				</div>
				<div class="form-row">
					<div class="col-md-2 mb-3">
						<label for="exampleInputEmail1"><b>NIS</b></label>
						<input type="text" class="form-control" name="txtnis" placeholder="Masukan NIS">
					</div>
					<div class="col-md-5 mb-3">
						<label for="exampleInputPassword1"><b>Nama Siswa</b></label>
						<input type="text" class="form-control" name="txtnamasiswa" placeholder="Masukan Nama Siswa">
					</div>
					<div class="col-md-3 mb-3">
						<label for="exampleInputPassword1"><b>Kode Kelas</b></label>
						<input type="text" class="form-control" name="txtkdkelas" placeholder="Masukan Kode Kelas">
					</div>
					<div class="col-md-2 mb-3">
						<label class="col-form-label"><b>Status</b></label>
						<select class="form-control" name="txtstatus">
							<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>

				<div class="input-group mb-3">
					<div class="input-group-prepend">
						<span class="input-group-text">Pilih Foto</span>
					</div>
					<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>
				<button type="submit" class="btn btn-primary col-12" name="btnSave"><b>Simpan Data</b></button>
			</form>
		</div>
	</div>
</div>
<!-- basic form end -->