<script type="text/javascript">
	$(document).ready(function(){
	$("#input_bon").hide();
	
	cari_itf();
	
	function cari_itf(){
		var no = 1;
		$.ajax({
			type	: "POST",
			url		: "modul/setor_bbm/cari_itf.php",
			data	: "no="+no,
			dataType: "json",
			success	: function(data){
				$("#txt_prem").val(data.prem);
				$("#txt_solar").val(data.solar);
				$("#txt_ptmx").val(data.ptmx);
				$("#txt_ptmp").val(data.ptmp);
				$("#txt_ptlt").val(data.ptlt);
				$("#txt_dex").val(data.dex);
				$("#txt_dexlite").val(data.dexlite);
				$("#txt_solek").val(data.solek);
			}
		});
	}
	
	cari_tgl();
	
	function cari_tgl(){
		var no = 1;
		$.ajax({
			type	: "POST",
			url		: "modul/report/cari_tanggal.php",
			data	: "no="+no,
			dataType: "json",
			success	: function(data){				
				$("#txt_tanggal").val(data.tanggal);				
			}
		});
	}
	
	$("#txt_tanggal").focus();
	
	$('#txt_jenis').keyup(function(){
		cari_nomor();
		cari_stok();
		tampil_data_detail();
	});
	
	$('#txt_jenis').click(function(){
		cari_nomor();
		cari_stok();
		tampil_data_detail();
	});
	
	function cari_nomor(){
		var tanggal	= $("#txt_tanggal").val();
		
		$.ajax({
			type	: "POST",
			url		: "spv/ubah_harga/cari_nomor.php",
			data	: "tanggal="+tanggal,
			dataType: "json",
			success	: function(data){
				$("#txt_kode").val(data.nomor);
			}
		});
	}
	
	function cari_stok(){
		var jenis	= $("#txt_jenis").val();
		var prem	= $("#txt_prem").val();
		var solar	= $("#txt_solar").val();
		var ptmx	= $("#txt_ptmx").val();
		var ptmp	= $("#txt_ptmp").val();
		var ptlt 	= $("#txt_ptlt").val();
		var dex		= $("#txt_dex").val();
		var dexlite	= $("#txt_dexlite").val();
		var solek	= $("#txt_solek").val();
			
		$.ajax({
			type	: "POST",
			url		: "spv/ubah_harga/cari_stok.php",
			data	: "jenis="+jenis+
				"&prem="+prem+
				"&solar="+solar+
				"&ptmx="+ptmx+
				"&ptmp="+ptmp+
				"&ptlt="+ptlt+
				"&dex="+dex+
				"&dexlite="+dexlite+
				"&solek="+solek,
			dataType: "json",
			success	: function(data){
				$("#txt_hblama").val(data.hbeli);
				$("#txt_hjlama").val(data.hjual);
				$("#txt_jmlstok").val(data.jstok);
			}
		});
	}
	
	function tampil_data_detail(){
		var tanggal = $("#txt_tanggal").val();
		$("#data_detail").load('spv/ubah_harga/tampil_data_detail.php?tanggal='+tanggal);
	}
	
	$('#txt_hbbaru').keyup(function(){
		hitung_sls();
	});
	
	function hitung_sls(){
		var jmlstok = $("#txt_jmlstok").val();
		var hblama  = $("#txt_hblama").val();
		var hbbaru  = $("#txt_hbbaru").val();
		var selisih	= (parseFloat(hbbaru)*parseFloat(jmlstok))-(parseFloat(hblama)*parseFloat(jmlstok));
		$("#txt_selisih").val(selisih);
	}
			
	$('#simpan').click(function(){
		simpan_data();
	});
	
	function simpan_data(){
		var kode	= $("#txt_kode").val();
		var tanggal = $("#txt_tanggal").val();		
		var jenis  	= $("#txt_jenis").val();		
		var jmlstok	= $("#txt_jmlstok").val();		
		var hblama 	= $("#txt_hblama").val();
		var hbbaru 	= $("#txt_hbbaru").val();
		var hjlama 	= $("#txt_hjlama").val();
		var hjbaru 	= $("#txt_hjbaru").val();
		var selisih = $("#txt_selisih").val();
		
		if(kode.length == 0){
           alert("Maaf, Form input belum diisi secara lengkap.");
		   $("#txt_tanggal").focus();
		   return (false);
        }
		if(jenis.length == 0){
           alert("Maaf, Jenis bbm belum dipilih.");
		   $("#txt_jenis").focus();
		   return (false);
        }
		if(hjbaru.length == 0){
           alert("Maaf, Harga jual sekarang masih kosong.");
		   $("#txt_hjbaru").focus();
		   return (false);
        }
		if(hbbaru.length == 0){
           alert("Maaf, Harga beli sekarang masih kosong.");
		   $("#txt_hbbaru").focus();
		   return (false);
        }

		$.ajax({
			type	: "POST",
			url		: "spv/ubah_harga/simpan.php",
			data	: "kode="+kode+
					"&tanggal="+tanggal+					
					"&jenis="+jenis+
					"&jmlstok="+jmlstok+
					"&hblama="+hblama+
					"&hbbaru="+hbbaru+
					"&hjlama="+hjlama+
					"&hjbaru="+hjbaru+
					"&selisih="+selisih,
			timeout	: 2000,
			beforeSend	: function(){		
				$("#info").html("<img src='loading.gif' />");			
			},				  
			success	: function(data){
				$("#info").html(data);
				$(".input").val('');
				$("#txt_tanggal").focus();
				tampil_data_detail();
			}
		});
	}

	$("#tutup").click(function(){
		document.location='?module=home';
	});
});
</script>

<style type="text/css">
#txt_kode {
	background:#CCC;
}
</style>

<?php

echo "<table width='100%'>	
	<tr>
		<input type='text' name='txt_prem' id='txt_prem' hidden />
		<input type='text' name='txt_solar' id='txt_solar' hidden />
		<input type='text' name='txt_ptmx' id='txt_ptmx' hidden />
		<input type='text' name='txt_ptmp' id='txt_ptmp' hidden />
		<input type='text' name='txt_ptlt' id='txt_ptlt' hidden />
		<input type='text' name='txt_dex' id='txt_dex' hidden />
		<input type='text' name='txt_dexlite' id='txt_dexlite' hidden />
		<input type='text' name='txt_solek' id='txt_solek' hidden />
		
		<td>Kode Tran</td>
		<td>:</td>
		<td><input type='text' name='txt_kode' id='txt_kode' size='14' class='input' readonly /></td>
		<td>Tanggal</td>
		<td>:</td>
		<td><input type='date' name='txt_tanggal' id='txt_tanggal'></td>		
		<td>Jenis BBM</td>
		<td>:</td>
		<td><select name='txt_jenis' id='txt_jenis' >
			<option value='' selected>-Pilih-</option>
			<option value='Premium'>Premium</option>
			<option value='Bio Solar'>Bio Solar</option>
			<option value='Pertamax'>Pertamax</option>
			<option value='Pertamax Turbo'>Pertamax Turbo</option>
			<option value='Pertalite'>Pertalite</option>
			<option value='Pertamina Dex'>Pertamina Dex</option>
			<option value='Dexlite'>Dexlite</option>
			<option value='Solar Keekonomian'>Solar Keekonomian</option>
			</select>
		</td>
	</tr>
	
	<tr>
		<td>Total Stok</td>
		<td>:</td>
		<td><input type='text' name='txt_jmlstok' id='txt_jmlstok' size='7' class='input' readonly /> lt.</td>
		<td>HJ Sebelumnya</td>
		<td>:</td>
		<td><input type='text' name='txt_hjlama' id='txt_hjlama' size='7' class='input' readonly /</td>
		<td>HJ Sekarang </td>
		<td>:</td>
		<td><input type='text' name='txt_hjbaru' id='txt_hjbaru' size='7' class='input'></td>
	</tr>
	
	<tr>
		<td>H.Beli Sbl</td>
		<td>:</td>
		<td><input type='text' name='txt_hblama' id='txt_hblama' size='7' class='input' readonly /></td>
		<td>HB Sekarang</td>
		<td>:</td>
		<td><input type='text' name='txt_hbbaru' id='txt_hbbaru' size='7' class='input'></td>
		<td>Total Selisih</td>
		<td>:</td>
		<td><input type='text' name='txt_selisih' id='txt_selisih' size='9' class='input' readonly/></td>
	</tr>
	</table>
	<table align='center'>
	<tr>
		<td>
			<button name='simpan' id='simpan'>Rekam</button>	
			<button name='tutup' id='tutup'>Tutup</button>
		</td>
	</tr>
	</table>";

echo "<div id='data_detail' align='center'><div>";
?>
