<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');
		$("#info_gr").load("modul/inventory/tampil_data_gr.php?");
		$("#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(){
		var cari= $("#txt_caribr").val();
		var kode= $("#txt_kodegr").val();
		$('#form_cari_br').dialog('open');
		$("#info_br").load("modul/inventory/tampil_data_br.php?cari="+cari+"&kode="+kode);
		$("#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);
			}
		});
	});
	
	$('#form_cari_sup').dialog({
		autoOpen: false,
		modal	: true,
		width: 600,
	});
		
	$('#cari_sp').click(function(){
		$('#form_cari_sup').dialog('open');
		$("#info_supplier").load('modul/inventory/tampil_data_sup.php');
		$("#txt_carisup").focus();
		return false;
	});
	
	$("#txt_carisup").keyup(function(){
		var cari = $("#txt_carisup").val();
		
		$.ajax({
			type	: "POST",
			url		: "modul/inventory/tampil_data_sup.php",
			data	: "cari="+cari,
			timeout	: 2000,
			beforeSend	: function(){
				$("#info_supplier").html("<img src='loading.gif' />");
			},
			success	: function(data){
				$("#info_supplier").html(data);
			}
		});
	});
	
	$("#txt_kodesp").keyup(function(){
		var kode 	= $("#txt_kodesp").val();
		
		if(kode.length==4){
		$.ajax({
			type		: "POST",
			url		: "modul/pembelian/cari_sup.php",
			data	: "kode="+kode,
			dataType: "json",
			success	: function(data){
				$("#txt_kodesp").val(kode);
				$("#txt_namasp").val(data.namasup);
				var namasp = $("#txt_namasp").val();
				
				if(namasp.length==0){
					alert('Kode supplier '+kode+' tidak ditemukan.');
					return (false);
				}
				$("#txt_hgjual").focus();
			}
		});
		}
	});
	
	$("#simpan").click(function(){
		simpan_data();
	});
		
	function simpan_data(){
		var	kodegr	= $("#txt_kodegr").val();
		var	kodest	= $("#txt_kodest").val();
		var	namast	= $("#txt_namast").val();
		var	satuan	= $("#txt_satuan").val();
		var	kodesp  = $("#txt_kodesp").val();
		var	namasp  = $("#txt_namasp").val();
		var	hgbeli	= $("#txt_hgbeli").val();
		var	hgjual	= $("#txt_hgjual").val();
		var	minstok = $("#txt_minstok").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(satuan.length == 0){
			var error = true;
			alert("Maaf, Satuan tidak boleh kosong");
			$("#txt_satuan").focus();
			return (false);
        }
		
		if(hgbeli.length == 0){
			var error = true;
			alert("Maaf, Harga beli tidak boleh kosong");
			$("#txt_hgbeli").focus();
			return (false);
        }
		
		if(kodesp.length == 0){
			var error = true;
			alert("Maaf, kode supplier harus diisi.");
			$("#txt_kodesp").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+
					"&satuan="+satuan+
					"&hgbeli="+hgbeli+
					"&kodesp="+kodesp+
					"&namasp="+namasp+
					"&hgjual="+hgjual+
					"&minstok="+minstok,
					
			timeout	: 3000,
			beforeSend	: function(){
				$("#info").html("<img src='loading.gif' />");
			},
			success	: function(data){
				$("#info").html(data);
				$(".input_detail").val('');
				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;
}
</style>

<?php
echo "<table>
	<tr>
		<td>Kode Kelompok</td>
		<td>:</td>
		<td><input name='txt_kodegr' id='txt_kodegr' size='2' maxlength='3' >
		<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</th>
			<th></th>
			<th>Nama Barang</th>
			<th>Satuan</th>
			<th>Harga Beli</th>
			<th>Supplier</th>
			<th></th>
			<th>Nama Supplier</th>
			<th>Harga Jual</th>
			<th></th>
			<th>Stok Minim</th>
		</tr>
		
		<tr>
			<input type='text' name='t_kodest' id='t_kodest' class='input_detail' hidden/>
			<td align='center'><input type='text' name='txt_kodest' id='txt_kodest' class='input_detail' size='6' maxlength='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='24' ></td>
			<td align='center'><input type='text' name='txt_satuan' id='txt_satuan' class='input_detail' size='6' ></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_kodesp' id='txt_kodesp' class='input_detail' size='5' ></td>
			<td><img src='images/view.png' width='18' height='18' title='Cari supplier' id='cari_sp'></td>
			<td align='center'><input type='text' name='txt_namasp' id='txt_namasp' class='input_detail' size='22' readonly /></td>
			<td align='center'><input type='text' name='txt_hgjual' id='txt_hgjual' class='input_detail' size='10' ></td>
			<td></td>
			<td align='center'><input type='text' name='txt_minstok' id='txt_minstok' class='input_detail' size='8' ></td>
			<td>
				<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='info_gr' 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 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='info_br' 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>";

echo "<div id='form_cari_sup' title='Pencarian Data Supplier'>
		<table width='100%'>
			<tr>
				<td width='20%'>Cari Supplier</td>
				<td width='2%'>:</td>
				<td><input type='text' name='txt_carisup' id='txt_carisup' size='40'</td>
			</tr>
		</table>
		<div id='info_supplier' align='center'></div>
	</div>";
?>