<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 pilih(ID){
		var kode = ID;
		
		$.ajax({
			type	: "POST",
			url	: "modul/report/cari_kar.php",
			data	: "kode="+kode,
			dataType: "json",
			success	: function(data){				
				$("#filter").hide();
				$("#data").hide();
				$("#tampil_detail").show();
				$("#tampil_detail").load('modul/report/info_kar.php?kode='+kode);
			}
		});
	}
	
</script>
<?php
include '../../inc/inc.koneksi.php';
include '../../inc/fungsi_tanggal.php';

$cari= $_GET['cari'];
$hal	= $_GET['hal'] ? $_GET['hal'] : 0;
$lim	= 15;
$tanggal=date('Y-m-d');
$th_now = (int) substr($tanggal, 0, 4);
$bl_now = (int) substr($tanggal, 5, 2);
$tg_now = (int) substr($tanggal, 8, 2);

if (!empty($cari)){
	$query2	= "SELECT * FROM departemen INNER JOIN karyawan on karyawan.kode_dep = departemen.kode_dep WHERE (kode_kar LIKE '%$cari%' OR nama_kar LIKE '%$cari%' OR nama_dep LIKE '%$cari%') ";
}
else{
	$query2	= "SELECT * FROM departemen INNER JOIN karyawan on karyawan.kode_dep = departemen.kode_dep";
}
$data2	= mysql_query($query2);
$jml	= mysql_num_rows($data2);

$max	= ceil($jml/$lim);

echo "<div id='global'>
		<table id='theList' width='100%'>
		<tr>
			<th width='2%'>No.</th>
			<th>Departemen</th>
			<th>Pilih</th>
			<th>Pas Foto</td>
			<th>Kode</th>
			<th>Nama Karyawan</th>
			<th>Tgl.Masuk</th>
			<th>Masa Kerja</th>
			<th>Jabatan</th>
			<th>Lokasi Kerja</th>			
		</tr>";
	if (!empty($cari)){
		$sql	= "SELECT * FROM departemen INNER JOIN karyawan on karyawan.kode_dep = departemen.kode_dep 
					WHERE (kode_kar LIKE '%$cari%' OR nama_kar LIKE '%$cari%' OR nama_dep LIKE '%$cari%')
				    ORDER BY kode_kar LIMIT $hal,$lim";
	}
	else{
		$sql	= "SELECT * FROM departemen INNER JOIN karyawan on karyawan.kode_dep = departemen.kode_dep ORDER BY kode_kar LIMIT $hal,$lim";
	}
						
		$query = mysql_query($sql);

		$no=1+$hal;
		while($r_data=mysql_fetch_array($query)){
			$foto = $r_data['foto'];
			$th_masuk = (int) substr($r_data[tanggal_msk], 0, 4);
			$bl_masuk = (int) substr($r_data[tanggal_msk], 5, 2);
			$tg_masuk = (int) substr($r_data[tanggal_msk], 8, 2);
						
			$jt_masuk=$th_now-$th_masuk-1;			
			$jb_masuk=12-$bl_masuk+$bl_now-1;			
			$kb_masuk = $th_masuk;			
			
			while($kb_masuk>4){
				$kb_masuk = $kb_masuk-4;
			}
			
			if($bl_masuk==1)
				$jh_masuk=31-$tg_masuk+$tg_now;
				if($jh_masuk>30){
					$jh_masuk=$jh_masuk-31;
					$jb_masuk=$jb_masuk+1;
				}
			else  if($bl_masuk=2)
				if($kb_masuk==0){
					$jh_masuk=29-$tg_masuk+$tg_now;
					if($jh_masuk>28){
						$jh_masuk=$jh_masuk-29;
						$jb_masuk=$jb_masuk+1;
					}
				}else{
					$jh_masuk=28-$tg_masuk+$tg_now;
					if($jh_masuk>27){
						$jh_masuk=$jh_masuk-28;
						$jb_masuk=$jb_masuk+1;
					}
				}				
			else if($bl_masuk==3)
				$jh_masuk=31-$tg_masuk+$tg_now;
				if($jh_masuk>30){
					$jh_masuk=$jh_masuk-31;
					$jb_masuk=$jb_masuk+1;
				}
			else if($bl_masuk==4)
				$jh_masuk=30-$tg_masuk+$tg_now;
				if($jh_masuk>29){
					$jh_masuk=$jh_masuk-30;
					$jb_masuk=$jb_masuk+1;
				}
			else if($bl_masuk==5)
				$jh_masuk=31-$tg_masuk+$tg_now;
				if($jh_masuk>30){
					$jh_masuk=$jh_masuk-31;
					$jb_masuk=$jb_masuk+1;
				}
			else if($bl_masuk==6)
				$jh_masuk=30-$tg_masuk+$tg_now;
				if($jh_masuk>29){
					$jh_masuk=$jh_masuk-30;
					$jb_masuk=$jb_masuk+1;
				}
			else if($bl_masuk==7)
				$jh_masuk=31-$tg_masuk+$tg_now;
				if($jh_masuk>30){
					$jh_masuk=$jh_masuk-31;
					$jb_masuk=$jb_masuk+1;
				}
			else if($bl_masuk==8)
				$jh_masuk=31-$tg_masuk+$tg_now;
				if($jh_masuk>30){
					$jh_masuk=$jh_masuk-31;
					$jb_masuk=$jb_masuk+1;
				}
			else if($bl_masuk==9)
				$jh_masuk=30-$tg_masuk+$tg_now;
				if($jh_masuk>29){
					$jh_masuk=$jh_masuk-30;
					$jb_masuk=$jb_masuk+1;
				}
			else if($bl_masuk==10)
				$jh_masuk=31-$tg_masuk+$tg_now;
				if($jh_masuk>30){
					$jh_masuk=$jh_masuk-31;
					$jb_masuk=$jb_masuk+1;
				}
			else if($bl_masuk==11)
				$jh_masuk=30-$tg_masuk+$tg_now;
				if($jh_masuk>29){
					$jh_masuk=$jh_masuk-30;
					$jb_masuk=$jb_masuk+1;
				}
			else
				$jh_masuk=31-$tg_masuk+$tg_now;
				if($jh_masuk>30){
					$jh_masuk=$jh_masuk-31;
					$jb_masuk=$jb_masuk+1;
				}
			
			if($jb_masuk>11){
				$jb_masuk=$jb_masuk-12;
				$jt_masuk=$jt_masuk+1;
			}
						
			$ms_kerja=$jt_masuk.' th '.$jb_masuk.' bl '.$jh_masuk.' hr';
			
			echo "<tr>
					<td align='center'>$no</td>
					<td>$r_data[nama_dep]</td>
					<td align='center'>
						<a href='javascript:void(0)' onClick=\"pilih('$r_data[kode_kar]')\">
							<img src='images/view.png' border='0' id='edit' title='View' width='20height='20'>
						</a>						
					</td>
					<td><img src='modul/foto/$foto' width='60' height='70'/></td>
						<td>$r_data[kode_kar]</td>
					<td>$r_data[nama_kar]</td>
					<td align='center'>".jin_date_str($r_data[tanggal_msk])."</td>
					<td>$ms_kerja</td>
					<td>$r_data[jabatan]</td>
					<td>$r_data[lokasi]</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/report/tampil_data_kar.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/report/tampil_data_kar.php?hal=<?php echo $list[$h]; ?>', 
                    null, function(data) {$('#tampil_data').html(data);})" <?php echo">".$h."</a> ";
				}	
		}

	echo "</td>
		</tr>
		</table>";
	echo "</div>";
	
	echo "<div id='detail'></div>"
?>
