<?php

$arrLevel	= array("Kasir","Admin");

if($_GET) 
{
	if(isset($_POST['btnSave']))
	{
				
		# BACA VARIABEL FORM
		$txtnis				= $_POST['txtnis'];
		$txtnis				= str_replace("'","&acute;",$txtnis);
		$txtnamasiswa		= $_POST['txtnamasiswa'];
		$txtnamasiswa		= str_replace("'","&acute;",$txtnamasiswa);
		$txttempatlahir		= $_POST['txttempatlahir'];
		$txttempatlahir		= str_replace("'","&acute;",$txttempatlahir);
		$txttanggallahir	= $_POST['txttanggallahir'];
		$txttanggallahir	= str_replace("'","&acute;",$txttanggallahir);
		$txtnamaortu		= $_POST['txtnamaortu'];
		$txtnamaortu		= str_replace("'","&acute;",$txtnamaortu);
		$txthportu			= $_POST['txthportu'];
		$txthportu			= str_replace("'","&acute;",$txthportu);
		$txtstatus			= $_POST['txtstatus'];
		$txtstatus			= str_replace("'","&acute;",$txtstatus);
		$file_foto 			= $_FILES['namaFile']['name'];				
					
		$sqlSave="UPDATE user SET nama='$txtNama', userid='$txtUser', $sqlSub, level='$cmbLevel' WHERE id='".$_POST['txtKode']."'";
		$qrySave=mysql_query($sqlSave, $koneksidb);
		if($qrySave)
			{
				echo "<meta http-equiv='refresh' content='0; url=?page=Data-User&Act=Sucsses'>";
			}
	}  

	# TAMPILKAN DATA LOGIN UNTUK DIEDIT
	$KodeEdit= isset($_GET['Kode']) ?  $_GET['Kode'] : $_POST['txtKode']; 
	$sqlShow = "SELECT * FROM user WHERE id='$KodeEdit'";
	$qryShow = mysql_query($sqlShow, $koneksidb)  or die ("Query ambil data user salah : ".mysql_error());
	$dataShow = mysql_fetch_array($qryShow);

	# MASUKKAN DATA KE VARIABEL
	$dataKode	= $dataShow['id'];
	$dataNama	= isset($dataShow['nama']) ?  $dataShow['nama'] : $_POST['txtNama'];
	$dataUser	= isset($dataShow['userid']) ?  $dataShow['userid'] : $_POST['txtUser'];
	$dataUserLm	= $dataShow['userid'];
	$dataPass	= isset($dataShow['password']) ?  $dataShow['password'] : $_POST['txtPassBaru'];
	$dataLevel = isset($dataShow['level']) ?  $dataShow['level'] : $_POST['cmbLevel'];
} 
?>
<form action="?page=Edit-User&Act=Save" method="post" name="frmadd">
<table class="table-common" width="70%" style="margin-top:0px;" border="0">
	<tr>
	  <th colspan="3"><h2>UBAH DATA USER </h2> </th>
	</tr>
	<tr height="30">
      <td width="150"><strong>Nama Lengkap </strong></td>
	  <td><b>:</b></td>
	  <td><input name="txtNama" value="<?php echo $dataNama; ?>"  size="40" maxlength="60"/>
          <input name="txtKode" type="hidden" value="<?php echo $dataKode; ?>" /></td>
    </tr height="30">
	<tr>
	  <td><strong>User ID </strong></td>
	  <td><b>:</b></td>
	  <td><input name="txtUser"  value="<?php echo $dataUser; ?>"  size="40" maxlength="30"/>
      <input name="txtUserLm" type="hidden" value="<?php echo $dataUserLm; ?>" /></td></tr>
	<tr height="30">
	  <td><strong>Password</strong></td>
	  <td><b>:</b></td>
	  <td><input name="txtPassBaru" type="password"  value="" size="40" maxlength="30"/>
      <input name="txtPassLama" type="hidden" value="<?php echo $dataPass; ?>" /></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 type="submit" name="btnSave" value=" SIMPAN " style="cursor:pointer;"></td>
    </tr>
</table>
</form>



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

# Opsi Level Login
$arrLevel	= array("Kasir","Admin");

if($_GET) {
	if(isset($_POST['btnSave'])){
		$message = array();
		if (trim($_POST['txtnis'])=="") {
			$message[] = "NIS tidak boleh kosong !";		
		}
		if (trim($_POST['txtnamasiswa'])=="") {
			$message[] = "Nama siswa tidak boleh kosong !";		
		}
		
		
		# Baca Variabel Form
		$txtnis	= $_POST['txtnis'];
		$txtnis 	= str_replace("'","&acute;",$txtnis);
		$txtnislm	= $_POST['txtnislm'];
		
		$txtnamasiswa	= $_POST['txtnamasiswa'];
		$txtnamasiswa	= str_replace("'","&acute;",$txtnamasiswa);
		
		$txttempatlahir	= $_POST['txttempatlahir'];
		$txttempatlahir	= str_replace("'","&acute;",$txttempatlahir);
		
		$txttanggallahir	= $_POST['txttanggallahir'];
		$txttanggallahir	= str_replace("'","&acute;",$txttanggallahir);
		
		$txtalamat	= $_POST['txtalamat'];
		$txtalamat	= str_replace("'","&acute;",$txtalamat);
		
		$txtnamaortu	= $_POST['txtnamaortu'];
		$txtnamaortu	= str_replace("'","&acute;",$txtnamaortu);
		
		$txthportu	= $_POST['txthportu'];
		$txthportu	= str_replace("'","&acute;",$txthportu);
		

		
				
		# Jika jumlah error message tidak ada
		if(count($message)==0){			
						
			$sqlSave="UPDATE siswa SET nis='$txtnis', nama_siswa='$txtnamasiswa', tempat_lahir='$txttempatlahir', tanggal_lahir='$txttanggallahir', 
						alamat='$txtalamat', nama_ortu='$txtnamaortu', no_hp_ortu='$txthportu  WHERE id='".$_POST['txtnis']."'";
			$qrySave=mysql_query($sqlSave, $koneksidb);
			if($qrySave){
				echo "<meta http-equiv='refresh' content='0; url=?page=Data-Siswa&Act=Sucsses'>";
			}
		}	
		
		# JIKA ADA PESAN ERROR DARI VALIDASI
		// (Form Kosong, atau Duplikat ada), Ditampilkan lewat kode ini
		if (! count($message)==0 ){
            echo "<div class='mssgBox'>";
			echo "<img src='images/attention.png' class='imgBox'> <hr>";
				$Num=0;
				foreach ($message as $indeks=>$pesan_tampil) { 
				$Num++;
					echo "&nbsp;&nbsp;$Num. $pesan_tampil<br>";	
				} 
			echo "</div> <br>"; 
		}
	} // End if($_POST) 

	# TAMPILKAN DATA LOGIN UNTUK DIEDIT
	$KodeEdit= isset($_GET['Kode']) ?  $_GET['Kode'] : $_POST['txtnis']; 
	$sqlShow = "SELECT * FROM siswa WHERE nis='$KodeEdit'";
	$qryShow = mysql_query($sqlShow, $koneksidb)  or die ("Query ambil data user salah : ".mysql_error());
	$dataShow = mysql_fetch_array($qryShow);

	# MASUKKAN DATA KE VARIABEL
	$dataKode	= $dataShow['nis'];
	$datanis	= isset($dataShow['nis']) ?  $dataShow['nis'] : $_POST['txtnis'];
	$datanamasiswa	= isset($dataShow['nama_siswa']) ?  $dataShow['nama_siswa'] : $_POST['txtnamasiswa'];
	$datatempatlahir	= isset($dataShow['tempat_lahir']) ?  $dataShow['tempat_lahir'] : $_POST['txttempatlahir'];
	$datatanggallahir	= isset($dataShow['tanggal_lahir']) ?  $dataShow['tanggal_lahir'] : $_POST['txttanggallahir'];
	$dataalamat	= isset($dataShow['alamat']) ?  $dataShow['alamat'] : $_POST['txtalamat'];
	$datanamaortu	= isset($dataShow['nama_ortu']) ?  $dataShow['nama_ortu'] : $_POST['txtnamaortu'];
	$datahportu	= isset($dataShow['no_hp_ortu']) ?  $dataShow['no_hp_ortu'] : $_POST['txthportu'];
} // End if($_GET) {
?>
<form action="?page=Edit-Siswa&Act=Save" method="post" name="frmadd">
<table class="table-common" style="margin-top:0px; width:70%; border:1px solid grey; padding-left:1em;" >
	<tr>
	  <th colspan="3"><div style="color:blue; font-size:1.5em; text-align:center; ">UBAH DATA SISWA </div></th>
	</tr>
	<tr>
      <td><strong>Nis </strong></td>
	  <td><b>:</b></td>
	  <td><input name="txtnis" value="<?php echo $datanis; ?>"  size="60" maxlength="60"/>
          <input name="txtnislm" type="hidden" value="<?php echo $datanislm; ?>" /></td>
    </tr>
	<tr>
      <td><strong>Nama Siswa </strong></td>
	  <td><b>:</b></td>
	  <td><input name="txtnamasiswa" value="<?php echo $datanamasiswa; ?>"  size="60" maxlength="60"/>
          <input name="txtnamasiswalm" type="hidden" value="<?php echo $datanamasiswalm; ?>" /></td>
    </tr>
	<tr>
      <td><strong>Tempat Lahir </strong></td>
	  <td><b>:</b></td>
	  <td><input name="txttempatlahir" value="<?php echo $datatempatlahir; ?>"  size="60" maxlength="60"/>
          <input name="txttempatlahirlm" type="hidden" value="<?php echo $datatempatlahirlm; ?>" /></td>
    </tr>
	<tr>
      <td><strong>TanggalLahir </strong></td>
	  <td><b>:</b></td>
	  <td><input name="txttanggallahir" value="<?php echo $datatanggallahir; ?>"  size="60" maxlength="60"/>
          <input name="txttanggallahirlm" type="hidden" value="<?php echo $datatanggallahirlm; ?>" /></td>
    </tr>
	<tr>
      <td><strong>Alamat </strong></td>
	  <td><b>:</b></td>
	  <td><input name="txtalamat" value="<?php echo $dataalamat; ?>"  size="60" maxlength="60"/>
          <input name="txtalamatlm" type="hidden" value="<?php echo $dataalamatlm; ?>" /></td>
    </tr>
	<tr>
      <td><strong>Nama Ortu </strong></td>
	  <td><b>:</b></td>
	  <td><input name="txtnamaortu" value="<?php echo $datanamaortu; ?>"  size="60" maxlength="60"/>
          <input name="txtnamaortulm" type="hidden" value="<?php echo $datanamaortulm; ?>" /></td>
    </tr>
	<tr>
      <td><strong>No.Hp. Ortu </strong></td>
	  <td><b>:</b></td>
	  <td><input name="txthportu" value="<?php echo $datahportu; ?>"  size="60" maxlength="60"/>
          <input name="txthportulm" type="hidden" value="<?php echo $datahportulm; ?>" /></td>
    </tr>
	<tr><td>&nbsp;</td>
	  <td>&nbsp;</td>
	  <td><input type="submit" name="btnSave" value=" SIMPAN " style="cursor:pointer;"></td>
    </tr>
</table>
</form>
