<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_grup').dialog({
		autoOpen: false,
		modal	: true,
		width: 600,
	});
	
	$('#cari_grup').click(function(){
		$('#form_cari_grup').dialog('open');
		$("#info_grup").load("modul/inventory/tampil_data_gr.php?");
		$("#txt_carigrup").focus();
	});
	
	$('#txt_carigrup').keyup(function(){		
		var cari	= $("#txt_carigrup").val();
		
		$.ajax({
			type	: "GET",
			url		: "modul/inventory/tampil_data_gr.php",
			data	: "cari="+cari,			
			success	: function(data){
				$("#info_grup").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();
			}
		});
		}
	});
	
	$("#txt_barcode").keyup(function(){
		var kode 	= $("#txt_barcode").val();
				
		$.ajax({
			type	: "POST",
			url		: "modul/inventory/cari_barcode.php",
			data	: "kode="+kode,
			dataType: "json",
			success	: function(data){				
				$("#t_barcode").val(data.barcode);
				$("#t_kodest").val(data.kodest);
				var barc   = $("#t_barcode").val();
				var kodest = $("#t_kodest").val();
				var kodesta= $("#txt_kodest").val();
				
				if(!(barc.length==0)){
					if(kodest==kodesta){
						alert('Maaf, Barcode '+kode+' sudah ada.');
						$("#txt_barcode").val('');
						$("#txt_barcode").focus();
						return (false);
					}
				}
			}
		});		
	});
	
	$('#form_cari_br').dialog({
		autoOpen: false,
		modal	: true,
		width: 950,
	});
	
	$('#cari_br').click(function(){
		var cari	= $("#txt_caribr").val();
		
		$('#form_cari_br').dialog('open');
		$("#info_br").load("modul/inventory/tampil_data_br.php?cari="+cari);
		$("#txt_caribr").focus();
	});
	
	$('#txt_caribr').keyup(function(){
		var cari	= $("#txt_caribr").val();
		
		if(cari.length>3){
		$.ajax({
			type	: "GET",
			url		: "modul/inventory/tampil_data_br.php",
			data	: "cari="+cari,
			success	: function(data){
				$("#info_br").html(data);
			}
		});
		}
	});
	
	$('#form_cari_sup').dialog({
		autoOpen: false,
		modal	: true,
		width: 700,
	});
		
	$('#txt_namasp').keyup(function(){
		$('#form_cari_sup').dialog('open');
		$("#info_supplier").load('modul/inventory/tampil_data_sup.php');
		$("#txt_carisup").focus();
	});
	
	$("#txt_carisup").keyup(function(){
		var cari = $("#txt_carisup").val();
		
		$.ajax({
			type	: "GET",
			url	    : "modul/inventory/tampil_data_sup.php",
			data	: "cari="+cari,
			success	: function(data){
				$("#info_supplier").html(data);
			}
		});
	});
	
	$('#form_cari_sup1').dialog({
		autoOpen: false,
		modal	: true,
		width: 700,
	});
	
	$('#txt_namasp1').keyup(function(){
		$('#form_cari_sup1').dialog('open');
		$("#info_supplier1").load('modul/inventory/tampil_data_sup1.php');
		$("#txt_carisup1").focus();
	});
	
	$("#txt_carisup1").keyup(function(){
		var cari = $("#txt_carisup1").val();
		
		$.ajax({
			type	: "GET",
			url	    : "modul/inventory/tampil_data_sup1.php",
			data	: "cari="+cari,
			success	: function(data){
				$("#info_supplier1").html(data);
			}
		});
	});
	
	$("#simpan").click(function(){		
		var kodegr	= $("#txt_kodegr").val();		
		var kodest	= $("#txt_kodest").val();
		var namast	= $("#txt_namast").val();
		var barcode = $("#txt_barcode").val();
		var satuan	= $("#txt_satuan").val();
		var hgbeli	= $("#txt_hgbeli").val();
		var hgjual	= $("#txt_hgjual").val();
		var hgresep = $("#txt_hgresep").val();
		var hgparam = $("#txt_hgparam").val();
		var kodesp  = $("#txt_kodesp").val();
		var namasp  = $("#txt_namasp").val();
		var kodesp1 = $("#txt_kodesp1").val();
		var namasp1 = $("#txt_namasp1").val();
		var minstok = $("#txt_minstok").val();
				
		if(kodegr.length == 0){
           alert("Maaf, Kode grup tidak boleh kosong");
		   $("#txt_kodegr").focus();
		   return (false);
        }		 
		if(kodest.length == 0){
           alert("Maaf, Kode persediaan tidak boleh kosong");
		   $("#txt_kodest").focus();
		   return (false);
        }		 
		if(namast.length == 0){
           alert("Maaf, Nama barang tidak boleh kosong");
		   $("#txt_namast").focus();
		   return (false);
        }		
		if(satuan.length == 0){
			alert("Maaf, Satuan tidak boleh kosong");
			$("#txt_satuan").focus();
			return (false);
        }		
		if(hgbeli.length == 0){
			alert("Maaf, Harga beli tidak boleh kosong");
			$("#txt_hgbeli").focus();
			return (false);
        }		
		if(hgjual.length == 0){
			alert("Maaf, Harga jual umum tidak boleh kosong.");
			$("#txt_hgjual").focus();
			return (false);
        }
		if(hgresep.length == 0){
			alert("Maaf, Harga jual resep tidak boleh kosong.");
			$("#txt_hgresep").focus();
			return (false);
        }
		if(hgparam.length == 0){
			alert("Maaf, Harga jual paramedis tidak boleh kosong.");
			$("#txt_hgparam").focus();
			return (false);
        }
		
		$.ajax({
			type	: "POST",
			url		: "modul/inventory/simpan.php",
			data	: "kodegr="+kodegr+
					"&kodest="+kodest+
					"&namast="+namast+
					"&barcode="+barcode+
					"&minstok="+minstok+
					"&satuan="+satuan+
					"&kodesp="+kodesp+
					"&namasp="+namasp+
					"&kodesp1="+kodesp1+
					"&namasp1="+namasp1+
					"&hgbeli="+hgbeli+
					"&hgjual="+hgjual+
					"&hgresep="+hgresep+
					"&hgparam="+hgparam,
			timeout	: 2000,
			beforeSend	: function(){		
				$("#info").html("<img src='loading.gif' />");
			},				  
			success	: function(data){
				$("#info").html(data);
				$(".input_detail").val('');
				cari_nomor();
				tampil_data_detail();				
			}
		});
	});
	
	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;
}
</style>

<?php
echo "<table width='100%'>
	<tr>
		<td width='15%'>Kode Kelompok : <input type='text' id='txt_kodegr' size='2' maxlength='3'> <img src='images/view.png' width='18' height='18' title='Cari grup' id='cari_grup'></td>
		<td>Nama Kelompok : <input type='text' id='txt_namagr' size='25' readonly/></td>
	</tr>
	</table>
	<hr>";
	
echo "<table>
		<tr>
			<th>Kode</th>
			<th></th>
			<th>Nama Barang</th>
			<th>Barcode</th>
			<th>Satuan</th>
			<th>Harga Beli</th>
			<th>HJ. Umum</th>
			<th>HJ. Resep</th>
			<th>HJ. Grosir</th>
		</tr>
		<tr>
			<input type='text' id='t_kodest' class='input_detail' hidden/>
			<input type='text' id='t_namast' class='input_detail' hidden/>
			<input type='text' id='t_barcode' class='input_detail' hidden/>
			<td align='center'><input type='text' name='txt_kodest' id='txt_kodest' class='input_detail' size='6' class='input_detail' readonly/></td>
			<td><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='30' ></td>
			<td align='center'><input type='text' name='txt_barcode' id='txt_barcode' class='input_detail' size='14' ></td>
			<td align='center'><input type='text' name='txt_satuan' id='txt_satuan' class='input_detail' size='10'></td>						
			<td align='center'><input type='text' name='txt_hgbeli' id='txt_hgbeli' class='input_detail' size='10' /></td>
			<td align='center'><input type='text' name='txt_hgjual' id='txt_hgjual' class='input_detail' size='10' ></td>
			<td align='center'><input type='text' name='txt_hgresep' id='txt_hgresep' class='input_detail' size='10' ></td>
			<td align='center'><input type='text' name='txt_hgresep' id='txt_hgparam' class='input_detail' size='10' ></td>			
		</tr>
		<tr>
			<th>Min. Stok</th>
			<th></th>
			<th>Supplier 1</th>
			<th>Kode Sup.1</th>
			<th>Supplier 2</th>
			<th>Kode Sup.2</th>
			<th></th>
			<th></th>
		</tr>
		<tr>
			<td align='center'><input type='text' id='txt_minstok' class='input_detail' size='6'></td>
			<td></td>
			<td align='center'><input type='text' id='txt_namasp' class='input_detail' size='20'></td>
			<td align='center'><input type='text' id='txt_kodesp' class='input_detail' size='3' readonly /></td>
			<td align='center'><input type='text' id='txt_namasp1' class='input_detail' size='20'></td>
			<td align='center'><input type='text' id='txt_kodesp1' class='input_detail' size='3' readonly /></td>
			<td align='center'><button id='simpan'>Rekam</button></td>
			<td align='center'><button id='tutup'>Tutup</button></td>
		</tr>
	</table>";
	
echo "<div id='form_cari_grup' title='Pencarian grup'>
		<table width='100%'>
			<tr>
				<td>Cari Kelompok : <input type='text' id='txt_carigrup' size='40'></td>
			</tr>
		</table>
		<div id='tampil_data_grup' align='center'></div>
		<div id='info_grup' align='center' style='overflow-x: auto;height: 400px;scroll-behavior: smooth'></div>
	</div>";

echo "<div id='form_cari_br' title='Pencarian Data Barang'>
		<table width='100%'>
			<td>
				<td>Cari Barang : <input type='text' id='txt_caribr' size='40'></td>
			</td>
		</table>
		<div id='info_br' align='center' style='overflow-x: auto;height: 400px;scroll-behavior: smooth'></div>
	</div>";
	
echo "<div id='form_cari_sup' title='Pencarian Data Supplier'>
		<table>
			<tr>
				<td>Cari Supplier : <input type='text' id='txt_carisup' size='40'></td>
			</tr>
		</table>
		<div id='info_supplier' align='center' style='overflow-x: auto;height: 400px;scroll-behavior: smooth'></div>
	</div>";

echo "<div id='form_cari_sup1' title='Pencarian Data Supplier'>
		<table>
			<tr>
				<td>Cari Supplier : <input type='text' id='txt_carisup1' size='40'></td>
			</tr>
		</table>
		<div id='info_supplier1' align='center' style='overflow-x: auto;height: 400px;scroll-behavior: smooth'></div>
	</div>";
	
echo "<div id='data_detail' style='overflow-x: auto;height: 400px;scroll-behavior: smooth'></div>";
?>
