<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		: "md_prod/supplier/cari.php",
			data	: "kode="+kode,
			dataType	: "json",
			success	: function(data){
				$("#txt_kode").val(kode);
				$("#txt_nama").val(data.tnama);
				$("#txt_alamat").val(data.talamat);
				$("#txt_telepon").val(data.ttelepon);
				$("#txt_fax").val(data.tfax);
				$("#txt_jaminan").val(data.tjaminan);
				$("#txt_bank").val(data.tbank);
				$("#txt_nomorac").val(data.nomorac);
				$("#txt_namaac").val(data.namaac);
				$("#txt_kontak").val(data.tkontak);
				$("#akses").val(data.akses);
				$("#info").html("Data sudah di tampung pada tab inputan, silahkan klik");
			}
		});
	}
	
	function hapus_data(ID){
		var kode = ID;
		
		$.ajax({
			type	: "POST",
			url	: "md_prod/supplier/ceksaldo.php",
			data	: "kode="+kode,
			success	: function(data){				
				$("#txt_utdg").val(data.jutdg);
				$("#txt_utbg").val(data.jutbg);				
			}
		});
		
		var t_saldo = $("#txt_saldo").val();
		if (!t_saldo==0) {
			alert('Maaf pelanggan masih berhutang Rp '+$tsaldo+', data tidak dapat dihapus.');
			return false;
		}
		
		var pilih = confirm('Data yang akan dihapus kode = '+ID+ '?');
		if (pilih==true) {
		var kode = ID;
		$.ajax({
			type	: "POST",
			url	: "md_prod/supplier/hapus.php",
			data	: "kode="+kode,
			success	: function(data){
				$("#info").html(data);
				$("#tampil_data").load('modul/supplier/tampil_data.php');
				$("#akses").val('');
			}
		});
		}
	}

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

$cari	= $_GET['cari'];
$hal	= $_GET['hal'] ? $_GET['hal'] : 0;
$lim	= 15;
	if (!empty($cari)){
		$query2	= "SELECT * FROM supplier WHERE (nama LIKE '%$cari%' OR alamat LIKE '%$cari%' OR telepon LIKE '%$cari%')  ";
	}
	else{
		$query2	= "SELECT * FROM supplier";
	}
	$data2	= mysql_query($query2);
	$jml	= mysql_num_rows($data2);
	
	$max	= ceil($jml/$lim);

echo "<table id='theList' width='100%'>
		<tr>
			<th width='2%'>No.</th>
			<th>Kode</th>
			<th>Nama Supplier</th>
			<th>Alamat</th>
			<th>Telepon</th>
			<th>Faxcimile</th>
			<th>Jaminan Rp</th>
			<th>Bank</th>
			<th>Nomor Rekening</th>
			<th>Atas Nama</th>
			<th>Contact Person</th>
			<th>Aksi</th>
		</tr>";
	if (!empty($cari)){
		$sql	= "SELECT * FROM supplier WHERE (nama LIKE '%$cari%' OR alamat LIKE '%$cari%' OR telepon LIKE '%$cari%' ) 
				    ORDER BY kode_sp LIMIT $hal,$lim";		
	}

	else{
		$sql	= "SELECT * FROM supplier ORDER BY kode_sp LIMIT $hal,$lim";
	}
						
		$query = mysql_query($sql);
		
		$no=1+$hal;
		while($r_data=mysql_fetch_array($query)){
			echo "<tr>
					<td align='center'>$no</td>
					<td>$r_data[kode_sp]</td>
					<td>$r_data[nama]</td>
					<td>$r_data[alamat]</td>
					<td>$r_data[telepon]</td>
					<td>$r_data[fax]</td>					
					<td align='right'>".number_format($r_data['jaminan'],0)."</td>
					<td>$r_data[bank]</td>
					<td>$r_data[nomor_ac]</td>
					<td>$r_data[nama_ac]</td>
					<td>$r_data[kontak]</td>
					<td align='center'>
					<a href='javascript:void(0)' onClick=\"edit_data('$r_data[kode_sp]')\">
					<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_data[kode_sp]')\">
					<img src='icon/thumb_down.png' border='0' id='hapus' title='Hapus' width='12' height='12' >
					</a>					
					</td>
					</tr>";
			$no++;
		}
		
	echo "</table>";
	
	echo "<table width='100%'>
		<tr>
			<td>Jumlah Data : $jml</td>
			<td align='right'>Halaman :";
		if (!empty($cari)){
			for ($h = 1; $h <= $max; $h++) {
					$list[$h] = $lim * $h - $lim;
					echo " <a href='javascript:void(0)' ";?> 
                    onClick="$.get('modul/supplier/tampil_data.php?cari=<?php echo $_GET[cari];?>&hal=<?php echo $list[$h]; ?>', 
                    null, function(data) {$('#tampil_data').html(data);})" <?php echo">".$h."</a> ";
				}		
		}
		else{
			for ($h = 1; $h <= $max; $h++) {
					$list[$h] = $lim * $h - $lim;
					echo " <a href='javascript:void(0)' ";?> 
                    onClick="$.get('modul/supplier/tampil_data.php?hal=<?php echo $list[$h]; ?>', 
                    null, function(data) {$('#tampil_data').html(data);})" <?php echo">".$h."</a> ";
				}	
		}

	echo "</td>
		</tr>
		</table>";
?>