<?php
if($rekap!=''){
	if($rekap->num_rows>0){
		$data[0]['blm_minutasi'] = 0;
		$data[0]['mediasi_berhasil_blm_minut']=0;
		$data[0]['sisa'] = 0;
		foreach ($rekap->result() as $row) {
			$minut = substr($row->minutasi,0,4);
			$th_putus = substr($row->putusan,0,4);
			$tgl_pendaftaran = substr($row->tanggal_pendaftaran,0,4);
			$th_mediasi = substr($row->mediasi,0,4);
			if((empty($row->minutasi) OR $minut>=$tahun) AND !empty($row->putusan) AND $th_putus<$tahun AND $tgl_pendaftaran<$tahun){
					$data[0]['blm_minutasi']++;
			}

			if((empty($row->putusan) OR $th_putus>=$tahun) AND $tgl_pendaftaran<$tahun){
					$data[0]['sisa']++;
			}

			if((empty($row->minutasi) OR $minut>=$tahun) AND $row->hasil_mediasi=='Y' AND !empty($row->mediasi) AND $th_mediasi<$tahun AND $tgl_pendaftaran<$tahun){
					$data[0]['mediasi_berhasil_blm_minut']++;
			}
		}
		for ($i=1; $i <=12 ; $i++) {
			$data[$i]['masuk'] = 0;
			$data[$i]['putus'] = 0;
			$data[$i]['majelis'] = 0;
			$data[$i]['mediasi_gagal'] = 0;
			$data[$i]['mediasi_berhasil'] = 0;
			$data[$i]['gugur'] = 0;
			$data[$i]['cabut'] = 0;
			$data[$i]['minutasi'] = 0;
			$data[$i]['blm_minutasi'] = 0;
			$data[$i]['beban'] = 0;
			$data[$i]['mediasi_berhasil_blm_minut'] = 0;
			$bln = ($i<10)? '0'.$i:$i;
			$tgl = $tahun.'-'.$bln;
			$date_opt = strtotime($tgl);
			foreach ($rekap->result() as $row) {
				
				if($row->bulan_pendaftaran==$i AND $row->tahun_pendaftaran==$tahun){
					$data[$i]['masuk']++;
					
				}
				$tgl_pendaftaran = strtotime(substr($row->tanggal_pendaftaran,0,7));
				if($tgl_pendaftaran<=$date_opt){
					$penetapan = strtotime($row->majelis);
					if(empty($row->majelis) OR $penetapan>$date_opt){
						$data[$i]['majelis']++;
					}
				}
				if(!empty($row->gugur) AND $row->gugur==$tgl){
					$data[$i]['gugur']++;
				}
				if(!empty($row->cabut) AND $row->cabut==$tgl){
					$data[$i]['cabut']++;
				}
				
				if(!empty($row->minutasi) AND $row->minutasi==$tgl){
					$data[$i]['minutasi']++;
				}
				if(!empty($row->mediasi) AND $row->mediasi==$tgl){
					if($row->hasil_mediasi=='Y'){
						$data[$i]['mediasi_berhasil']++;
					}else{
						$data[$i]['mediasi_gagal']++;
					}
				}
				if(!empty($row->mediasi) AND $row->mediasi==$tgl AND $row->hasil_mediasi=='Y' AND $row->minutasi!=$tgl){
					$data[$i]['mediasi_berhasil_blm_minut']++;
				}
				if($row->putusan==$tgl){
					$data[$i]['putus']++;
				}
			}
			$data[$i]['blm_minutasi'] = $data[$i-1]['blm_minutasi']+($data[$i]['putus']-$data[$i]['minutasi']);
			$data[$i]['beban'] = $data[$i-1]['sisa']+$data[$i-1]['blm_minutasi']+$data[$i-1]['mediasi_berhasil_blm_minut']+$data[$i]['masuk'];
			$data[$i]['sisa'] = $data[$i]['beban']-$data[$i]['minutasi']-$data[$i]['blm_minutasi'];
			$data[$i]['sisa_akhir'] = $data[$i]['beban']-$data[$i]['gugur']-$data[$i]['cabut']-$data[$i]['minutasi'];
			$data[$i]['jml_putus'] = $data[$i-1]['blm_minutasi']+$data[$i]['putus'];
			$data[$i]['jml_mediasi_berhasil'] = $data[$i]['mediasi_berhasil_blm_minut']+$data[$i-1]['mediasi_berhasil_blm_minut'];
		}
	}
}
?>

<div style="padding:5px;width:1024px;">
<div style="padding:20px;"></div>
<button class="btn btnExcel" style="width:150px;text-align:center;">Export Excel</button></div>
<table id="view_laporan" class="cetakExcel" border="0" cellpadding="2" cellspacing="0" style="width:1024px;">
	<tbody>
		<tr>
			<td align="center" colspan="18" style="text-align: center;border:0px;border-top:0;font-size:14px;border-right:0px;">REKAPITULASI PERKARA <?php echo $perkara_nama;?></td>
		</tr>
		<tr>
			<td align="center" colspan="18" style="text-align: center;border:0px;font-size:14px;border-right:0px;"><?php echo $nama_pn;?></td>

		</tr>
		<tr>
			<td align="center" colspan="18" style="text-align: center;border:0px;font-size:14px;border-bottom:1px solid #000000;border-right:0px;">Januari - Desember <?php echo $tahun;?></td>

		</tr>
	</tbody>
	<tbody>
		<tr>
			<th rowspan="2">NO</th>
			<th rowspan="2">BULAN</th>
			<th colspan="3">SISA BULAN LALU</th>
			<th rowspan="2">MASUK</th>
			<th rowspan="2">BEBAN BULAN INI</th>
			<th rowspan="2">PUTUS BULAN INI</th>
			<th rowspan="2">BELUM PEMBAGIAN MAJELIS</th>
			<th rowspan="2">MEDIASI BERHASIL</th>
			<th rowspan="2">MEDIASI GAGAL</th>
			<th rowspan="2">GUGUR</th>
			<th rowspan="2">CABUT</th>
			<th rowspan="2">JUMLAH PUTUS</th>
			<th rowspan="2">JUMLAH MEDIASI BERHASIL</th>
			<th rowspan="2">MINUTASI BULAN INI</th>
			<th rowspan="2">BELUM MINUTASI</th>
			<th rowspan="2">SISA AKHIR PERKARA</th>
		</tr>
		<tr>
			<th style="border-left:0px;">BELUM PUTUS</th>
			<th>SUDAH PUTUS BELUM MINUTASI</th>
			<th>MEDIASI BERHASIL BELUM MINUTASI</th>
		</tr>
		<tr>
			<?php
			for ($i=1; $i<=18 ; $i++) { 
				echo "<td style='text-align:center;'>".$i."</td>";
			}?>
		</tr>
		<tr>
			<?php
			for ($i=1; $i<=18 ; $i++) { 
				echo "<td style='padding:10px;'></td>";
			}?>
		</tr>

		<?php
		if(isset($data)){
			$masuk = 0;
			$putus = 0;
			$berhasil = 0;
			$gagal = 0;
			$gugur = 0;
			$cabut = 0;
			$minutasi = 0;
			for ($i=1; $i<=12 ; $i++) { 
				echo "<tr>";
					echo "<td>".$i."</td>";
					echo "<td align='left' style='text-align:left;font-size: 9.0pt;'>".fetch_montd($i)."</td>";
					echo "<td style='text-align:center;'>".$data[$i-1]['sisa']."</td>";
					echo "<td style='text-align:center;'>".$data[$i-1]['blm_minutasi']."</td>";
					echo "<td style='text-align:center;'>".$data[$i-1]['mediasi_berhasil_blm_minut']."</td>";
					echo "<td style='text-align:center;'>".$data[$i]['masuk']."</td>";
					echo "<td style='text-align:center;'>".$data[$i]['beban']."</td>";
					echo "<td style='text-align:center;'>".$data[$i]['putus']."</td>";
					echo "<td style='text-align:center;'>".$data[$i]['majelis']."</td>";
					echo "<td style='text-align:center;'>".$data[$i]['mediasi_berhasil']."</td>";
					echo "<td style='text-align:center;'>".$data[$i]['mediasi_gagal']."</td>";
					echo "<td style='text-align:center;'>".$data[$i]['gugur']."</td>";
					echo "<td style='text-align:center;'>".$data[$i]['cabut']."</td>";
					echo "<td style='text-align:center;'>".$data[$i]['jml_putus']."</td>";
					echo "<td style='text-align:center;'>".$data[$i]['jml_mediasi_berhasil']."</td>";
					echo "<td style='text-align:center;'>".$data[$i]['minutasi']."</td>";
					echo "<td style='text-align:center;'>".$data[$i]['blm_minutasi']."</td>";
					echo "<td style='text-align:center;'>".$data[$i]['sisa_akhir']."</td>";
				echo "</tr>";
				$masuk = $masuk+$data[$i]['masuk'];
				$putus = $putus+$data[$i]['putus'];
				$berhasil = $berhasil+$data[$i]['mediasi_berhasil'];
				$gagal = $gagal+$data[$i]['mediasi_gagal'];
				$gugur = $gugur+$data[$i]['gugur'];
				$cabut = $cabut+$data[$i]['cabut'];
				$minutasi = $minutasi+$data[$i]['minutasi'];
			}
			echo "<tr>";
				echo "<th colspan='5' align='center'>TOTAL</th>";
				echo "<th>".$masuk."</th>";
				echo "<th></th>";
				echo "<th>".$putus."</th>";
				echo "<th></th>";
				echo "<th>".$berhasil."</th>";
				echo "<th>".$gagal."</th>";
				echo "<th>".$gugur."</th>";
				echo "<th>".$cabut."</th>";
				echo "<th></th>";
				echo "<th></th>";
				echo "<th>".$minutasi."</th>";
				echo "<th></th>";
				echo "<th></th>";
			echo "</tr>";
		}

		?>
		</tbody>
</table>
</div>
<script src="resources/js/jquery.table2excel.js"></script>
<script>
$(function() {
	$(".btnExcel").click(function(){
		$(".cetakExcel").table2excel({
			exclude: ".noExl",
			name: "Excel Document Name",
			filename: "Cetak Excel"
		});
	});
});
</script>