<script type="text/javascript">
$(document).ready(function(){
	$("#radio_admin").attr("checked","checked");
	$("#radio_n").attr("checked","checked");
	$("#txt_user").focus();
		
	$("#tambah").click(function(){
		$(".input").val('');
		$(".input").val('');
		$(".radio_level").attr("checked","");
		$("#txt_user").focus();
	});

	$("#txt_user").keyup(function(){
		var user = $("#txt_user").val();
		
		$.ajax({
			type	: "POST",
			url	    : "spv/users/cari.php",
			data	: "user="+user,
			dataType: "json",
			success	: function(data){
				$("#txt_pwd1").val(data.pwd1);
				$("#txt_pwd2").val(data.pwd2);
				$("#txt_nama").val(data.nama);
				if(data.admin=='admin'){
					$("#radio_admin").attr("checked","checked");
				}							
			}
		});
	});
	
	$("#simpan").click(function(){
		var user		= $("#txt_user").val();
		var kodecab	    = $("#txt_kodecab").val();
		var pwd1		= $("#txt_pwd1").val();
		var pwd2		= $("#txt_pwd2").val();
		var nama		= $("#txt_nama").val();
		var status		= $("#txt_status").val();
		var jamawal		= $("#txt_jamawal").val();
		var jamakhir	= $("#txt_jamakhir").val();
		var level		= $(".radio_level:checked").val();
		var radio_level = $(".radio_level:checked");

		if(user.length == 0){
           alert("Maaf, User Name tidak boleh kosong");
		   $("#txt_user").focus();
		   return (false);
        }
		if(pwd1.length == 0){
           alert("Maaf, Password tidak boleh kosong");
		   $("#txt_pwd1").focus();
		   return (false);
        } 
		if(pwd2.length == 0){
           alert("Maaf, Ulangi Password tidak boleh kosong");
		   $("#txt_pwd2").focus();
		   return (false);
        }
		if(pwd1 != pwd2){
           alert("Maaf, Ulangi Password tidak sama");
		   $("#txt_pwd2").focus();
		   return (false);
        }	 
		 
		if(nama.length == 0){
           alert("Maaf, Nama Lengkap tidak boleh kosong");
		   $("#txt_nama").focus();
		   return (false);
        }	 
		if(radio_level.length == 0){
           alert("Maaf, Anda belum memilih Level");
		   return (false);
        }

		if(jamawal.length == 0){
			alert("Maaf, Jam akses awal tidak boleh kosong.");
			$("#txt_jamawal").focus();
			return (false);
        }
		 
		if(jamakhir.length == 0){
			alert("Maaf, Jam akses awal tidak boleh kosong.");
			$("#txt_jamakhir").focus();
			return (false);
        }	 
		 
		$.ajax({
			type	: "POST",
			url	    : "md_dir/users/simpan.php",
			data	: "user="+user+
			        "&kodecab="+kodecab+
					"&pwd1="+pwd1+
					"&nama="+nama+
					"&status="+status+
					"&jamawal="+jamawal+
					"&jamakhir="+jamakhir+
					"&level="+level,
			success	: function(data){
				$("#info").html(data);
				$("#tampil_data").load('spv/users/tampil_data.php');
			}
		});
	});
	
	$("#keluar").click(function(){
		document.location='?module=home';
	});
});
</script>

<?php
include "../../inc/inc.koneksi.php";

echo "<table width='100%'>
	<tr>
		<td width='10%'>User Name</td>
		<td>: <input type='text' name='txt_user' id='txt_user' size='40' lenght='50' class='input'></td>
	</tr>
	<tr>
		<td>Cabang</td>
		<td>: <select id='txt_kodecab' class='input'>
		    <option value=''></option>";
		    $sql=mysql_query("SELECT * FROM cabang WHERE 1");
		    while($r=mysql_fetch_array($sql)){
		        $kodecab=$r[kode_cab];
		        echo "<option value='$kodecab'>$r[kode_cab] - $r[nama_cab]</option>";
		    }
		echo "</select>
		</td>
	</tr>
	<tr>
		<td>Password</td>
		<td>: <input type='password' name='txt_pwd1' id='txt_pwd1'  size='40' lenght='50' class='input'></td>
	</tr>
	<tr>
		<td>Ulangi Password</td>
		<td>: <input type='password' name='txt_pwd2' id='txt_pwd2'  size='40' lenght='50' class='input'></td>
	</tr>	
	<tr>
		<td>Nama Lengkap</td>
		<td>: <input type='text' name='txt_nama' id='txt_nama' size='50' lenght='50' class='input'></td>
	</tr>
	<tr>
		<td>Level</td>
		<td>: <input type='radio' name='txt_level' id='radio_admin' value='Direktur' class='radio_level'>Direktur
			<input type='radio' name='txt_level' id='radio_admin' value='Supervisor' class='radio_level'>Supervisor
			<input type='radio' name='txt_level' id='radio_admin' value='Admin' class='radio_level'>Admin
			<input type='radio' name='txt_level' id='radio_admin' value='Kepala Toko' class='radio_level'>Kepala Toko
			<input type='radio' name='txt_level' id='radio_admin' value='Kasir' class='radio_level'>Kasir
			<input type='radio' name='txt_level' id='radio_admin' value='Gudang' class='radio_level'>Gudang
		</td>
	</tr>
	<tr>
		<td>Status User</td>
		<td>: <select id='txt_status' class='input'>
				<option>ENABLE</option>
				<option>DISABLE</option>
			</select>
		</td>
	</tr>
	<tr>
		<td>Jam Akses</td>
		<td>: <input type='time' id='txt_jamawal' size='8' class='input'> sampai dengan <input type='time' id='txt_jamakhir' size='8' class='input'></td>
	</tr>
	</table>
	<table width='100%'
	<tr>
		<td colspan='3' align='center'>
		<button name='tambah' id='tambah'>Tambah</button>
		<button name='simpan' id='simpan'>Simpan</button>
		<button name='keluar' id='keluar'>Keluar</button>
		</td>
	</tr>
	</table>";
?>