<?php
$tahun_sekarang = date("Y",time());
$bulan_sekarang = (int) date("m",time());
function fetch_month($month){
	if($month==1) { $mnth= 'Januari'; }
	elseif($month==2) { $mnth= 'Februari'; }
	elseif($month==3) { $mnth= 'Maret'; }
	elseif($month==4) { $mnth= 'April'; }
	elseif($month==5) { $mnth= 'Mei'; }
	elseif($month==6) { $mnth= 'Juni'; }
	elseif($month==7) { $mnth= 'Juli'; }
	elseif($month==8) { $mnth= 'Agustus'; }
	elseif($month==9) { $mnth= 'September'; }
	elseif($month==10) { $mnth= 'Oktober'; }
	elseif($month==11) { $mnth= 'November'; }
	elseif($month==12) { $mnth= 'Desember'; }
	else{$mnth= 'error, contact admin';}
	return $mnth;
}
?>
<div id="konten">
	<div id="pageTitle">
		<?php echo $page_title;?>
	</div>
<hr class="thick-line" style="clear:both;">
<table style="padding-left:5px; font-size:13px;">
	<tr>
		<td>Tahun</td>
		<td>
			<span class="custom-dropdown custom-dropdown--emerald" style="width:80px">
				<select style="width:80px" name="tahun_opt" id="tahun_opt" onchange="set_lap()" class="custom-dropdown__select custom-dropdown__select--white">
					<?php
					foreach ($tahun as $val) {
						echo '<option value="'.$val.'">'.$val.'</option>';
					}
					?>
				</select>
			</span>	
		</td>
	</tr>
</table>
<hr class="thick-line" style="clear:both;">
<div class="cssTable" style="font-size:12px;">
	<table id="t_laporan">
			<tr>
				<th width="30">No</th>
				<th width="420">Jenis Laporan</th>
				<th>JAN</th>
				<th>FEB</th>
				<th>MAR</th>
				<th>APR</th>
				<th>MEI</th>
				<th>JUN</th>
				<th>JUL</th>
				<th>AGU</th>
				<th>SEP</th>
				<th>OKT</th>
				<th>NOV</th>
				<th>DES</th>
			</tr>
			<?php
			$x = 1;
			foreach ($jenis_laporan as $item) {
				$laporan = $item['nama'];
				echo '<tr>';
					echo "<td width='40'>".$x."</td>";
					echo "<td width='400' lap_pidmil='".$item['lap_pidmil']."' tahapan='".$item['tahapan']."' name='".$item['id']."'>".$laporan."</td>";
					$bln_esok = 12-$bulan_sekarang;
					for ($i=1; $i <= $bulan_sekarang; $i++) { 
						echo "<td><a href='#'>tabular</a></td>";
					}
				for ($y=$bulan_sekarang; $y < 12; $y++) { 
						echo "<td>-</td>";
					}
				echo '</tr>';
				$x++;
			}
			?>

		</tbody>
	</table>
</div>
<div style="padding:10px;"></div>
<hr class="thick-line" style="clear:both;">
<div class="cssTable" style="font-size:12px;">
	<table id="t_rekapitulasi">
		<tbody>
			<col width="3%">
			<col width="57%">
            <col width="40%">
			<tr>
				<th width="40">No</th>
				<th width="500">Jenis Laporan</th>
				<th>Link</th>
			</tr>
			<?php
			$x = 1;
			foreach ($rekap_laporan as $rekap) {
				echo '<tr>';
					echo "<td width='40'>".$x."</td>";
					echo "<td width='400' idalur='".$rekap['idalur']."' name='".$rekap['id']."' tahapan='".$rekap['tahapan']."'>".$rekap['nama']."</td>";
					echo "<td align='center'><a href='#' id='tab'>tabular</a></td>";
				echo '</tr>';
				$x++;
			}
			?>
		</tbody>
	</table>
</div>
<script type="text/javascript">
var month = 0;
function set_lap() {
	var curr_year = new Date().getFullYear();
	var curr_month = new Date().getMonth()+1;
	var tahun = $('#tahun_opt').val()
	var safe = 2;

	$("#t_laporan tr").each(function(){
		var tableRow = $("td").filter(function() {
			return $(this).text() == "REKAPITULASI KLASIFIKASI PERKARA";
		}).closest("tr");
		var rowCount = $('#t_rekapitulasi tr:nth-child(2) td').length
		if(tahun!=curr_year){
			var list = '<ol>';
			for (i = 0; i < 12; i++) {
				col_num = safe+i;
				$(this).find("td:eq("+col_num+")").html('<a href="#">tabular</a>');
				list = list+'<li><a href="#" onClick=updateMonth('+(i+1)+')>'+fetch_month(i+1)+'</a></li>';
			}
			list = list+'</ol>';
			for (i = 2; i < rowCount; i++) {
				tableRow.children('td:eq('+i+')').html(list);
			}
		}else{
			col_num = safe+i;
			for (i = 2; i <= curr_month; i++) {
				$(this).find("td:eq("+i+")").html('<a href="#">tabular</a>');
			}
			for (i = curr_month; i < 12; i++) {
				col_num = safe+i;
				$(this).find("td:eq("+col_num+")").html('-');
			}
		}
	});
}


function updateMonth(val){
	month = val;
}
$('#t_laporan td').click(function(event){
	var col = $(this).parent().children().index($(this));
	if(col>1){
		var row = $(this).parent().parent().children().index($(this).parent());
		if($(this).text()=='-'){
			return;
		}
		var tahapan = $("#t_laporan tr:eq("+row+") td:eq(1)").attr('tahapan');
		var lap_pidmil = $("#t_laporan tr:eq("+row+") td:eq(1)").attr('lap_pidmil');
		var jenis = $("#t_laporan tr:eq("+row+") td:eq(1)").attr('name');
		var w = window.open('', 'view_laporan');
		$.post('<?php echo base_url()?>view_laporan_pidana_militer/laporan', {
			month: col-1,
			jenis:jenis,
			tahapan:tahapan,
			lap_pidmil:lap_pidmil,
			tahun_opt:$('#tahun_opt').val()
		}, function(response){
			var json = jQuery.parseJSON(response);
			events = '';
			if(json.st==1){
				w.location.href ='<?php echo base_url()?>view_laporan_pidana_militer';
			}else{
				message_show(json.msg)
			}
		});
	}
});

var events = '';
$('#t_rekapitulasi td').click(function(event){
	var col = $(this).parent().children().index($(this));
	if(col>1){
		var row = $(this).parent().parent().children().index($(this).parent());
		var th_name = $("#t_rekapitulasi tr:first th:eq("+col+")").attr('name')
		var tahapan = $("#t_rekapitulasi tr:eq("+row+") td:eq(1)").attr('tahapan');
		var jenis = $("#t_rekapitulasi tr:eq("+row+") td:eq(1)").attr('name');
		var idalur = $("#t_rekapitulasi tr:eq("+row+") td:eq(1)").attr('idalur');
		var tmp = $("#t_rekapitulasi tr:eq("+row+") td:eq(1)").text();
		var w = window.open('', 'view_laporan');
		var url;
		if(month!=0){
			url = '<?php echo base_url()?>view_laporan_pidana_militer/rekap_bulan';
		}else{
			url = '<?php echo base_url()?>view_laporan_pidana_militer/rekap';
		}
		$.post(url, {
			events: events,
			th_name:th_name,
			tahapan:tahapan,
			idalur:idalur,
			jenis:jenis,
			tahun_opt:$('#tahun_opt').val(),
			bulan:month
		}, function(response){
			var json = jQuery.parseJSON(response);
			events = '';
			if(json.st==1){
				w.location.href ='<?php echo base_url()?>view_laporan_pidana_militer';
				if(month!=0){
					month=0;
				}
				message_show(json.msg)
			}else{
				message_show(json.msg)
			}
		});
	}
});

$('#t_rekapitulasi a').click(function(event){
	events = $(this).attr('id')
});

function lap(evt){
	events = evt;
}

</script>
