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

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

$kawalanyear = $kawalan_List['tahun_bayaran_tunai'];

$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';	
}

$PDO_stmt = $PDO->prepare("
	SELECT *
	FROM i_template
	WHERE aktif = 1
	AND flag_jenis = 2
	ORDER BY susunan
");
$PDO_stmt->bindValue(1, $program, PDO::PARAM_INT);
$PDO_stmt->execute();
$template_List = [];
while($result = $PDO_stmt->fetch(PDO::FETCH_ASSOC))
{
	$template_List[] = $result;
}

$templateBtn = '';
if (count($template_List) > 0) //if need to print
{
	$templateBtn .= '<div class="dropdown dropdown-inline mr-2" id="cetakTunaiDiv" style="display:none">';
		$templateBtn .= '<button type="button" class="btn btn-success font-weight-bolder dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">';
		$templateBtn .= 'Cetakan</button>';
		$templateBtn .= '<div class="dropdown-menu dropdown-menu-sm dropdown-menu-right">';
			$templateBtn .= '<ul class="navi flex-column navi-hover py-2">';
				$templateBtn .= '<li class="navi-header font-weight-bolder text-uppercase font-size-sm text-primary pb-2">Pilih Cetakan</li>';
				
				foreach($template_List as $item)
				{
					$templateBtn .= '
					<li class="navi-item">
						<a href="javascript:;" class="navi-link" onclick="cetakBaucer(\''.$item['nama_fail'].'\')">
							<span class="navi-text">'.$item['desk_template'].'</span>
						</a>
					</li>
					';
				}
			$templateBtn .= '</ul>';
		$templateBtn .= '</div>';
	$templateBtn .= '</div>';
}

$template5 = 'template5.php';
$suratBtn = '';
$suratBtn .= '<div class="dropdown dropdown-inline mr-2" id="cetaksuratDiv" style="display:none">';
	$suratBtn .= '<button type="button" class="btn btn-success font-weight-bolder dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">';
	$suratBtn .= 'Cetakan Surat</button>';
	$suratBtn .= '<div class="dropdown-menu dropdown-menu-sm dropdown-menu-right">';
		$suratBtn .= '<ul class="navi flex-column navi-hover py-2">';
			$suratBtn .= '<li class="navi-header font-weight-bolder text-uppercase font-size-sm text-primary pb-2">Pilih Cetakan</li>';
				$suratBtn .= '
				<li class="navi-item">
						<a href="javascript:;" class="navi-link" onclick="cetakSurat(\''.$template5.'\')">
						<span class="navi-text">Surat Makluman Tukar ke EFT</span>
					</a>
				</li>
				';
		$suratBtn .= '</ul>';
	$suratBtn .= '</div>';
$suratBtn .= '</div>';

?>	

<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="p_accordionExample8">
				<div class="card">
					<div class="card-header" id="p_headingThree8">
						<div class="card-title collapsed" data-toggle="collapse" data-target="#p_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="p_collapseThree8" class="collapse" data-parent="#p_accordionExample8">					
						<div class="row">
							<div class="col-md-12">
								<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);
										
										$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,'');
										
										createDropDown($PDO, 'Poskod', 'i_poskod', 'poskod_sc', '', 'Semua Poskod', 8, 3, 12, $readonly);
										createDropDown($PDO, 'Cara Mohon', 'i_caramohon', 'caramohon_sc', '', 'Semua Cara Mohon', 8, 3, 12, $readonly);
										
										if($id_program == 2)
										{
											$myArray = [];
											$myArray[] = array(1, 'TINGGAL SENDIRI');
											$myArray[] = array(2, 'RUMAH ORANG-ORANG TUA');
											createDropDown_Array('Status Huni', $myArray, 'orgtua_sc', '', 'Semua', 8, 3, 12, $readonly);
										
											createDropDown($PDO, 'Rumah Orang Tua', 'i_rumahorgtua', 'rmhorgtua_sc', '', 'Semua Rumah Orang Tua', 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, 2, 0, 10, 0);
										echo '</div>';
									?>
								</div>
							</div>
						</div>
						<div class="row">
							<div class="col-md-12">
								<div class="card-body">
									<?php
										$info_text = 'Senarai Bayaran boleh ditambah sekiranya Pilihan Daerah, Pilihan DUN, Status Bayaran (Belum Bayar) dan Tahun Pembayaran telah dipilih';
										echo '<h4>'.$info_text.'</h4>';
									?>
								</div>
							</div>
						</div>
						<div class="row">
							<div class="col-md-6">
								<div class="card-body">
									<?php
										
										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', 0, 'Semua DUN', 8, 3, 12, $readonly);
										//}
									
										//createDropDown($PDO, 'DUN *', 'i_dun', 'dun_sc', $my_details->user_dun, 'Semua DUN', 8, 3, 12, $readonly);
										
										$myArray = [];
										if($id_program != 4)
										{
											$myArray[] = array(1, 'BELUM BAYAR (TUNAI/EFT SAH SELEPAS CUTOFF)');
											$myArray[] = array(2, 'BELUM BAYAR (EFT SAH SEBELUM CUTOFF)');
										}
										else
										{
											$myArray[] = array(4, 'BELUM BAYAR');
										}
										$myArray[] = array(3, 'TELAH BAYAR');
										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);
										createThn_simple('tahun_sc', $kawalan_List['tahun_bayaran_tunai'], false, 8, 0, 1, 0);
										echo '</div>';*/							
										
										createThn('Tahun Pembayaran', 'tahun_sc_year', $kawalanyear, 3, 12, 8, 1, 1, 0);
										
										if($my_details->user_level <= 3)
										{
											createDateTB('Tarikh Cut Off', 'tkh_lbl', 'Tarikh Cut Off',(formatDate($kawalan_List['tkh_cutoff_tunai'], 'd M Y') == ''?date('d M Y'):formatDate($kawalan_List['tkh_cutoff_tunai'], 'd M Y')), 3, 0, 4, 12);										
										}
										else
										{
											createLabels('Tarikh Cut Off', 'tkh_lbl', formatDate($kawalan_List['tkh_cutoff_tunai'], 'd M Y'), '', 3, 8, 1, 12);
										}
										
										//createDropDown($PDO, 'Fasa Bayaran *', 'i_bayaranfasa', 'bayaranfasa_sc', 0, 'Sila Pilih Fasa Bayaran', 8, 3, 12, $readonly);
									?>
									<div style="text-align:left;display:none">
										<button type="button" class="btn btn-primary" id="janaBtn">Jana Senarai</button>
									</div>
								</div>
							</div>
							<div class="col-md-6">
								<div class="card-body">
									<?php
										if($id_program != 4)
										{									
											createDropDown($PDO, 'Cara Bayar Pilihan Pemohon', 'i_carabayar', 'carabayar_sc', '', 'Semua Cara Bayar', 8, 3, 12, $readonly);
											
											createDropDown($PDO, 'Bayar Kepada', 'i_byrkepada', 'byrkepada_sc', '', 'Semua Bayar Kepada', 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,6,15,8) AND aktif = 1 ORDER BY name";										
										createDropDown_sql($PDO, 'Pegawai Daftar', $mySQL, 'daftar_sc', 0, 'SEMUA', 8, 3, 12, $readonly,'');
										
										$mySQL = "SELECT id_pengguna AS id, nama as name FROM t_pengguna WHERE id_peranan IN (2,4,6,15,8) 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,7,16,9) AND aktif = 1 ORDER BY name";										
										createDropDown_sql($PDO, 'Pegawai Sah', $mySQL, 'sah_sc', 0, 'SEMUA', 8, 3, 12, $readonly,'');
												
										if($id_program != 4)
										{								
											$mySQL = "SELECT id_pengguna AS id, nama as name FROM t_pengguna WHERE id_peranan IN (2,4,5) AND aktif = 1 ORDER BY name";										
											createDropDown_sql($PDO, 'Pegawai Sah EFT', $mySQL, 'pegawai_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 = '* Cetakan Dokumen Bayaran Tunai sekiranya Pilihan Daerah, Pilihan DUN, Status Bayaran (Belum Bayar) dan Tahun Pembayaran telah dipilih';
										echo '<p class="d-block text-muted pt-2 font-size-sm">'.$info_text.'</p>';
									?>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>  
			<!--begin::Row-->
			<div class="row">
				<?php
					$tbColumn = array();
					$tbColumn[] = array('Bil', 5);
					$tbColumn[] = array('Nama Pemohon', 20);
					$tbColumn[] = array('No KP', 15);
					$tbColumn[] = array('Tarikh Sah', 10);
					$tbColumn[] = array('Cara Mohon', 10);
					$tbColumn[] = array('Cara Bayar', 10);
					$tbColumn[] = array('Tindakan', 0);
					
					$printOnlyTbColumn = array();
					$printOnlyTbColumn[] = array('No Tel', 10); 
					$printOnlyTbColumn[] = array('DUN', 10); 
					$printOnlyTbColumn[] = array('Alamat', 10); 
					$printOnlyTbColumn[] = array('Alamat(sambg)', 10); 
					$printOnlyTbColumn[] = array('Alamat(sambg)', 10); 
					$printOnlyTbColumn[] = array('Poskod', 10); 
					$printOnlyTbColumn[] = array('Bandar', 10); 
					$printOnlyTbColumn[] = array('Negeri', 10); 
					$printOnlyTbColumn[] = array('Bank', 10); 
					$printOnlyTbColumn[] = array('No Akaun', 10); 
					$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); 
					$printOnlyTbColumn[] = array('Tarikh Pengesahan EFT', 10); 
					$printOnlyTbColumn[] = array('Pegawai Sah EFT', 10); 
					
					$userBtn = '';
					if($id_program != 4)
					{
						$userBtn = '<span><a class="btn btn-primary font-weight-bolder mr-2" id="cetakSenaraiBtn">Cetak Warta</a></span><span>';
					}
					$userBtn .= $templateBtn.''.$suratBtn;
					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 Bayaran Pukal', $info_text, 0, $userBtn, 'ajax-modal2', 12);
					}
					
					createModal('khairat-modal');
				?>
				
			</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-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(),
		poskod_sc: $('#poskod_sc').val(),
		caramohon_sc: $('#caramohon_sc').val(),
		carabayar_sc: $('#carabayar_sc').val(),
		daftar_sc: $('#daftar_sc').val(),
		terima_sc: $('#terima_sc').val(),
		sah_sc: $('#sah_sc').val(),
		pegawai_sc: $('#pegawai_sc').val(),
		byrkepada_sc: $('#byrkepada_sc').val(),
		saheft_sc: $('#saheft_sc').val(),
		telahbayar_sc: $('#telahbayar_sc').val(),
		//tahun_sc: $('#tahun_sc_year').val(),
		tahun_sc: "<?=$kawalanyear?>",
		tahun_sah: $('#tahun_sah_year').val(),
		bayaranfasa_sc: $('#bayaranfasa_sc').val(),
		orgtua_sc: $('#orgtua_sc').val(),
		rmhorgtua_sc: $('#rmhorgtua_sc').val(),
		tkh_lbl: $('#tkh_lbl').val(),
	};
}

function reloadTable()
{
	//tahun_bayaran = $('#tahun_sc_year').val();
	tahun_bayaran = "<?=$kawalanyear?>";
	reinitTable($('#table_name').val(), this.ajaxUrl, this.getData(), 3, 0);
}

function cetakBaucer(file_name)
{
	var indicator_list = [[]];
	indicator_list.push(['daerah_sc', 'Daerah', 'dd']);
	indicator_list.push(['dun_sc', 'DUN', 'dd']);
	indicator_list.push(['tahun_sc_year', 'Tahun', 'dd']);
	indicator_list.push(['telahbayar_sc', 'Status Bayaran', 'dd']);
	
	if (checkInput(indicator_list))
	{
		if(file_name == 'template2.php')
		{
			var popup_param = 'program_sc=' + "<?=$id_program?>";
			popup_param += '&daerah_sc=' + $('#daerah_sc').val();
			popup_param += '&dun_sc=' + $('#dun_sc').val();
			//popup_param += '&tahun_sc_year=' + $('#tahun_sc_year').val();
			popup_param += '&tahun_sc_year=' + "<?=$kawalanyear?>";
			loadMyModal('general-pemohon_list_baucer_popup.php?' + popup_param, '#ajax-modal');
		}
		else
		{
			var dataOption = [];
			dataOption.program_sc = "<?=$id_program?>";
			dataOption.daerah_sc = $('#daerah_sc').val();
			dataOption.dun_sc = $('#dun_sc').val();
			//dataOption.tahun_sc_year = $('#tahun_sc_year').val();
			dataOption.tahun_sc_year = "<?=$kawalanyear?>";
			dataOption.file_name = file_name;
			console.log(dataOption);
			post('general-pemohon_list_' + file_name, dataOption);
		}
	
	}
}

function cetakSurat(file_name)
{
	var indicator_list = [[]];
	indicator_list.push(['daerah_sc', 'Daerah', 'dd']);
	indicator_list.push(['dun_sc', 'DUN', 'dd']);
	
	if (checkInput(indicator_list))
	{
		var dataOption = [];
		dataOption.program_sc = "<?=$id_program?>";
		dataOption.daerah_sc = $('#daerah_sc').val();
		dataOption.dun_sc = $('#dun_sc').val();
		dataOption.file_name = file_name;
		console.log(dataOption);
		post('general-pemohon_list_' + file_name, dataOption);
	}
}

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(){  
		
	if($('#orgtua_sc').val() < 2)
	{
		$('#rmhorgtua_sc_div').hide();
		$('#rmhorgtua_sc').val(0);
		$('#rmhorgtua_sc').trigger('change');
	}
	else
	{
		$('#rmhorgtua_sc_div').show();
	}
	
	$('#orgtua_sc').change(function() {
		if($('#orgtua_sc').val() == 2)
		{
			$('#rmhorgtua_sc_div').show();
		}
		else
		{
			$('#rmhorgtua_sc_div').hide();
			$('#rmhorgtua_sc').val(0);
			$('#rmhorgtua_sc').trigger('change');
		}
	});

	$('#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();
	$('#tahun_sc_year_div').hide();
	$('#bayaranfasa_sc_div').hide();
	$('#tkh_lbl_div').hide();
	$('#carabayar_sc_div').show();
	$('#daftar_sc_div').show();
	$('#terima_sc_div').show();
	$('#sah_sc_div').show();
	$('#pegawai_sc_div').show();
	$('#tahun_sah_div').show();
	$('#byrkepada_sc_div').hide();
	$('#saheft_sc_div').hide();
	
	$('#telahbayar_sc').change(function(){
		if($('#telahbayar_sc').val() == 1 || $('#telahbayar_sc').val() == 2)//belum bayar
		{
			$('#tkh_lbl_div').show();
			//$('#tahun_sc_div').show();
			$('#tahun_sc_year_div').show();
			$('#bayaranfasa_sc_div').hide();
			$('#carabayar_sc_div').hide();
			$('#carabayar_sc').val(0);
			$('#byrkepada_sc_div').hide();
			$('#byrkepada_sc').val(0);
			$('#saheft_sc_div').hide();
			$('#saheft_sc').val(0);
			$('#daftar_sc_div').hide();
			$('#daftar_sc').val(0);
			$('#terima_sc_div').hide();
			$('#terima_sc').val(0);
			$('#sah_sc_div').hide();
			$('#sah_sc').val(0);
			$('#pegawai_sc_div').hide();
			$('#pegawai_sc').val(0);
			$('#tahun_sah_div').hide();
			$('#tahun_sah_year').val(0);
		}
		else if($('#telahbayar_sc').val() == 4)//belum bayar oku
		{
			//$('#tahun_sc_div').show();
			$('#tahun_sc_year_div').show();
			$('#bayaranfasa_sc_div').hide();
			$('#carabayar_sc_div').hide();
			$('#carabayar_sc').val(0);
			$('#byrkepada_sc_div').hide();
			$('#byrkepada_sc').val(0);
			$('#saheft_sc_div').hide();
			$('#saheft_sc').val(0);
			$('#daftar_sc_div').hide();
			$('#daftar_sc').val(0);
			$('#terima_sc_div').hide();
			$('#terima_sc').val(0);
			$('#sah_sc_div').hide();
			$('#sah_sc').val(0);
			$('#pegawai_sc_div').hide();
			$('#pegawai_sc').val(0);
			$('#tahun_sah_div').hide();
			$('#tahun_sah_year').val(0);
		}
		else if($('#telahbayar_sc').val() == 3)//telah bayar
		{
			$('#tkh_lbl_div').hide();
			//$('#tahun_sc_div').show();
			$('#tahun_sc_year_div').show();
			$('#bayaranfasa_sc_div').show();
			$('#carabayar_sc_div').show();
			$('#tahun_sah_div').hide();
			$('#tahun_sah_year').val(0);
		}
		else
		{
			$('#tkh_lbl_div').hide();
			//$('#tahun_sc_div').hide();
			$('#tahun_sc_year_div').hide();
			$('#bayaranfasa_sc_div').hide();
			$('#carabayar_sc_div').show();
			$('#tahun_sah_div').show();
		}
	});

	if($('#carabayar_sc').val() == 2)
	{
		$('#byrkepada_sc_div').hide();
		$('#byrkepada_sc').val(0);
		$('#byrkepada_sc').trigger('change');
		$('#saheft_sc_div').show();
	}
	else if($('#carabayar_sc').val() == 1)
	{
		$('#byrkepada_sc_div').show();
		$('#saheft_sc_div').hide();
		$('#saheft_sc').val(0);
		$('#saheft_sc').trigger('change');
	}
	else
	{
		$('#byrkepada_sc_div').hide();
		$('#byrkepada_sc').val(0);
		$('#byrkepada_sc').trigger('change');
		$('#saheft_sc_div').hide();
		$('#saheft_sc').val(0);
		$('#saheft_sc').trigger('change');
	}
	
	$('#carabayar_sc').change(function() {
		if($('#carabayar_sc').val() == 2)
		{
			$('#byrkepada_sc_div').hide();
			$('#byrkepada_sc').val(0);
			$('#byrkepada_sc').trigger('change');
			$('#saheft_sc_div').show();
		}
		else if($('#carabayar_sc').val() == 1)
		{
			$('#byrkepada_sc_div').show();
			$('#saheft_sc_div').hide();
			$('#saheft_sc').val(0);
			$('#saheft_sc').trigger('change');
		}
		else
		{
			$('#byrkepada_sc_div').hide();
			$('#byrkepada_sc').val(0);
			$('#byrkepada_sc').trigger('change');
			$('#saheft_sc_div').hide();
			$('#saheft_sc').val(0);
			$('#saheft_sc').trigger('change');
		}
	});	

	if($('#byrkepada_sc').val() == 2)
	{
		$('#saheft_sc_div').show();
	}
	else
	{
		$('#saheft_sc_div').hide();
		$('#saheft_sc').val(0);
		$('#saheft_sc').trigger('change');
	}
	
	$('#byrkepada_sc').change(function() {
		if($('#byrkepada_sc').val() == 2)
		{
			$('#saheft_sc_div').show();
		}
		else
		{
			$('#saheft_sc_div').hide();
			$('#saheft_sc').val(0);
			$('#saheft_sc').trigger('change');
		}
	});

	/*if($('#saheft_sc').val() < 2)
	{
		$('#pegawai_sc_div').hide();
		$('#pegawai_sc').val(0);
		$('#pegawai_sc').trigger('change');
	}
	else
	{
		$('#pegawai_sc_div').show();
	}
	
	$('#saheft_sc').change(function() {
		if($('#saheft_sc').val() == 2)
		{
			$('#pegawai_sc_div').show();
		}
		else
		{
			$('#pegawai_sc_div').hide();
			$('#pegawai_sc').val(0);
			$('#pegawai_sc').trigger('change');
		}
	});*/
	
	initTable($('#table_name').val(), ajaxUrl, getData(), 3, 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,#daftar_sc,#terima_sc,#sah_sc,#pegawai_sc,#byrkepada_sc,#saheft_sc,#telahbayar_sc,#tahun_sc_year,#tahun_sah_year,#bayaranfasa_sc,#poskod_sc,#orgtua_sc,#rmhorgtua_sc,#tkh_lbl').change(function(){
	//$('#janaBtn').click(function(){
		reloadTable();
		//tahun_bayaran = $('#tahun_sc_year').val();
		tahun_bayaran = "<?=$kawalanyear?>";
		
		if(
			$('#daerah_sc').val() > 0 &&
			$('#dun_sc').val() > 0 &&
			//$('#carabayar_sc').val() == 1 && //tunai
			$('#telahbayar_sc').val() == 1 && //belum bayar
			//$('#tahun_sc_year').val() > 0
			tahun_bayaran > 0
		)
		{
			$('#cetakTunaiDiv').show();
		}
		else
		{
			$('#cetakTunaiDiv').hide();
		}
		
		if($('#carabayar_sc').val() == 1 &&	$('#byrkepada_sc').val() == 1)
		{
			$('#cetaksuratDiv').show();
		}
		else
		{
			$('#cetaksuratDiv').hide();
		}
		//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-pemohon_list_bayar.php?id=' + param + '&tahun=' + tahun_bayaran + /*'&fasa=' + fasa_bayaran + '&fasatext=' + encodeURI(fasa_text) +*/ '&program=' + "<?=$program_List['id_program']?>", '#ajax-modal');
		}
	});
	
	$('#cetakSenaraiBtn').click(function(){
		var dataOption = [];
		dataOption.program_sc = "<?=$id_program?>";
		dataOption.tkh_lbl = $('#tkh_lbl').val();
		post('general-pemohon_list_cetak_penerima.php', dataOption);
		
	});
});
</script>