<script type="text/javascript">
$(document).ready(function(){
	
	$("#t_kode").focus();
	
	$("#t_kode").keyup(function(){
		var kode = $("#t_kode").val();
		
		if(kode.length>3){
			$.ajax({
				type	: "POST",
				url		: "spv/customer/cari.php",
				data	: "kode="+kode,
				dataType: "json",
				success	: function(data){
					$("#t_nama").val(data.nama);
					$("#t_alamat").val(data.alamat);
					$("#t_umurpiut").val(data.umurpiut);
					var nama = $("#t_nama").val();
				
					if(nama.length==0){
						alert('Kode pelanggan '+kode+' tidak ditemukan.');
						return (false);
					}else $("#t_umurpiut").focus();
				}
			});
		}
	});
	
	$('#form_cari_plg').dialog({
		autoOpen: false,
		modal	: true,
		width: 800,
	});
		
	$('#cari_plg').click(function(){
		var cari= $("#txt_cariplg").val();
		$('#form_cari_plg').dialog('open');
		$("#info_pelanggan").load('spv/customer/tampil_data.php?cari='+cari);
		$("#txt_cariplg").focus();
	});
	
	$("#txt_cariplg").keyup(function(){
		var cari	= $("#txt_cariplg").val();
		
		$.ajax({
			type	: "GET",
			url		: "spv/customer/tampil_data.php",
			data	: "cari="+cari,
			timeout	: 3000,
			beforeSend	: function(){
				$("#info_pelanggan").html("<img src='loading.gif' />");			
			},				  
			success	: function(data){
				$("#info_pelanggan").html(data);
			}
		});
	});
	
	$("#simpan_up").click(function(){
		simpan_data();
	});

	function simpan_data(){
		var	kode	= $("#t_kode").val();
		var	umurpiut= $("#t_umurpiut").val();
		
		var error = false;
 
		if(error == false){
			$.ajax({
				type	: "POST",
				url		: "spv/customer/simpan.php",
				data	: "kode="+kode+
						"&umurpiut="+umurpiut,
				timeout	: 2000,
				beforeSend	: function(){		
					$("#info").html("<img src='loading.gif' />");			
				},				  
				success	: function(data){
					$("#info").html(data);
					$(".input").val('');
				}
			});
		}
	}
	
	$("#keluar_up").click(function(){
		document.location='?module=home';
	});
});
</script>

<?php
echo "<table>
	<tr>
		<td width='20%'>Kode Pelanggan</td>
		<td>: <input type='text' name='t_kode' id='t_kode' size='4' lenght='4' value='$kode' class='input'>
			<img src='images/view.png' width='18' height='18' title='Cari data pelanggan' id='cari_plg'>
		</td>
	</tr>
	<tr>
		<td>Nama Pelanggan</td>
		<td>: <input type='text' name='t_nama' id='t_nama'  size='30' lenght='30' class='input' readonly/></td>
	</tr>
	<tr>
		<td>Alamat</td>
		<td>: <input type='text' name='t_alamat' id='t_alamat' size='60' lenght='60' class='input' readonly/></td>
	</tr>
	<tr>
		<td>Batas Umur Piutang</td>
		<td>: <input type='text' name='t_umurpiut' id='t_umurpiut' size='3' lenght='3' class='input'> hari</td>
	</tr>
	</table>
	
	<table width='100%'>
	<tr>
		<td colspan='3' align='center'>		
		<button name='simpan_up' id='simpan_up'>Simpan</button>
		<button name='keluar_up' id='keluar_up'>Keluar</button>
		</td>
	</tr>
	</table>";

echo "<div id='form_cari_plg' title='Pencarian Data Pelanggan'>
		<table width='100%'>
			<tr>
				<td>Cari Pelanggan : <input type='text' name='txt_cariplg' id='txt_cariplg' size='40'</td>
			</tr>
		</table>
		<div id='info_pelanggan' align='center' style='overflow-x: auto;height: 400px;scroll-behavior: smooth'></div>
	</div>";

?>