<?php
	include_once "library/inc.sesadmin.php";
	include_once "library/inc.library.php";
	
	$arrLevel	= array("Siswa","Admin");
	
	# TOMBOL SIMPAN DIKLIK
	if(isset($_POST['btnSimpan']))
	{
		$txtNama		= $_POST['txtNama'];
		$txtUser		= $_POST['txtUser'];
		$cmbLevel		= $_POST['cmbLevel'];
	
	
		# Validasi form, jika kosong sampaikan pesan error
		$pesanError = array();
		if(trim($txtNama)=="") 
		{
			$pesanError[] = "Data <b>Nama User</b> tidak boleh kosong, harus diisi !";		
		}
		if(trim($txtUser)=="") 
		{
			$pesanError[] = "Data <b>User </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			
			// Membaca Kode dari form hidden
			$Kode	= $_POST['txtKode'];
		
			
			// Simpan data dari form ke Database
			$mySql	= "UPDATE userweb SET nama='$txtNama', userid='$txtUser', level='$cmbLevel' WHERE id='".$_POST['txtKode']."'";		
			$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=?page=Data-Userweb&Act=Sucsses'>";
			}
			exit;
		}	
	}

	// Skrip membaca variable dari URL (Kode dikirim dari menu Edit)
	$Kode	= isset($_GET['Kode']) ?  $_GET['Kode'] : $_POST['txtKode']; 

	# TAMPILKAN DATA LOGIN UNTUK DIEDIT
	$mySql 	= "SELECT * FROM userweb WHERE id='$Kode'";
	$myQry 	= mysql_query($mySql, $koneksidb)  or die ("Query ambil data salah : ".mysql_error());
	$myData = mysql_fetch_array($myQry); 

	# MEMBUAT NILAI DATA PADA FORM
	// Masukkan data ke dalam variabel, supaya bisa dipanggil di Form
	$dataKode		= $myData['id'];
	$dataNama		= isset($_POST['txtNama']) ? 	$_POST['txtNama'] 	: $myData['nama'];
	$dataUser		= isset($_POST['txtUser']) ? 	$_POST['txtUser'] 	: $myData['userid'];
	$dataLevel 		= isset($_POST['cmbLevel']) ?  	$_POST['cmbLevel'] 	: $myData['level'];
	
?>


<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>UBAH DATA USER</strong> </td>
    </tr>
    <tr>
      <td width="184"><strong>ID</strong></td>
      <td width="5"><strong>:</strong></td>
      <td width="489"><input name="textfield" type="text" value="<?php echo $dataKode; ?>" size="10" maxlength="10" readonly="readonly">
      <input name="txtKode" type="hidden" value="<?php echo $dataKode; ?>" /></td>
    </tr>
    <tr>
      <td><strong>Nama Lengkap </strong></td>
      <td><strong>:</strong></td>
      <td><input name="txtNama" type="text" value="<?php echo $dataNama; ?>" size="80" maxlength="100"></td>
    </tr>
	<tr>
      <td><strong>User ID</strong></td>
      <td><strong>:</strong></td>
      <td><input name="txtUser" type="text" value="<?php echo $dataUser; ?>" size="30" maxlength="20"></td>
    </tr>
    <tr height="30">
	  <td><strong>Level</strong></td>
	  <td><b>:</b></td>
	  <td><select name="cmbLevel">
        <option value="BLANK"> </option>
        <?php 
        foreach ($arrLevel as $index => $value) {
            if ($value==$dataLevel) {
                $cek="selected";
            } else { $cek = ""; }
            echo "<option value='$value' $cek>$value</option>";
        }
         ?>
      </select></td>
    </tr>
   
   
   
   
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td><input name="btnSimpan" type="submit" value=" Simpan "></td>
    </tr>
  </table>
</form>
