<?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">LAPORAN PEMBAYARAN HARIAN</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', 'id_daerah', '', 'Semua', 6, 3, 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, 3, 12, $readonly);
										
								createDropDown($PDO, 'Cara Bayar', 'i_carabayar', 'id_carabayar', '', 'Semua', 6, 3, 12, $readonly);	
								
								$my_sql = 'SELECT A.id_pengguna AS id, CONCAT(A.nama, " - ", B.desk_peranan) as name 
										   FROM t_pengguna A
										   LEFT JOIN t_peranan B ON B.id_peranan = A.id_peranan
										   WHERE A.id_peranan IN (2,4,5)
										   AND A.aktif = 1
										   ORDER BY A.id_peranan,A.nama';
								createDropDown_sql($PDO, 'Pegawai Sah', $my_sql, 'id_sah', '', 'Semua', 6, 3, 12);
								
								createDateTB('Tarikh Mula Bayar', 'tkh_mula', 'Tarikh Mula Bayar',(formatDate($indicator_List['tkh_mula'], 'd-m-Y') == ''?date('d-m-Y'):formatDate($indicator_List['tkh_mula'], 'd-m-Y')), 3, $readonly, 4, 12);				
								createDateTB('Tarikh Hingga Bayar', 'tkh_hingga', 'Tarikh Hingga Bayar',(formatDate($indicator_List['tkh_hingga'], 'd-m-Y') == ''?date('d-m-Y'):formatDate($indicator_List['tkh_tamat'], 'd-m-Y')), 3, $readonly, 4, 12);
								
								echo '<div class="form-group m-form__group row col-md-12" id="tahun_sah_div">';
								echo '<label class="col-form-label col-md-3" id="tahun">Untuk Bayaran Tahun</label>';
								createThn_simple('tahun_bayar', '', false, 2, 0, 10, 2);
								echo '</div>';								
								
								createDropDown($PDO, 'Fasa Bayaran', 'i_bayaranfasa', 'bayaranfasa_sc', 0, 'Semua Fasa Bayaran', 6, 3, 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 mr-5" id="janaBtn">Jana Laporan</button>
								<button type="button" class="btn btn-primary mr-5" id="cetakStaBtn" style="display:none">Cetak Laporan</button>
							</div>
						</div>
					</div>
				</div>
			</div>				
			<div class="row" id="resultRow" style="display:none">
				<div class="col-lg-12">
					<!--begin::List Widget 11-->
					<div class="card card-custom card-stretch gutter-b">
						<!--begin::Header-->
						<div class="card-header border-0">
							<h3 class="card-title font-weight-bolder text-dark">Senarai Pembayaran Harian</h3>
						</div>
						<!--end::Header-->
						<!--begin::Body-->
						<div class="card-body pt-0">
							<div id="resultDiv"></div>
						</div>
						<!--end::Body-->
					</div>
					<!--end::List Widget 11-->
				</div>
			</div>
		</div>
		<!--end::Container-->
	</div>
	<!--end::Entry-->
</div>
<!--end::Content-->
					
<?php 
require('includes/footer.php');

$id_program = fetch_postget_val("id_program", $_POST, $_GET);
$id_carabayar = fetch_postget_val("id_carabayar", $_POST, $_GET);
$id_sah = fetch_postget_val("id_sah", $_POST, $_GET);
$id_daerah = fetch_postget_val("id_daerah", $_POST, $_GET);
$tkh_mula = formatDate(fetch_postget_val("tkh_mula", $_POST, $_GET), 'Y-m-d');
$tkh_hingga = formatDate(fetch_postget_val("tkh_hingga", $_POST, $_GET), 'Y-m-d');
$tahun_bayar_year = fetch_postget_val("tahun_bayar_year", $_POST, $_GET);
$bayaranfasa_sc = fetch_postget_val("bayaranfasa_sc", $_POST, $_GET);
$dun_sc = fetch_postget_val("dun_sc", $_POST, $_GET);
?>

<script>
function showPemohon(program, carabayar, daerah, tkh_mula, tkh_hingga, pengesah, tahun_bayar_year, bayaranfasa_sc, dun_sc)
{
	var dataObject = [];
	dataObject['id_program'] = program;
	dataObject['id_carabayar'] = carabayar;
	dataObject['id_daerah'] = daerah;
	dataObject['tkh_mula'] = tkh_mula;
	dataObject['tkh_hingga'] = tkh_hingga;
	dataObject['id_sah'] = pengesah;
	dataObject['tahun_bayar_year'] = tahun_bayar_year;
	dataObject['bayaranfasa_sc'] = bayaranfasa_sc;
	dataObject['dun_sc'] = dun_sc;
	
	post('la_harian-pemohon.php', dataObject);
}

$(document).ready(function(){
	
	$('#id_daerah').change(function(){
		$('.all_daerah').attr('disabled', 'disabled');
		$('.daerah_' + $('#id_daerah').val()).removeAttr('disabled');
		$('#dun_sc').val(0);
		$('#dun_sc').trigger("change");
	});
	$('#id_daerah').trigger('change');

	$('#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() {
		var indicator_list = [[]];
		//indicator_list.push(['id_daerah', 'Daerah', 'dd']);
		
		if (checkInput(indicator_list))
		{
			$('#resultRow').show();
			$('#janaBtn').html("LOADING...");
			$('#janaBtn').addClass('disabled');
			$('#resultDiv').html("Sedang Jana Laporan ...");
			var param = $('#tapisan_form').serialize();
			
			$.ajax({
			   type: "POST",
			   url: "la_harian-check.php",
			   data: param,
			   success: function(res){
					$('#resultDiv').html(res);
					$('#janaBtn').html("Jana Laporan");
					$('#janaBtn').removeClass('disabled');
				
					var cetakInput = 'Laporan Pembayaran Harian';
					cetakInput += '|BRE|' + $("#id_daerah option:selected").text();
					cetakInput += '|BRE|' + $("#id_carabayar option:selected").text();
					cetakInput += '|BRE|' + $("#id_sah option:selected").text();
					cetakInput += '|BRE|' + $("#tkh_mula").val();
					cetakInput += '|BRE|' + $("#tkh_hingga").val();
					cetakInput += '|BRE|' + $("#tahun_bayar_year option:selected").text();
					cetakInput += '|BRE|' + $("#bayaranfasa_sc option:selected").text();
					cetakInput += '|BRE|' + $("#dun_sc option:selected").text();
					
					var cetakInputValue = 'Laporan Pembayaran Harian';
					cetakInputValue += '|BRE|' + $("#id_daerah").val();
					cetakInputValue += '|BRE|' + $("#id_carabayar").val();
					cetakInputValue += '|BRE|' + $("#id_sah").val();
					cetakInputValue += '|BRE|' + $("#tkh_mula").val();
					cetakInputValue += '|BRE|' + $("#tkh_hingga").val();
					cetakInputValue += '|BRE|' + $("#tahun_bayar_year").val();
					cetakInputValue += '|BRE|' + $("#bayaranfasa_sc").val();
					cetakInputValue += '|BRE|' + $("#dun_sc").val();
					
					$('#cetakInput').val(cetakInput);
					$('#cetakInputValue').val(cetakInputValue);
					$('#cetakStaBtn').show();
			   }
			});
		}
	});
	
	$('#cetakStaBtn').click(function(){
		var allTable = document.getElementsByClassName("statTable");
		console.log(allTable);
		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>