<?php
include_once "library/inc.sesadmin.php";
include_once "library/inc.library.php";

# TOMBOL SIMPAN DIKLIK
if(isset($_POST['btnSimpan'])){
	// Skrip validasi dan Simpan data
	# Baca Variabel Form
	$txtNis			= $_POST['txtNis'];
	$txtNama		= $_POST['txtNama'];
	$txtTempatLahir	= $_POST['txtTempatLahir'];
	$txtTanggal		= InggrisTgl($_POST['txtTanggal']);
	$txtAlamat		= $_POST['txtAlamat'];
	$txtNamaOrtu	= $_POST['txtNamaOrtu'];
	$txthportu		= $_POST['txthportu'];
	$cmbStatus		= $_POST['cmbStatus'];
	
	# Validasi form, jika kosong sampaikan pesan error
	$pesanError = array();
	if(trim($txtNis)=="") {
		$pesanError[] = "Data <b>NIS Siswa</b> tidak boleh kosong, harus diisi !";		
	}
	

	# JIKA ADA PESAN ERROR DARI VALIDASI
	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> <br>"; 
	}
	else {
		# SIMPAN DATA KE DATABASE. Jika tidak menemukan pesan error, simpan data ke database			
		// Membuat Kode Siswa
		$kodeBaru	= buatKode("siswa", "S");

		# SKRIP UNTUK MENYIMPAN FOTO/GAMBAR
		if (! empty($_FILES['namaFile']['tmp_name'])) {
			// Membaca nama file foto/gambar
			$file_foto = $_FILES['namaFile']['name'];
			$file_foto = stripslashes($file_foto);
			$file_foto = str_replace("'","",$file_foto);
			
			// Simpan gambar
			$file_foto = $file_foto;
			copy($_FILES['namaFile']['tmp_name'],"foto/".$file_foto);
		}
		else {
			// Jika tidak ada foto/gambar
			$file_foto = "";
		}

		// Simpan data dari form ke Database
		$mySql	= "INSERT INTO siswa ( nis, nama_siswa,tempat_lahir, tanggal_lahir, alamat, nama_ortu, no_hp_ortu, status, foto)
							VALUES ( '$txtNis', '$txtNama', '$txtTempatLahir', '$txtTanggal',  '$txtAlamat', '$txtNamaOrtu','$txthportu','$cmbStatus', '$file_foto)";		
		$myQry	= mysql_query($mySql, $koneksidb) or die ("Gagal query".mysql_error());
		if($myQry){
			// Setelah data disimpan, Refresh
			echo "<meta http-equiv='refresh' content='0; url=?open=Upload-Foto'>";
		}
		exit;
	}	
}

# MEMBUAT NILAI DATA PADA FORM
$dataNis		= isset($_POST['txtNis']) ? $_POST['txtNis'] : '';
$dataNama		= isset($_POST['txtNama']) ? $_POST['txtNama'] : '';
$dataTempatLahir= isset($_POST['txtTempatLahir']) ? $_POST['txtTempatLahir'] : '';
$dataTanggal	= isset($_POST['txtTanggal']) ? $_POST['txtTanggal'] : date('d-m-Y');
$dataAlamat		= isset($_POST['txtAlamat']) ? $_POST['txtAlamat'] : '';
$dataNamaOrtu	= isset($_POST['txtNamaOrtu']) ? $_POST['txtNamaOrtu'] : '';
$datahportu		= isset($_POST['txthportu']) ? $_POST['txthportu'] : '';
$dataStatus		= isset($_POST['rbStatus']) ? $_POST['rbStatus'] : '';
?>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post" name="form1" enctype="multipart/form-data" target="_self">
  <table class="table-list" width="700" border="0" cellspacing="1" cellpadding="3">
    <tr>
      <td colspan="3" bgcolor="#F5F5F5"><strong>TAMBAH DATA SISWA</strong> </td>
    </tr>
	
	<tr>
      <td><strong>Nis </strong></td>
	  <td><b>:</b></td>
	  <td><input name="txtNis" value="<?php echo $dataNis; ?>"  size="40" maxlength="60"/></td>
    </tr>
	<tr>
      <td><strong>Nama Siswa </strong></td>
	  <td><b>:</b></td>
	  <td><input name="txtNama" value="<?php echo $dataNama; ?>"  size="40" maxlength="60"/></td>
    </tr>
	<tr>
      <td><strong>Tempat, Tgl Lahir </strong></td>
      <td><strong>:</strong></td>
      <td><input name="txtTempatLahir" type="text" value="<?php echo $dataTempatLahir; ?>" size="40" maxlength="100"> ,
       <input name="txtTanggal" type="text" class="tcal" value="<?php echo $dataTanggal; ?>" size="20" maxlength="20" /></td>
    </tr>
	<tr>
      <td><strong>Alamat</strong></td>
	  <td><b>:</b></td>
	  <td><input name="txtAlamat" value="<?php echo $dataAlamat; ?>"  size="80" maxlength="60"/></td>
    </tr>
	<tr>
      <td><strong>Nama Orangtua </strong></td>
	  <td><b>:</b></td>
	  <td><input name="txtNamaOrtu" value="<?php echo $dataNamaOrtu; ?>"  size="40" maxlength="60"/></td>
    </tr>
	<tr>
      <td><strong>No.Hp. Orangtua </strong></td>
	  <td><b>:</b></td>
	  <td><input name="txthportu" value="<?php echo $datahportu; ?>"  size="40" maxlength="60"/></td>
    </tr>
	    <tr>
      <td><strong>Foto Siswa </strong></td>
      <td><strong>:</strong></td>
      <td><input name="namaFile" type="file" size="60" /></td>
    </tr>
   
    <tr>
      <td><strong>Status Aktif </strong></td>
      <td><strong>:</strong></td>
      <td><select name="cmbStatus">
        <option value="KOSONG">....</option>
        <?php
		   $pilihan = array("Aktif", "Lulus", "Keluar");
          foreach ($pilihan as $status) {
            if ($dataStatus==$status) {
                $cek="selected";
            } else { $cek = ""; }
            echo "<option value='$status' $cek>$status</option>";
          }
          ?>
      </select></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td><input name="btnSimpan" type="submit" value=" Simpan "></td>
    </tr>
  </table>
</form>
