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

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

	function edit_data(ID){	
		var kode = ID;
		$.ajax({
			type	: "POST",
			url		: "modul/customer/cari.php",
			data	: "kode="+kode,
			dataType: "json",
			success	: function(data){
				$("#txt_kode").val(kode);
				$("#txt_nama").val(data.nama);
				$("#txt_alamat").val(data.alamat);
				$("#txt_telepon").val(data.telepon);
				$("#txt_kategori").val(data.kategori);
				$("#txt_gudang").val(data.gudang);
				$("#info").html("Data sudah di tampung pada tab inputan, silahkan klik.");
			}
		});
	}
	
	function hapus_data(ID){
		var kode = ID;
		
		$.ajax({
			type	: "POST",
			url	    : "modul/customer/ceksaldo.php",
			data	: "kode="+kode,
			success	: function(data){				
				$("#txt_saldo").val(data.jsaldo);				
			}
		});
		
		var tsaldo = $("#txt_tsaldo").val();
		if(!tsaldo==0){
			alert('Maaf, pelanggan masih berhutang Rp '+taldo+'. Data tidak dapat dihapus.');
		}
		
		var pilih = confirm('Data yang akan dihapus kode = '+ID+ '?');
		if (pilih==true) {		
			$.ajax({
				type	: "POST",
				url		: "modul/customer/hapus.php",
				data	: "kode="+kode,
				success	: function(data){
					$("#info").html(data);
					$("#tampil_data").load('modul/customer/tampil_data.php');
				}
			});
		}
	}

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

$cari= $_GET['cari'];

echo "<table id='theList' width='100%'>		
		<tr>
			<th>No.</th>
			<th>Kode</th>
			<th>Nama Pelanggan</th>
			<th>Kategori</th>
			<th>Cabang</th>
			<th>Alamat</th>
			<th>Telepon</th>
			<th>Aksi</th>
		</tr>";
		if (!empty($cari))
			$sql= "SELECT * FROM customer WHERE (nama LIKE '%$cari%') ORDER BY kode_pl";
		else
			$sql= "SELECT * FROM customer ORDER BY kode_pl";
		$query = mysql_query($sql);
		
		$no=1;
		while($r=mysql_fetch_array($query)){
			echo "<tr>
				<td align='center'>$no</td>
				<td>$r[kode_pl]</td>
				<td>$r[nama]</td>
				<td>$r[kategori]</td>
				<td>$r[kode_cab]</td>
				<td>$r[alamat]</td>
				<td>$r[telepon]</td>
				<td align='center'>
					<a href='javascript:void(0)' onClick=\"edit_data('$r[kode_pl]')\">
				    	<img src='icon/thumb_up.png' border='0' id='edit' title='Edit' width='12' height='12' >
					</a>
					<a href='javascript:void(0)' onClick=\"hapus_data('$r[kode_pl]')\">
					    <img src='icon/thumb_down.png' border='0' id='hapus' title='Hapus' width='12' height='12' >
					</a>					
				</td>
			</tr>";
			$no++;
		}
	echo "</table>";
?>