<script type="text/javascript">
$(document).ready(function() {		
    $(function() {
        $("#theList.detail tr:even").addClass("stripe1");
        $("#theList.detail tr:odd").addClass("stripe2");

        $("#theList.detail tr").hover(
            function() {
                $(this).toggleClass("highlight");
            },
            function() {
                $(this).toggleClass("highlight");
            }
        );
    });	
});

	function hapus_data(ID){
		var kode = ID;
		
		$.ajax({
			type	: "POST",
			url	    : "modul/inventory/edit.php",
			data	: "kode="+kode,
			dataType: "json",
			success	: function(data){				
				$("#txt_kodest").val(kode);
				$("#txt_jmlstok").val(data.jmlstok);
				var jmlstok = $("#txt_jmlstok").val();
				
				if(jmlstok=='') var jmlstok=0;
				else var jmlstok=parseInt(jmlstok);
			
				if(!(jmlstok==0)){
					alert('Maaf, jumlah stok masih '+jmlstok+', data barang tidak dapat dihapus.');
					return(false);
				}else hapus_databr();
			}
		});
	}

	function hapus_databr(){		
		var kode = $("#txt_kodest").val();
		
		var pilih = confirm('Hapus data barang kode '+kode+' ?');
		
		if(pilih==true) {
			$.ajax({
				type	: "POST",
				url	    : "spv/inventory/hapus.php",
				data	: "kode="+kode,
				success	: function(data){				
					$("#info").html(data);
					tampil_data_detail();
				}
			});
		}
	}
	
	function tampil_data_detail(){
		var cari= $("#t_carist").val();		
		$("#tampil_data_hd").load('spv/inventory/tampil_data_detail.php?cari='+cari);
	}
</script>

<?php
include '../../inc/inc.koneksi.php';

$cari	= $_GET['cari'];
echo "<table id='theList' class='detail' width='100%'>
		<tr>
			<th>Kode</th>
			<th>Nama Barang</th>
			<th>Barcode</th>
			<th>Satuan</th>
			<th>Harga Beli</th>
			<th>Harga Jual</th>
			<th>Jumlah Stok</th>
			<th>Hapus</th>
		</tr>";
		if (!empty($cari))
			$sql = "SELECT kode_st,nama_st,barcode,satuan,hg_beli,hg_jual,sum(jml_stok+jml_stokb) as jstok FROM stock WHERE (nama_st LIKE '%$cari%' OR kode_st LIKE '%$cari%' OR barcode LIKE '%$cari%') GROUP BY kode_st";
		else
			$sql = "SELECT kode_st,nama_st,barcode,satuan,hg_beli,hg_jual,sum(jml_stok+jml_stokb) as jstok FROM stock WHERE 1 GROUP BY kode_st";
		$query = mysql_query($sql);
		
		while($r=mysql_fetch_array($query)){
			$urut=$urut+1;
			
			echo "<tr>
				<td>$r[kode_st]</td>
				<td>$r[nama_st]</td>
				<td>$r[barcode]</td>
				<td>$r[satuan]</td>
				<td align='right'>".number_format($r['hg_beli'],2)."</td>
				<td align='right'>".number_format($r['hg_jual'],0)."</td>
				<td align='right'>".number_format($r['jstok'],0)."</td>
				<td align='center'>
					<a href='javascript:void(0)' onClick=\"hapus_data('$r[kode_st]')\">
						<img src='icon/thumb_down.png' border='0' id='hapus' title='Hapus' width='12' height='12' >
					</a>
				</td>
			</tr>";
		}
	echo "</table>
	<table width='100%'>
		<tr>
			<td>Jumlah data : $urut</td>
		</tr>
	</table>";
?>