<script type="text/javascript">
$(document).ready(function(){
	$("#txt_kodegr").focus();
	
	function cari_nomor(){
		var kode = $("#txt_kodegr").val();
		
		$.ajax({
			type	: "POST",
			url	: "modul/inventory/cari_nomor.php",
			data	: "kode="+kode,
			dataType: "json",
			success	: function(data){
				$("#txt_kodest").val(data.nomor);
				$("#txt_namast").focus();
			}
		});
	}
	
	$('#form_cari_gr').dialog({
		autoOpen: false,
		modal	: true,
		width: 600,
	});
	
	$('#cari_gr').click(function(){		
		$('#form_cari_gr').dialog('open');
		$("#txt_carigr").focus();		
		return false;
	});
	
	$('#txt_carigr').keyup(function(){		
		var cari	= $("#txt_carigr").val();
		
		$.ajax({
			type	: "GET",
			url	: "modul/inventory/tampil_data_gr.php",
			data	: "cari="+cari,			
			timeout	: 2000,
			beforeSend	: function(){		
				$("#info_gr").html("<img src='loading.gif' />");
			},				  
			success	: function(data){
				$("#info_gr").html(data);				
			}
		});		
	});
		
	$("#txt_kodegr").keyup(function(){
		var kode 	= $("#txt_kodegr").val();
		
		if(kode.length==2){
		$.ajax({
			type	: "POST",
			url	: "modul/inventory/cari_grup.php",
			data	: "kode="+kode,
			dataType	: "json",
			success	: function(data){
				$("#txt_kodegr").val(kode);
				$("#txt_namagr").val(data.namagr);
				var nagr = $("#txt_namagr").val();
				
				if(nagr.length==0){
					alert('Kode kelompok '+kode+' tidak ditemukan.');
					return (false);
				}
				tampil_data_detail();
			}
		});
		}
	});
	
	$('#form_cari_br').dialog({
		autoOpen: false,
		modal	: true,
		width: 600,
	});
	
	$('#cari_br').click(function(){		
		$('#form_cari_br').dialog('open');
		$("#txt_caribr").focus();		
		return false;
	});
	
	$('#txt_caribr').keyup(function(){
		var cari= $("#txt_caribr").val();
		var kode= $("#txt_kodegr").val();
		
		$.ajax({
			type	: "GET",
			url	: "modul/inventory/tampil_data_br.php",
			data	: "cari="+cari+
					"&kode="+kode,
			timeout	: 2000,
			beforeSend	: function(){		
				$("#info_br").html("<img src='loading.gif' />");
			},				  
			success	: function(data){
				$("#info_br").html(data);				
			}
		});		
	});
	
	$("#simpan").click(function(){
		simpan_data();
	});
		
	function simpan_data(){
		var	kodegr	= $("#txt_kodegr").val();		
		var	kodest	= $("#txt_kodest").val();
		var	namast	= $("#txt_namast").val();		
		var	tsatuan	= $("#txt_satuan").val();
		var	hgbeli	= $("#txt_hgbeli").val();
		var	hgjual	= $("#txt_hgjual").val();				
		
		var error = false;

		if(kodegr.length == 0){
           var error = true;
           alert("Maaf, Kode grup tidak boleh kosong");
		   $("#txt_kodegr").focus();
		   return (false);
         }
		 
		 if(kodest.length == 0){
           var error = true;
           alert("Maaf, Kode persediaan tidak boleh kosong");
		   $("#txt_kodest").focus();
		   return (false);
         }
		if(namast.length == 0){
           var error = true;
           alert("Maaf, Nama barang tidak boleh kosong");
		   $("#txt_namast").focus();
		   return (false);
         }
		
		if(tsatuan.length == 0){
			var error = true;
			alert("Maaf, Satuan tidak boleh kosong");
			$("#txt_hgjual").focus();
			return (false);
        }
		 
		if(hgbeli.length == 0){
			var error = true;
			alert("Maaf, Harga beli tidak boleh kosong");
			$("#txt_hgbeli").focus();
			return (false);
        }
		
		if(hgjual.length == 0){
			var error = true;
			alert("Maaf, Harga jual tidak boleh kosong");
			$("#txt_hgjual").focus();
			return (false);
        }
		 
		if(error == false){
		$.ajax({
			type	: "POST",
			url	: "modul/inventory/simpan.php",
			data: "kodegr="+kodegr+
					"&kodest="+kodest+
					"&namast="+namast+
					"&tsatuan="+tsatuan+
					"&hgbeli="+hgbeli+
					"&hgjual="+hgjual,
					
			timeout	: 3000,
			beforeSend	: function(){		
				$("#info").html("<img src='loading.gif' />");
			},				  
			success	: function(data){
				$("#info").html(data);
				tampil_data_detail();				
			}
		});
		}
		return false;
	}
	
	function tampil_data_detail(){
		var kode = $("#txt_kodegr").val();
		
		$("#data_detail").show();
		$("#data_detail").load("modul/inventory/tampil_data_detail.php?kode="+kode);
		cari_nomor();
	}
	
	$("#tutup").click(function(){	
		document.location='?module=home';
	});
});
</script>

<style type="text/css">
#txt_kode {
	background:#CCC;
}
#cari_produk{
	cursor:pointer;
}
#cari_buyer{
	cursor:pointer;
}
.input_detail readonly {
	background:#666;
}
</style>

<?php
echo "<table>
	<tr>
		<td>Kode Kelompok</td>
		<td>:</td>
		<td><input name='txt_kodegr' id='txt_kodegr' size='2' maxlength='2' >
		<img src='images/view.png' width='18' height='18' title='Cari grup' id='cari_gr'></td>
		<td width='20%'>Nama Kelompok</td>
		<td>:</td>
		<td><input name='txt_namagr' id='txt_namagr' size='25' readonly />
	</tr>
	</table>
	<hr>";
	
echo "<table>
		<tr>
			<th>Kode Persediaan</th>
			<th>Nama Barang</th>
			<th>Satuan</th>
			<th>Harga Beli</th>
			<th>Harga Jual</th>
		</tr>
		
		<tr>						
			<td align='center'><input type='text' name='txt_kodest' id='txt_kodest' class='input_detail' size='6' maxlength='6' class='input_detail' readonly />
				<img src='images/view.png' width='18' height='18' title='Cari data barang' id='cari_br'></td>
			<td align='center'><input type='text' name='txt_namast' id='txt_namast' class='input_detail' size='40' ></td>
			<td align='center'><input type='text' name='txt_satuan' id='txt_satuan' class='input_detail' size='7' ></td>
			<td align='center'><input type='text' name='txt_hgbeli' id='txt_hgbeli' class='input_detail' size='8' ></td>
			<td align='center'><input type='text' name='txt_hgjual' id='txt_hgjual' class='input_detail' size='8' ></td>			
		</tr>
	</table>
	
	<table width='100%'>
	<tr>
		<td colspan='3' align='center'>		
		<button name='simpan' id='simpan'>Rekam</button>
		<button name='tutup' id='tutup'>Tutup</button>
		</td>
	</tr>
	</table>";
	
echo "<div id='form_cari_gr' title='Pencarian grup'>
		<table width='100%'>
			<td>
				<td width='20%'>Cari Kelompok</td>
				<td width='2%'>:</td>
				<td><input type='text' name='txt_carigr' id='txt_carigr' size='40'</td>
			</td>
		</table>
		<div id='tampil_data_gr' align='center'></div>
		<div id='info_gr' align='center'></div>
	</div>";

echo "<div id='form_cari_br' title='Pencarian Data Barang'>
		<table width='100%'>
			<td>
				<td width='20%'>Cari Barang</td>
				<td width='2%'>:</td>
				<td><input type='text' name='txt_caribr' id='txt_caribr' size='40'</td>
			</td>
		</table>
		<div id='tampil_data_br' align='center'></div>
		<div id='info_br' align='center'></div>
	</div>";
	
echo "<div id='data_detail'></div>";
?>
