<?php 
require('includes/header.php');  
?>	

<style>
.select2-container--default .select2-results__option[aria-disabled=true] {
    display: none;
}
</style>
<link href="//www.amcharts.com/lib/3/plugins/export/export.css" rel="stylesheet" type="text/css" />
<!--begin::Content-->
<div class="content d-flex flex-column flex-column-fluid" id="kt_content">
	<!--begin::Subheader-->
	<div class="subheader py-2 py-lg-4 subheader-solid" id="kt_subheader">
		<div class="container-fluid d-flex align-items-center justify-content-between flex-wrap flex-sm-nowrap">
			<!--begin::Info-->
			<div class="d-flex align-items-center flex-wrap mr-2">
				<!--begin::Page Title-->
				<h5 class="text-dark font-weight-bold mt-2 mb-2 mr-5">Statistik Permohonan Borang</h5>
				<!--end::Page Title-->
				<!--begin::Actions-->
				<div class="subheader-separator subheader-separator-ver mt-2 mb-2 mr-4 bg-gray-200"></div>
			</div>
			<!--end::Info-->
		</div>
	</div>
	<!--end::Subheader-->
	<!--begin::Entry-->
	<div class="d-flex flex-column-fluid">
		<!--begin::Container-->
		<div class="container">
			<div class="accordion accordion-solid accordion-panel accordion-svg-toggle" id="accordionExample8">
				<div class="card">
					<div class="card-header" id="headingThree8">
						<div class="card-title" data-toggle="" data-target="#collapseThree8">
							<div class="card-label">													
								<span class="svg-icon svg-icon-primary menu-icon">
									<?=$Tapisanhome_svg?>
								</span>Tapisan</div>
							<span class="svg-icon">
								<?=$Tapisan_svg?>
							</span>
						</div>
					</div>
					<div id="collapseThree8" class="" data-parent="#accordionExample8">
						<div class="card-body"><br>
							<form id="tapisan_form">
							<?php
								createDropDown($PDO, 'Daerah', 'i_daerah', 'daerah_sc', 0, 'Semua Daerah', 6, 2, 12, $readonly);
								
								$Query = "SELECT id_dun AS id, desk_dun as name, id_daerah
											FROM i_dun
											WHERE aktif = 1
											ORDER BY susunan";
								$PDO_stmt = $PDO->prepare($Query);
								$PDO_stmt->execute();
								$dunArray = [];
								while($result = $PDO_stmt->fetch(PDO::FETCH_ASSOC))
								{
									$temp = array($result['id'], $result['name'], ' class="all_daerah daerah_'.$result['id_daerah'].'"');		
									$dunArray[] = $temp;
								}
								createDropDown_Array_Special('DUN', $dunArray, 'dun_sc', 0, 'Semua DUN', 6, 2, 12, $readonly);
								
								$Query = "SELECT id_parlimen AS id, desk_parlimen as name, id_daerah
											FROM i_parlimen
											WHERE aktif = 1
											ORDER BY susunan";
								$PDO_stmt = $PDO->prepare($Query);
								$PDO_stmt->execute();
								$parlimenArray = [];
								while($result = $PDO_stmt->fetch(PDO::FETCH_ASSOC))
								{
									$temp = array($result['id'], $result['name'], ' class="all_daerah daerah_'.$result['id_daerah'].'"');		
									$parlimenArray[] = $temp;
								}								
								createDropDown_Array_Special('Parlimen', $parlimenArray, 'parlimen_sc', 0, 'Semua Parlimen', 6, 2, 12, $readonly);
								
								createDropDown($PDO, 'Program', 'i_program', 'program_sc', 0, 'Semua Program', 6, 2, 12, $readonly);
								
								$mySQL = "SELECT id_bangsa AS id, desk_bangsa as name FROM i_bangsa WHERE id_bangsa IN (1,2,3,5) ORDER BY susunan";										
								createDropDown_sql($PDO, 'Bangsa', $mySQL, 'bangsa_sc', 0, 'Semua Bangsa', 6, 2, 12, $readonly,'');
								
								echo '<div class="form-group m-form__group row col-md-12">';
								echo '<label class="col-form-label col-md-2" id="jangkamasa_label">Jangka Masa</label>';
								createBlnThn_simple('blnthnDari', '01/'.date('Y'), false, 2, 2, 0, 20, 0);
								echo '<label class="col-form-label col-md-1">Hingga</label>';
								createBlnThn_simple('blnthnHingga', '12/'.date('Y'), false, 2, 2, 0, 20, 0);
								echo '</div>';
								
								createDropDown($PDO, 'Status Permohonan', 'i_statusmohon', 'statusmohon_sc', 4, 'Semua Status Permohonan', 6, 2, 12, $readonly);
								
								$myArray = [];
								$myArray[] = array(1, 'BELUM DIBAYAR');
								$myArray[] = array(2, 'TELAH DIBAYAR');
								createDropDown_Array('Status Bayaran', $myArray, 'bayaran_sc', '', 'Semua Status Bayaran', 4, 2, 12, $readonly);
												
								createDropDown($PDO, 'Status Aktif', 'i_aktifmohon', 'aktifmohon_sc', 1, 'Semua Status Aktif', 6, 2, 12, $readonly);
								createDropDown($PDO, 'Status Hidup', 'i_statushidup', 'statushidup_sc', 1, 'Semua Status Hidup', 6, 2, 12, $readonly);
								createDropDown($PDO, 'Cara Pembayaran', 'i_carabayar', 'carabayar_sc', 0, 'Semua Cara Pembayaran', 6, 2, 12, $readonly);
							?>
							</form>
							<div style="text-align:center">
								<input type="hidden" id="cetakInput">
								<input type="hidden" id="cetakInputValue">
								<button type="button" class="btn btn-primary" id="janaBtn">Jana</button>
								<button type="button" class="btn btn-primary" id="cetakStaBtn" style="display:none">Cetak Statistik</button>
							</div>
						</div>
					</div>
				</div>
			</div>
			<!--begin::Row-->
			<div class="row">
				<div class="col-md-12">
					<div class="accordion accordion-toggle-arrow" id="accordionExample1">
						<div id="tableDiv"></div>
					</div>
				</div>
			</div>
			<div class="row">
				<div class="col-md-12">
					<div class="card">
						<div id="chartdiv"></div>
					</div>
				</div>
			</div>
			<!--end::Row-->
		</div>
		<!--end::Container-->
	</div>
	<!--end::Entry-->
</div>
<!--end::Content-->
					
<?php 
require('includes/footer.php');
?>

<script>
function cetakSenaraiNama(tahun, program)
{
	var dataObject = [];
	dataObject['kriteria'] = $('#cetakInput').val();
	dataObject['cetakInputValue'] = $('#cetakInputValue').val();
	dataObject['tahun'] = tahun;
	dataObject['program'] = program;
	console.log(dataObject);
	post($('#script_base_name').val() + '-cetak_senarai.php', dataObject);
}
$(document).ready(function(){  

	if($('#program_sc').val() == 5)
	{
		$('#aktifmohon_sc_div').hide();
		$('#aktifmohon_sc').val(0);
		$('#aktifmohon_sc').trigger('change');
		$('#statushidup_sc_div').hide();
		$('#statushidup_sc').val(0);
		$('#statushidup_sc').trigger('change');
		$('#carabayar_sc_div').hide();
		$('#carabayar_sc').val(0);
		$('#carabayar_sc').trigger('change');
		$('#bayaran_sc_div').show();
	}
	else
	{
		$('#aktifmohon_sc_div').show();
		$('#statushidup_sc_div').show();
		$('#carabayar_sc_div').show();
		$('#bayaran_sc_div').hide();
		$('#bayaran_sc').val(0);
		$('#bayaran_sc').trigger('change');
	}
	
	$('#program_sc').change(function() {
		if($('#program_sc').val() != 5)
		{
			$('#aktifmohon_sc_div').show();
			$('#statushidup_sc_div').show();
			$('#carabayar_sc_div').show();
			$('#bayaran_sc_div').hide();
			$('#bayaran_sc').val(0);
			$('#bayaran_sc').trigger('change');
		}
		else
		{
			$('#aktifmohon_sc_div').hide();
			$('#aktifmohon_sc').val(0);
			$('#aktifmohon_sc').trigger('change');
			$('#statushidup_sc_div').hide();
			$('#statushidup_sc').val(0);
			$('#statushidup_sc').trigger('change');
			$('#carabayar_sc_div').hide();
			$('#carabayar_sc').val(0);
			$('#carabayar_sc').trigger('change');
			$('#bayaran_sc_div').show();
		}
	});
	$('#program_sc').trigger('change');

	$('#daerah_sc').change(function(){
		$('.all_daerah').attr('disabled', 'disabled');
		$('.daerah_' + $('#daerah_sc').val()).removeAttr('disabled');
	});

	$('#ajax-modal').on('shown.bs.modal', function () {  // this is to clear modal cache each time modal load
	 $(this).removeData('bs.modal');
	}); 
	
	//print function
	$('#printExcel, #printPdf').click(function() {
		printFunc($('#table_name').val(), $(this).attr("value"), "normalPrint.php", $('#script_base_name').val(), $('#table_title').val(), $('#table_header').val());
	});
	
	$('#janaBtn').click(function() {
		
		$('#janaBtn').html("LOADING...");
		$('#janaBtn').addClass('disabled');
		var param = $('#tapisan_form').serialize();
		$('#resultDiv').html('');
		$.ajax({
		   type: "POST",
		   url: $('#script_base_name').val() + "-check.php",
		   data: param,		   
		   success: function(res){
			    var showJumlah = false;
				var json_x = $.parseJSON(res);
				
			    var cetakInput = json_x.title[0].text;
				cetakInput += '|BRE|' + $("#dun_sc option:selected").text();
				cetakInput += '|BRE|' + $("#daerah_sc option:selected").text();
				cetakInput += '|BRE|' + $("#parlimen_sc option:selected").text();
				cetakInput += '|BRE|' + $("#program_sc option:selected").text();
				cetakInput += '|BRE|' + $('#blnthnDari_month').val();
				cetakInput += '|BRE|' + $('#blnthnDari_year').val();
				cetakInput += '|BRE|' + $('#blnthnHingga_month').val();
				cetakInput += '|BRE|' + $('#blnthnHingga_year').val();
				cetakInput += '|BRE|' + $("#statusmohon_sc option:selected").text();
				cetakInput += '|BRE|' + $("#statushidup_sc option:selected").text();
				cetakInput += '|BRE|' + $("#aktifmohon_sc option:selected").text();
				cetakInput += '|BRE|' + $("#carabayar_sc option:selected").text();
				cetakInput += '|BRE|' + $("#bangsa_sc option:selected").text();
				cetakInput += '|BRE|' + $("#bayaran_sc option:selected").text();
				
			    var cetakInputValue = json_x.title[0].text;
				cetakInputValue += '|BRE|' + $("#dun_sc").val();
				cetakInputValue += '|BRE|' + $("#daerah_sc").val();
				cetakInputValue += '|BRE|' + $("#parlimen_sc").val();
				cetakInputValue += '|BRE|' + $("#program_sc").val();
				cetakInputValue += '|BRE|' + $('#blnthnDari_month').val();
				cetakInputValue += '|BRE|' + $('#blnthnDari_year').val();
				cetakInputValue += '|BRE|' + $('#blnthnHingga_month').val();
				cetakInputValue += '|BRE|' + $('#blnthnHingga_year').val();
				cetakInputValue += '|BRE|' + $("#statusmohon_sc").val();
				cetakInputValue += '|BRE|' + $("#statushidup_sc").val();
				cetakInputValue += '|BRE|' + $("#aktifmohon_sc").val();
				cetakInputValue += '|BRE|' + $("#carabayar_sc").val();
				cetakInputValue += '|BRE|' + $("#bangsa_sc").val();
				cetakInputValue += '|BRE|' + $("#bayaran_sc").val();
				
			    $('#cetakInput').val(cetakInput);
			    $('#cetakInputValue').val(cetakInputValue);
				$('#cetakStaBtn').show();	

				$('#janaBtn').html("Jana");
				$('#janaBtn').removeClass('disabled');				
				
				if(Number($('#blnthnHingga_year').val()) - Number($('#blnthnDari_year').val()) >= 1)
				{
					$("#tableDiv").html(json_x.jumlah_data + json_x.data);
					showJumlah = true;
				}
				else
				{
					$("#tableDiv").html(json_x.data);
				}
				
				for(var num = Number($('#blnthnDari_year').val()) ; num <= Number($('#blnthnHingga_year').val()) ; num++)
				{
					$('#statTable_' + num).DataTable({
						"bPaginate": false,
						"bLengthChange": false,
						"searching": false,
						"ordering": false,
						"info": false,
					});
					
					var myData = json_x[num];
					
					var chart = AmCharts.makeChart( "chartDiv_" + num, {
						"type": "serial",
						"theme": "light",
						"depth3D": 0,
						"angle": 30,
						"autoResize": false,
						"autoDisplay": true,
						"legend": {
						"horizontalGap": 10,
						"useGraphSettings": true,
						"markerSize": 10
						},
						"dataProvider": myData.statData,
						"valueAxes": 
						[ 
							{
								"stackType": "regular",
								"axisAlpha": 0,
								"gridAlpha": 0
							} 
						],
						"graphs": myData.graphSetting,
						"categoryField": json_x.catField,
						"categoryAxis": {
							"gridPosition": "start",
							"axisAlpha": 0,
							"gridAlpha": 0,
							"position": "left",
							"equalSpacing": true,
							//"forceShowField": "forceShow",
							//"boldLabels" : true,
							"gridCount": myData.statData.length,
							"autoGridCount": false,
							"labelRotation": 90,
							"dashLength": 5,
						},
						"export": {
							"enabled": true
						},
						"titles": myData.title,
						"mouseWheelScrollEnabled": true,
						"mouseWheelZoomEnabled": true,
						//"pathToImages": "assets/global/plugins/amcharts/amcharts/images/",
						"chartScrollbar": {
							"dragIconHeight": 20,
							"dragIconWidth": 20,
							"graphType": "column",
							"resizeEnabled": true,
							"scrollbarHeight": 5,
							"scrollDuration": 0,
							"updateOnReleaseOnly": false,
							"oppositeAxis": false,
						}
					} );
					//console.log(chart);
				}
				
				if(showJumlah == true)
				{
					$('#statTable_jumlah').DataTable({
						"bPaginate": false,
						"bLengthChange": false,
						"searching": false,
						"ordering": false,
						"info": false,
					});
					
					var chart = AmCharts.makeChart( "chartDiv_jumlah", {
						"type": "serial",
						"theme": "light",
						"depth3D": 0,
						"angle": 30,
						"autoResize": false,
						"autoDisplay": true,
						"legend": {
						"horizontalGap": 10,
						"useGraphSettings": true,
						"markerSize": 10
						},
						"dataProvider": json_x.statData,
						"valueAxes": 
						[ 
							{
								"stackType": "regular",
								"axisAlpha": 0,
								"gridAlpha": 0
							} 
						],
						"graphs": json_x.graphSetting,
						"categoryField": json_x.catField,
						"categoryAxis": {
							"gridPosition": "start",
							"axisAlpha": 0,
							"gridAlpha": 0,
							"position": "left",
							"equalSpacing": true,
							//"forceShowField": "forceShow",
							//"boldLabels" : true,
							"gridCount": json_x.statData.length,
							"autoGridCount": false,
							"labelRotation": 90,
							"dashLength": 5,
						},
						"export": {
							"enabled": true,
						},
						"titles": json_x.title,
						"mouseWheelScrollEnabled": true,
						"mouseWheelZoomEnabled": true,
						"chartScrollbar": {
							"dragIconHeight": 20,
							"dragIconWidth": 20,
							"graphType": "column",
							"resizeEnabled": true,
							"scrollbarHeight": 5,
							"scrollDuration": 0,
							"updateOnReleaseOnly": false,
							"oppositeAxis": false,
						}
					} );
				}
		   }
	   });  //end ajax
	});
	
	$('#cetakStaBtn').click(function(){
		var allTable = document.getElementsByClassName("statTable");
		if(allTable.length > 0)
		{
			var dataObject = [];
			//dataObject['title'] = $('#' + allTable[0].id + '_title').html();
			dataObject['kriteria'] = $('#cetakInput').val();
			dataObject['allTable'] = '';
			for(var i=0; i < allTable.length; i+=1)
			{
				var tableId = allTable[i].id;
				//alert(tableId);
				var table = $("#" + tableId);
				var data = [];
				data.push($("#" + tableId + '_title').html());
				table.find('tr').each(function (rowIndex, r) 
				{
					var cols = [];
					$(this).find('th,td').each(function (colIndex, c) {
					cols.push(c.textContent);
					});
					data.push(cols.join('|'));
					//alert(cols);
				});
				dataObject[tableId] = data.join('|BRE|');
				dataObject['allTable'] += tableId + ',';
				//alert(data.join('<BR>'));
			}
			console.log(dataObject);
			post($('#script_base_name').val() + '-print.php', dataObject);
		}
		else
		{
			alert("Sila Jana Statistik dahulu sebelum cetak");
		}
	});
	
});
</script>