<script type="text/javascript">
$(document).ready(function(){
	$(':input:not([type="submit"])').each(function() {
		$(this).focus(function() {
			$(this).addClass('hilite');
		}).blur(function() {
			$(this).removeClass('hilite');});
	});	
	
	tampil_data();
	
	function tampil_data(){
		var kode = $("#kode_beli").val();
		//alert(kode);
		$.ajax({
			type	: 'POST',
			url		: "<?php echo site_url(); ?>/pembelian/DataDetail",
			data	: "kode="+kode,
			cache	: false,
			success	: function(data){
				$("#tampil_data").html(data);
			}
		});
		//return false();
	}
	
	$("#tgl").datepicker({
			dateFormat:"dd-mm-yy"
    });
	
	$("#kode_brg").focus();
	$("#kode_brg").keyup(function(e){
		var isi = $(e.target).val();
		$(e.target).val(isi.toUpperCase());
	});
	$("#kode_brg").focus(function(e){
		var isi = $(e.target).val();
		CariBarang();
	});
	
	$("#kode_brg").keyup(function(){
		CariBarang();
		
	});
	
	function CariBarang(){
		var kode = $("#kode_brg").val();
		$.ajax({
			type	: 'POST',
			url		: "<?php echo site_url(); ?>/ref_json/InfoBarang",
			data	: "kode="+kode,
			cache	: false,
			dataType : "json",
			success	: function(data){
				$("#nama_brg").val(data.nama_barang);
				$("#satuan").val(data.satuan);
				$("#bandul").val(data.bandul);
				$("#laba").val(data.persen_laba);
			}
		});
	};	
	
	$("#simpan").click(function(){
		var kode	= $("#kode_brg").val();
		var jml		= $("#jml").val();
		var string 	= $("#form").serialize();
		
		if(kode.length==0){
			$.messager.show({
				title:'Info',
				msg:'Maaf, Kode Barang tidak boleh kosong', 
				timeout:2000,
				showType:'show'
			});
			$("#kode_brg").focus();
			return false();
		}
		if(jml<0){
			$.messager.show({
				title:'Info',
				msg:'Maaf, Jml Stok tidak boleh negatif', 
				timeout:2000,
				showType:'show'
			});
			$("#jml").focus();
			return false();
		}
		$.ajax({
			type	: 'POST',
			url		: "<?php echo site_url(); ?>/stok_opname/simpan",
			data	: string,
			cache	: false,
			success	: function(data){
				$.messager.show({
					title:'Info',
					msg:data, 
					timeout:2000,
					showType:'slide'
				});
			},
			error : function(xhr, teksStatus, kesalahan) {
				$.messager.show({
					title:'Info',
					msg: 'Server tidak merespon :'+kesalahan,
					timeout:2000,
					showType:'slide'
				});
			}
		});
		return false();		
	});
	
	$("#tambah_data").click(function(){
		$(".detail").val('');
		$("#kode_brg").val('');
		$("#kode_brg").focus();
	});
	
	$("#cetak").click(function(){
		var kode	= $("#kode_beli").val();
		window.open('<?php echo site_url();?>/pembelian/cetak/'+kode);
		return false();
	});
	
	$("#cari_barang").click(function(){
		//AmbilDaftarBarang();
		$("#dlg").dialog('open');
	});
	
	$("#cari_barang2").click(function(){
		AmbilDaftarBarang();
		//$("#dlg").dialog('open');
	});
	
	function AmbilDaftarBarang(){
		var cari = $("#text_cari").val();
		
		$.ajax({
			type	: 'POST',
			url		: "<?php echo site_url(); ?>/ref_json/DataBarang",
			data	: "cari="+cari,
			cache	: false,
			success	: function(data){
				$("#daftar_barang").html(data);
			}
		});
	}
});	
</script>
<form name="form" id="form">
<table width="100%">
<tr>
<td valign="top" width="50%">
    <fieldset class="atas">
    <table width="100%">
        <tr>    
        <td> Tgl Stok Opname</td>
        <td>:</td>
        <td><input name="tgl" type="text" id="tgl" value="<?php echo $tgl; ?>"  size="12" maxlength="12"/></td>
    </tr>
    <tr>    
        <td width="150">Kode Barang</td>
        <td width="5">:</td>
        <td><input name="kode_brg" type="text" class="easyui-validatebox" id="kode_brg" value="<?php echo $kode_brg; ?>" size="12" maxlength="12" data-options="required:true,validType:'length[3,10]'" />
        <button type="button" name="cari_barang" id="cari_barang" class="easyui-linkbutton" data-options="iconCls:'icon-search'">Cari</button>
        </td>
    </tr>
    <tr>    
        <td>Nama Barang</td>
        <td>:</td>
        <td><input type="text" name="nama_brg" id="nama_brg"  size="50" class="detail" maxlength="50" readonly/></td>
    </tr>
    <tr>    
        <td>Satuan</td>
        <td>:</td>
        <td><input type="text" name="satuan" id="satuan"  size="20" class="detail" maxlength="20" readonly/></td>
    </tr>
    <tr>    
        <td> Stok Sebelumnya</td>
        <td>:</td>
        <td><input name="jml_sblm" type="text"class="detail" id="jml_sblm" value="<?php echo $stok_sblm; ?>"  size="20" maxlength="20"/></td>
    </tr>
    <tr>    
        <td> Stok Sekarang</td>
        <td>:</td>
        <td><input name="jml" type="text"class="detail" id="jml" value="<?php echo $stok; ?>"  size="20" maxlength="20"/></td>
    </tr>
    </table>
    </fieldset>
</td>
</tr>
</table>    
<fieldset class="bawah">
<table width="100%">
<tr>
	<td colspan="3" align="center">
    <button type="button" name="simpan" id="simpan" class="easyui-linkbutton" data-options="iconCls:'icon-save'">SIMPAN</button>
    <button type="button" name="tambah_data" id="tambah_data" class="easyui-linkbutton" data-options="iconCls:'icon-add'">TAMBAH</button>
    <a href="<?php echo base_url();?>index.php/stok_opname/">
    <button type="button" name="kembali" id="kembali" class="easyui-linkbutton" data-options="iconCls:'icon-logout'">TUTUP</button>
    </a>
    </td>
</tr>
</table>  
</fieldset>   
</form>
<div id="dlg" class="easyui-dialog" title="Daftar Barang" style="width:900px;height:400px; padding:5px;" data-options="closed:true">
	Cari : <input type="text" name="text_cari" id="text_cari" size="50" /> <button type="button" name="cari_barang2" id="cari_barang2" class="easyui-linkbutton" data-options="iconCls:'icon-search'">Cari</button>
	<div id="daftar_barang"></div>
</div>