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

$indicator_Query = "SELECT A.*
				  FROM i_program A
				  WHERE A.id_program = ?";
$PDO_stmt = $PDO->prepare($indicator_Query);
$PDO_stmt->bindValue(1, $id_program, PDO::PARAM_INT);
$PDO_stmt->execute();
$program_List = $PDO_stmt->fetch(PDO::FETCH_ASSOC);

if($flow_type == 3)
{
	$header_title = 'Senarai Pemohon';
	$table_info = 'Senarai Keseluruhan Permohonan Program';	
}

?>	

<style>
.select2-container--default .select2-results__option[aria-disabled=true] {
    display: none;
}
</style>

<!--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"><?=$header_title?> : PROGRAM <?=$program_List['desk_program']?></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">
			<!--begin::Dashboard--> 
			<!--begin::Row-->
			<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 collapsed" data-toggle="collapse" 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="collapse" data-parent="#accordionExample8">					
						<div class="row">
							<div class="col-md-6">
								<div class="card-body"><br>
									<?php
										createDropDown($PDO, 'Parlimen', 'i_parlimen', 'parlimen_sc', $my_details->user_parlimen, 'Semua Parlimen', 8, 3, 12, $readonly);
										createDropDown($PDO, 'Daerah', 'i_daerah', 'daerah_sc', $my_details->user_daerah, 'Semua Daerah', 8, 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;
										}
										
										if($id_program != 4)
										{
											createDropDown_Array_Special('DUN', $dunArray, 'dun_sc', $my_details->user_dun, 'Semua DUN', 8, 3, 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', 8, 3, 12, $readonly,'');
										
										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">Tahun Pengesahan</label>';
										createThn_simple('tahun_sah', '', false, 3, 0, 10, 0);
										echo '</div>';
									?>
								</div>
							</div>
							<div class="col-md-6">
								<div class="card-body"><br>
									<?php
										/*
										createDropDown($PDO, 'Cara Mohon', 'i_caramohon', 'caramohon_sc', '', 'Semua Cara Mohon', 8, 3, 12, $readonly);
										createDropDown($PDO, 'Cara Bayar', 'i_carabayar', 'carabayar_sc', '', 'Semua Cara Bayar', 8, 3, 12, $readonly);
										
										if($flow_type == 3)
										{
											$myArray = [];
											$myArray[] = array(1, 'BELUM DISAHKAN');
											$myArray[] = array(2, 'TELAH DISAHKAN');
											createDropDown_Array('Pengesahan EFT', $myArray, 'saheft_sc', '', 'Semua', 8, 3, 12, $readonly);
										}
										*/
										
										$mySQL = "SELECT id_pengguna AS id, nama as name FROM t_pengguna WHERE id_peranan IN (2,4) AND aktif = 1 ORDER BY name";										
										createDropDown_sql($PDO, 'Pegawai Terima', $mySQL, 'terima_sc', 0, 'SEMUA', 8, 3, 12, $readonly,'');
										
										$mySQL = "SELECT id_pengguna AS id, nama as name FROM t_pengguna WHERE id_peranan IN (5) AND aktif = 1 ORDER BY name";										
										createDropDown_sql($PDO, 'Pegawai Sah', $mySQL, 'sah_sc', 0, 'SEMUA', 8, 3, 12, $readonly,'');
									?>
								</div>
							</div>
						</div>
						<div class="row">
							<div class="col-md-12">
								<div class="card-body">
									<?php
										$info_text = 'Senarai Bayaran boleh ditambah sekiranya Status Bayaran (Belum Bayar) telah dipilih';
										echo '<h4>'.$info_text.'</h4>';
									?>
								</div>
							</div>
						</div>
						<div class="row">
							<div class="col-md-6">
								<div class="card-body">
									<?php			
										
										$myArray = [];
										$myArray[] = array(1, 'BELUM BAYAR');
										$myArray[] = array(2, 'TELAH BAYAR');
										$myArray[] = array(3, 'TELAH BAYAR (EFT GANTIAN)');
										createDropDown_Array('Status Bayaran *', $myArray, 'telahbayar_sc', '', 'SEMUA STATUS BAYARAN', 8, 3, 12, $readonly);
										echo '<div class="form-group m-form__group row col-md-12" id="tahun_sc_div">';
										echo '<label class="col-form-label col-md-3" id="tahun">Tahun Pembayaran *</label>';
										createThn_simple('tahun_sc', date('Y'), false, 8, 0, 20, 0);
										echo '</div>';
										
										createDropDown($PDO, 'Fasa Bayaran *', 'i_bayaranfasa', 'bayaranfasa_sc', 0, 'Sila Pilih Fasa Bayaran', 8, 3, 12, $readonly);
									?>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>  
			<!--begin::Row-->
			<div class="row">
				<?php
					$tbColumn = array();
					$tbColumn[] = array('Bil', 5);
					$tbColumn[] = array('Nama Anak Emas', 13);
					$tbColumn[] = array('No MyKid', 11);
					$tbColumn[] = array('No S.Beranak', 12);
					$tbColumn[] = array('Tarikh Sah', 10);
					$tbColumn[] = array('Tindakan', 0);
					
					$printOnlyTbColumn = array();
					$printOnlyTbColumn[] = array('Nama Ibu/Bapa', 12); 
					$printOnlyTbColumn[] = array('No KP', 10); 
					$printOnlyTbColumn[] = array('Nama Bank', 10); 
					$printOnlyTbColumn[] = array('No Akaun', 10); 
					$printOnlyTbColumn[] = array('Kadun', 7); 
					$printOnlyTbColumn[] = array('Tarikh Daftar', 10); 
					$printOnlyTbColumn[] = array('Tarikh Terima', 10); 
					$printOnlyTbColumn[] = array('Pegawai Terima', 10); 
					$printOnlyTbColumn[] = array('Tarikh Sah', 10); 
					$printOnlyTbColumn[] = array('Pegawai Sah', 10); 
					
					$userBtn = '';
					createTable('program_table', $tbColumn, $header_title.' : PROGRAM '.$program_List['desk_program'], $table_info, 2, $userBtn, 'ajax-modal', 12);
					
					echo '<input type="hidden" id="table_printonly_header" value=\''.json_encode($printOnlyTbColumn).'\'/>';
				?>
				
			</div>
			<div class="row">
				<?php
					if($my_details->user_level <= 5)
					{
						$userBtn = '<span><a class="btn btn-primary font-weight-bolder" id="bayarBtn">Bayar Semua</a></span><span>';
						createTable('program_table_tawar', $tbColumn, 'Senarai Untuk Pembayaran', $info_text, 0, $userBtn, 'ajax-modal2', 12);
					}
				?>
				
			</div>
			<!--end::Row-->
			<!--end::Dashboard-->
		</div>
		<!--end::Container-->
	</div>
	<!--end::Entry-->
</div>
<!--end::Content-->
					
<?php 
require('includes/footer.php');
?>


<script>
function paparBorang(pemohon, typeBorang)
{
	var dataOption = [];
	dataOption.readonly = 1;
	dataOption.pemohon = pemohon;
	
	post("p" + typeBorang + "_borang.php", dataOption);
}

function editBorang(pemohon, typeBorang)
{
	var dataOption = [];
	dataOption.readonly = 0;
	dataOption.pemohon = pemohon;
	
	post("p" + typeBorang + "_borang.php", dataOption);
}

var ajaxUrl = "general-sub_pemohon_list_ajax.php";
function getData()
{
	return {
		sqlOnly: 0,
		program: "<?=$id_program?>",
		flow_type: "<?=$flow_type?>",
		dun_sc: $('#dun_sc').val(),
		parlimen_sc: $('#parlimen_sc').val(),
		daerah_sc: $('#daerah_sc').val(),
		bangsa_sc: $('#bangsa_sc').val(),
		terima_sc: $('#terima_sc').val(),
		sah_sc: $('#sah_sc').val(),
		saheft_sc: $('#saheft_sc').val(),
		telahbayar_sc: $('#telahbayar_sc').val(),
		tahun_sc: $('#tahun_sc_year').val(),
		bayaranfasa_sc: $('#bayaranfasa_sc').val(),
		tahun_sah: $('#tahun_sah_year').val(),
	};
}

function reloadTable()
{
	reinitTable($('#table_name').val(), this.ajaxUrl, this.getData(), 4, 0);
}

var tawaranList = [];
var tahun_bayaran = '';
var fasa_bayaran = '';
var fasa_text = '';
$('#bayarBtn').hide();
function tambah(myRow, id, print)
{
	if (typeof(print)==='undefined') print = 0;
	
	if(tawaranList[id] > 0)
	{
		swal.fire({
			text: "Pemohon telah berada dalam senarai",
			icon: "error",
			buttonsStyling: false,
			confirmButtonText: "OK!",
			customClass: {
				confirmButton: "btn font-weight-bold btn-light-primary"
			}
		}).then(function() {									
		});
	}
	else
	{
		var row = myRow.parentNode.parentNode;
		var o_table = $('#program_table_tawar').DataTable();
		var newData = [];
		//var tolakExceed_msg = "Pemohon " + row.cells[6].innerHTML + " (" + row.cells[5].innerHTML + ") tidak layak untuk ditawar kerana pemohon telah tolak tawaran " + MAX_TOLAK + " kali ke atas. \n\nAdakah anda ingin teruskan dengan tawaran rumah?";
		//var milikRumah_msg = "Pemohon " + row.cells[6].innerHTML + " (" + row.cells[5].innerHTML + ") tidak layak untuk ditawar kerana pemohon / pasangan telah milik rumah atau masih mempunyai tunggakan sewa senggara. \n\nAdakah anda ingin teruskan dengan tawaran rumah?";
		//if( (Number(row.cells[BIL_TOLAK_INDEX].innerHTML) < MAX_TOLAK || confirm(tolakExceed_msg)) && (row.cells[ADA_RUMAH_INDEX].innerHTML == 'TIDAK' || confirm(milikRumah_msg)))
		{
			for(var num = 0; num < row.cells.length - 1; num++)
			{
				newData.push(row.cells[num].innerHTML);
			}
			var svgicon = '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="24px" height="24px" viewBox="0 0 24 24" version="1.1"><g stroke="none" stroke-width="1" fill="none" fill-rule="evenodd"><rect x="0" y="0" width="24" height="24"/><circle fill="#000000" opacity="0.3" cx="12" cy="12" r="10"/><rect fill="#000000" x="6" y="11" width="12" height="2" rx="1"/></g></svg>';
			newData.push('<a class="btn btn-sm btn-default btn-text-danger btn-hover-danger btn-icon mr-2" onclick="deleteTawaran(this, '+id+')"><span class="svg-icon svg-icon-danger svg-icon-2x">' + svgicon + '</span></a>');
			var rowNode = o_table.row.add( newData  ).draw().node();
			tawaranList[id] = 1;
			$('#tambahBtn_' + id).hide();
			$('#bayarBtn').show();
		}
	}
}

function deleteTawaran(myBtn, id)
{
	var o_table = $('#program_table_tawar').DataTable();
	var row = myBtn.parentNode.parentNode;
	var myRow = o_table.row( row.rowIndex - 1);
	//if(confirm("Pastikan penghapusan tawaran " + row.cells[6].innerHTML + " ( No KP Baru : " + row.cells[6].innerHTML + ") ?"))
	{
		myRow.remove().draw();
		delete tawaranList[id];
		$('#tambahBtn_' + id).show();
		if(tawaranList.length == 0)
		{
			$('#bayarBtn').hide();
		}
	}
}
$(document).ready(function(){  

	$('#ajax-modal').on('shown.bs.modal', function () {  // this is to clear modal cache each time modal load
	 $(this).removeData('bs.modal');
	}); 
	
	$('#daerah_sc').change(function(){
		$('.all_daerah').attr('disabled', 'disabled');
		$('.daerah_' + $('#daerah_sc').val()).removeAttr('disabled');
		$('#dun_sc').val(0);
		$('#dun_sc').trigger("change");
	});
	$('#daerah_sc').trigger("change");
	
	$('#tahun_sc_div').hide();
	$('#bayaranfasa_sc_div').hide();
	
	$('#telahbayar_sc').change(function(){
		if($('#telahbayar_sc').val() == 1)//belum bayar
		{
			$('#tahun_sc_div').show();
			$('#bayaranfasa_sc_div').hide();
		}
		else
		{
			$('#tahun_sc_div').hide();
			$('#bayaranfasa_sc_div').show();
		}
	});
	
	initTable($('#table_name').val(), ajaxUrl, getData(), 4, 0);
	
	//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(), getData(), $('#table_printonly_header').val(), 'L');
	});
	
	$('#dun_sc,#parlimen_sc,#daerah_sc,#bangsa_sc,#caramohon_sc,#carabayar_sc,#terima_sc,#sah_sc,#telahbayar_sc,#tahun_sc_year,#tahun_sah_year,#bayaranfasa_sc').change(function(){
	//$('#janaBtn').click(function(){
		reloadTable();
		tahun_bayaran = $('#tahun_sc_year').val();
		//fasa_bayaran = $('#bayaranfasa_sc').val();
		//fasa_text = $("#bayaranfasa_sc option:selected").text();
	});
	
	$('#bayarBtn').click(function(){
		var param = '';
		for(var key in tawaranList)
		{
			param += key + ',';
		}
		if(param == '')
		{
			swal.fire({
				text: "Tiada Pemohon dalam Senarai Bayaran",
				icon: "error",
				buttonsStyling: false,
				confirmButtonText: "OK!",
				customClass: {
					confirmButton: "btn font-weight-bold btn-light-primary"
				}
			}).then(function() {									
			});
			return false;
		}
		else
		{
			loadMyModal('general-sub_pemohon_list_bayar.php?id=' + param + '&tahun=' + tahun_bayaran + /*'&fasa=' + fasa_bayaran + '&fasatext=' + encodeURI(fasa_text) +*/ '&program=' + "<?=$program_List['id_program']?>", '#ajax-modal');
		}
	});
});
</script>