<?php 
include_once ("classes/phpqrcode/qrlib.php");

if($id_program == 5)
{
	$table_to_fetch = 'm_anakemas';
	$id_to_fetch = 'id_anakemas';
	$nosiri_to_fetch = 'no_sirianakemas';
	$uploadfolder_to_fetch = 'upload_anakemas';
}

$indicator_Query = "SELECT A.*, B.nama AS nama_terima, C.desk_dun AS dun_terima, D.desk_daerah AS daerah_terima, E.nama AS nama_sah, F.desk_dun AS dun_sah, G.desk_daerah AS daerah_sah, H.nama AS nama_saheft, I.desk_dun AS dun_saheft, J.desk_daerah AS daerah_saheft
				  FROM {$table_to_fetch} A
				  LEFT JOIN t_pengguna B ON B.id_pengguna = A.id_terima
				  LEFT JOIN i_dun C ON C.id_dun = B.id_dun
				  LEFT JOIN i_daerah D ON D.id_daerah = B.id_daerah
				  LEFT JOIN t_pengguna E ON E.id_pengguna = A.id_sah
				  LEFT JOIN i_dun F ON F.id_dun = E.id_dun
				  LEFT JOIN i_daerah G ON G.id_daerah = E.id_daerah
				  LEFT JOIN t_pengguna H ON H.id_pengguna = A.id_saheft
				  LEFT JOIN i_dun I ON I.id_dun = H.id_dun
				  LEFT JOIN i_daerah J ON J.id_daerah = H.id_daerah
				  WHERE A.{$id_to_fetch} = ?";
$PDO_stmt = $PDO->prepare($indicator_Query);
$PDO_stmt->bindValue(1, $myID, PDO::PARAM_INT);
$PDO_stmt->execute();
$indicator_List[$table_to_fetch] = $PDO_stmt->fetch(PDO::FETCH_ASSOC);

$indicator_Query = "SELECT *
					FROM m_pemohon
					WHERE id_pemohon = ?";
$PDO_stmt = $PDO->prepare($indicator_Query);
$PDO_stmt->bindValue(1, $indicator_List[$table_to_fetch]['id_pemohon'], PDO::PARAM_INT);
$PDO_stmt->execute();
$indicator_List['m_pemohon'] = $PDO_stmt->fetch(PDO::FETCH_ASSOC);

$indicator_Query = "SELECT *
					FROM i_bank
					WHERE id_bank = ?";
$PDO_stmt = $PDO->prepare($indicator_Query);
$PDO_stmt->bindValue(1, $indicator_List[$table_to_fetch]['id_bankanakemas'], PDO::PARAM_INT);
$PDO_stmt->execute();
$indicator_List['i_bank'] = $PDO_stmt->fetch(PDO::FETCH_ASSOC);

$indicator_Query = "SELECT *
					FROM i_jantina
					WHERE id_jantina = ?";
$PDO_stmt = $PDO->prepare($indicator_Query);
$PDO_stmt->bindValue(1, $indicator_List[$table_to_fetch]['id_jantinaanakemas'], PDO::PARAM_INT);
$PDO_stmt->execute();
$indicator_List['i_jantina'] = $PDO_stmt->fetch(PDO::FETCH_ASSOC);

//get all borang info
$indicator_Query = "SELECT *
				  FROM u_borangdiv 
				  WHERE id_borangdiv NOT IN (1,2,9)
				  ORDER BY susunan";
$PDO_stmt = $PDO->prepare($indicator_Query);
$PDO_stmt->execute();
$borangDivList = [];
while($result = $PDO_stmt->fetch(PDO::FETCH_ASSOC))
{
	$borangDivList[$result['id_borangdiv']] = $result;
}

$indicator_Query = "SELECT B.*, A.is_check_input
				  FROM u_pautanborang A
				  LEFT JOIN u_borangitem B ON A.id_borangitem = B.id_borangitem
				  WHERE A.id_program = ?
				  AND B.linked_borangitem = 0
				  AND B.id_inputtype IN (1, 8, 20, 2, 3, 17, 11)
				  AND B.aktif = 1
				  ORDER BY B.susunan";
$PDO_stmt = $PDO->prepare($indicator_Query);
$PDO_stmt->bindValue(1, $id_program, PDO::PARAM_INT);
$PDO_stmt->execute();
$borangList = [];
while($result = $PDO_stmt->fetch(PDO::FETCH_ASSOC))
{
	$borangList[$result['id_borangdiv']][] = $result;
}

$borangTabMenu = [];
foreach($borangDivList as $div_key => $div_item)
{
	if(count($borangList[$div_key]) > 0)
	{
		$borangTabMenu[$div_key] = $div_item;
	}
}

$errorCorrectionLevel ='H';
$matrixPointSize = 2;
$url = $indicator_List['m_anakemas']['kpbaru_anakemas'];
$filename = 'qrcodes/'.$indicator_List['m_anakemas']['kpbaru_anakemas'].'.png';
QRcode::png($url, $filename, $errorCorrectionLevel, $matrixPointSize, 1);	

?>
<!--begin::Accordion-->
<div style="text-align:left;margin-bottom:5px;">
	<img src="<?=$filename?>" style="width:100px;height:100px">
	<?php
	if($indicator_List[$table_to_fetch]['id_statusmohon'] > 1)
	{
	?>	
		<a class="btn btn-success" id="printPaparBtn">Cetak Borang Permohonan</a>
		<a class="btn btn-success" id="printSlipBtn">Cetak Senarai Semak</a>
	<?php
	}
	
	if(($indicator_List[$table_to_fetch]['id_statusmohon'] == 2 || $indicator_List[$table_to_fetch]['id_statusmohon'] == 4) && $my_details->user_level <= 5)
	{
	?>		
		<a class="btn btn-success" href="p05_borang.php?type=1&pemohon=<?=$indicator_List[$table_to_fetch]['id_pemohon']?>" target="_blank">Kemaskini Borang Permohonan</a>
	<?php
	}
	?>
</div>
<div class="accordion accordion-toggle-arrow" id="accordionExample1">
	<?php
		$collapsed = 'collapsed';
		$show = '';
		foreach($borangTabMenu as $div_key => $div_item)
		{
			if($div_key == 5)
			{
				$collapsed = '';
				$show = 'show';
			}
			
			echo '<div class="card">';
				echo '<div class="card-header">';
					echo '<div class="card-title '.$collapsed.'" data-toggle="collapse" data-target="#collapseTarget_'.$div_key.'">';
					echo '<i class="flaticon2-layers-1"></i>'.$div_item['title'];
					echo '</div>';
				echo '</div>';
				echo '<div id="collapseTarget_'.$div_key.'" class="collapse '.$show.'" data-parent="#accordionExample1">';				
				
					$collapsed = 'collapsed';
					$show = '';
					
					echo '<div class="card-body">';
						echo '<table>';
						$isset_nosiri = 0;
							foreach($borangList[$div_item['id_borangdiv']] as $key => $item)
							{
								if($isset_nosiri == 0 && $div_key == 5)
								{
									$currentValue = $indicator_List[$table_to_fetch][$nosiri_to_fetch];
									echo '<tr>';
										echo '<td>No. Siri</td>';
										echo '<td style="width:10%">:</td>';
										echo '<td>'.(($currentValue=='')?'-':$currentValue).'</td>';
									echo '</tr>';
									$isset_nosiri = 1;
								}
								
								if($item['id_inputtype'] == 17) //dropzone
								{
									//get all dokumen from pemohon folder
									$path = 'upload_pemohon/'.$myID.'/*';
									$images = glob($path);
									for($num = 0; $num < count($images); $num++)
									{
										echo '<tr>';
										echo '<td>'.($num + 1).'.<td>';
										echo '<td style="width:5%"></td>';
										echo '<td><a href="'.$images[$num].'" target="_blank">'.end(explode('/', $images[$num])).'</a></td>';
										echo '</tr>';
									}
								}
								else if($item['id_inputtype'] == 11) //datatable
								{
									$indicator_Query = "SELECT *
													  FROM u_datatableitem 
													  WHERE id_borangitem = ".$item['id_borangitem'].' 
													  AND (type = 3 OR type = 4)
													  order by datatable_index';
									$PDO_stmt = $PDO->prepare($indicator_Query);
									$PDO_stmt->execute();
									$datatableItemList = [];
									while($result = $PDO_stmt->fetch(PDO::FETCH_ASSOC))
									{
										if($result['table_name'] != '' && $result['column_label'] != '')
										{
											$currentValue = $indicator_List[$result['table_name']][$result['column_name']];
											echo '<tr>';
												echo '<td>'.$result['column_label'].'</td>';
												echo '<td style="width:10%">:</td>';
												echo '<td>'.$indicator_List[$result['table_name']][$result['column_name']].'</td>';
											echo '</tr>';
											
											if($currentValue != '' && $result['column_name'] == 'tkh_lahiranakemas')
											{
												$createDate = new DateTime($currentValue);
												$todayDate = new DateTime(date('Y-m-d'));
												$diff = $todayDate->diff($createDate)->format("%y Tahun %m Bulan");
												echo '<tr>';
													echo '<td>Umur</td>';
													echo '<td style="width:10%">:</td>';
													echo '<td>'.$diff.'</td>';
												echo '</tr>';
											}
										}
									}
								}
								else
								{
									echo '<tr>';
										$currentValue = '-';
										switch ($item['id_inputtype']) 
										{
											case '1': //textbox
											case '8': //textbox uncopyable
											case '20': //date textbox
												$currentValue = $indicator_List[$div_item['main_table']][$item['value']];
											break;
											
											case '2': //dropdown
												$tableName = $item['extra_param'];
												$name = 'desk_';
												$identifier = explode("_", $tableName);
												$indicator_Query = "SELECT id_{$identifier[1]} AS id, concat({$name}{$identifier[1]}) as name
																	FROM {$tableName}
																	WHERE aktif = 1 AND id_{$identifier[1]} = ?
																	ORDER BY susunan";
												$PDO_stmt = $PDO->prepare($indicator_Query);
												$PDO_stmt->bindValue(1, $indicator_List[$div_item['main_table']][$item['value']], PDO::PARAM_INT);
												$PDO_stmt->execute();
												$ddResult = $PDO_stmt->fetch(PDO::FETCH_ASSOC);
												$currentValue = $ddResult['name'];
											break;
											
											case '3': //dropdown_sql
												$PDO_stmt = $PDO->prepare($item['extra_param']);
												$PDO_stmt->execute();
												while($result = $PDO_stmt->fetch(PDO::FETCH_ASSOC))
												{
													if($result['id'] == $indicator_List[$div_item['main_table']][$item['value']])
													{
														$currentValue = $result['name'];
														break;
													}
												}
											break;
										}
										echo '<td>'.$item['label'].'</td>';
										echo '<td style="width:10%">:</td>';
										echo '<td><b>'.(($currentValue=='')?'-':$currentValue).'</b></td>';
									echo '</tr>';
									
									if($currentValue != '' && $item['value'] == 'tkh_lahir')
									{
										$createDate = new DateTime($currentValue);
										$todayDate = new DateTime(date('Y-m-d'));
										$diff = $todayDate->diff($createDate)->format("%y");
										echo '<tr>';
											echo '<td>Umur</td>';
											echo '<td style="width:10%">:</td>';
											echo '<td><b>'.$diff.' Tahun</b></td>';
										echo '</tr>';
									}
								}
							}
						echo '</table>';
					echo '</div>';
				echo '</div>';
			echo '</div>';
		}
		
		echo '<div class="card">';
			echo '<div class="card-header">';
				echo '<div class="card-title '.$collapsed.'" data-toggle="collapse" data-target="#collapseTarget_dok">';
				echo '<i class="flaticon2-layers-1"></i>Senarai Lampiran Dokumen Yang Telah DiMuat Naik';
				echo '</div>';
			echo '</div>';
			echo '<div id="collapseTarget_dok" class="collapse '.$show.'" data-parent="#accordionExample1">';
				echo '<div class="card-body">';
					echo '<table>';
						//get all dokumen from anakemas folder
						$path = $uploadfolder_to_fetch.'/'.$myID.'/*';
						$images = glob($path);
						if(count($images) > 0)
						{
							for($num = 0; $num < count($images); $num++)
							{
								echo '<tr>';
									echo '<td>'.($num + 1).'.<td>';
									echo '<td style="width:5%"></td>';
									echo '<td><a href="'.$images[$num].'" target="_blank">'.end(explode('/', $images[$num])).'</a></td>';
								echo '</tr>';
							}
						}
						else
						{
							echo '<tr>';
								echo '<td>Tiada Dokumen<td>';
							echo '</tr>';
						}
					echo '</table>';
				echo '</div>';
			echo '</div>';
		echo '</div>';
	?>
	<div class="card">
		<div class="card-header" id="head1">
			<div class="card-title collapsed" data-toggle="collapse" data-target="#col3">
				
				<i class="flaticon2-layers-1"></i>Maklumat Pengesahan
			</div>
		</div>
		<div id="col3" class="collapse" data-parent="#accordionExample1">					
			<div class="row">
				<div class="col-md-12">
					<div class="card-body">
						<table>
							<tbody><br>
								<?php
									createtableLabels('Tarikh Daftar', '10', formatDate($indicator_List[$table_to_fetch]['tkh_daftar'],'d M Y'));
									if($indicator_List[$table_to_fetch]['id_terima'] > 0)
									{
										$dun_terima = '<br>KADUN : '.$indicator_List[$table_to_fetch]['dun_terima'];
										$daerah_terima = '<br>DAERAH : '.$indicator_List[$table_to_fetch]['daerah_terima'];
										
										if($indicator_List[$table_to_fetch]['dun_terima'] == '')
										{											
											$dun_terima = '';
										}
										
										if($indicator_List[$table_to_fetch]['daerah_terima'] == '')
										{											
											$daerah_terima = '';
										}
										
										createtableLabels('Tarikh Terima Dokumen', '10', formatDate($indicator_List[$table_to_fetch]['tkh_terima'],'d M Y'));
										createtableLabels('Pegawai Terima Dokumen', '10', $indicator_List[$table_to_fetch]['nama_terima'].$dun_terima.$daerah_terima);
									}
									if($indicator_List[$table_to_fetch]['id_sah'] > 0)
									{
										$dun_sah = '<br>KADUN : '.$indicator_List[$table_to_fetch]['dun_sah'];
										$daerah_sah = '<br>DAERAH : '.$indicator_List[$table_to_fetch]['daerah_sah'];
										
										if($indicator_List[$table_to_fetch]['dun_sah'] == '')
										{											
											$dun_sah = '';
										}
										
										if($indicator_List[$table_to_fetch]['daerah_sah'] == '')
										{											
											$daerah_sah = '';
										}
										createtableLabels('Tarikh Pengesahan', '10', formatDate($indicator_List[$table_to_fetch]['tkh_sah'],'d M Y'));
										createtableLabels('Pegawai Pengesahan', '10', $indicator_List[$table_to_fetch]['nama_sah'].$dun_sah.$daerah_sah);
									}
									if($indicator_List[$table_to_fetch]['id_saheft'] > 0)
									{
										$dun_saheft = '<br>KADUN : '.$indicator_List[$table_to_fetch]['dun_saheft'];
										$daerah_saheft = '<br>DAERAH : '.$indicator_List[$table_to_fetch]['daerah_saheft'];
										
										if($indicator_List[$table_to_fetch]['dun_saheft'] == '')
										{											
											$dun_saheft = '';
										}
										
										if($indicator_List[$table_to_fetch]['daerah_saheft'] == '')
										{											
											$daerah_saheft = '';
										}
										createtableLabels('Tarikh Pengesahan EFT', '10', formatDate($indicator_List[$table_to_fetch]['tkh_saheft'],'d M Y'));
										createtableLabels('Pegawai Pengesahan EFT', '10', $indicator_List[$table_to_fetch]['nama_saheft'].$dun_saheft.$daerah_saheft);
									}
								?>
							</tbody>
						</table>								
					</div>
				</div>
			</div>
		</div>
	</div>
	<?php
	if($indicator_List[$table_to_fetch]['flag_bayaran'] == 1)
	{
		$indicator_Query = "SELECT A.*, B.desk_bayaranjenis, C.desk_carabayar, D.desk_bank, E.desk_bayaranfasa
						  FROM m_bayaran_AE A
						  LEFT JOIN i_bayaranjenis B ON B.id_bayaranjenis = A.id_bayaranjenis
						  LEFT JOIN i_carabayar C ON C.id_carabayar = A.id_carabayar
						  LEFT JOIN i_bank D ON D.id_bank = A.id_bank
						  LEFT JOIN i_bayaranfasa E ON E.id_bayaranfasa = A.id_bayaranfasa
						  WHERE A.id_anakemas = ?";
		$PDO_stmt = $PDO->prepare($indicator_Query);
		$PDO_stmt->bindValue(1, $myID, PDO::PARAM_INT);
		$PDO_stmt->execute();
		$bayaran_List = $PDO_stmt->fetch(PDO::FETCH_ASSOC);
		
	?>
		<div class="card">
			<div class="card-header" id="head2">
				<div class="card-title collapsed" data-toggle="collapse" data-target="#col4">					
					<i class="flaticon2-layers-1"></i>Maklumat Pembayaran
				</div>
			</div>
			<div id="col4" class="collapse" data-parent="#accordionExample1">					
				<div class="row">
					<div class="col-md-12">
						<div class="card-body">
							<table>
								<tbody><br>
								<?php

									createtableLabels('Jumlah Bayaran', '10', 'RM'.$bayaran_List['jum_bayaran']);
									createtableLabels('Jenis Bayaran', '10', $bayaran_List['desk_bayaranjenis']);
									createtableLabels('Fasa Bayaran', '10', $bayaran_List['desk_bayaranfasa']);
									createtableLabels('Tahun Bayaran', '10', 'Tahun '.$bayaran_List['tahun_bayar']);
									createtableLabels('Tarikh Bayaran', '10', formatDate($bayaran_List['tkh_bayar'],'d M Y'));
									createtableLabels('Cara Bayaran', '10', $bayaran_List['desk_carabayar']);
									createtableLabels('Bank', '10', $bayaran_List['desk_bank']);
									createtableLabels('No. Akaun', '10', $bayaran_List['no_akaun']);
									createtableLabels('Catatan', '10', $bayaran_List['catatan']);
			
								?>	
								</tbody>
							</table><br><br>
							<?php
							if($my_details->user_level <= 5)
							{							
								echo '<a class="btn btn-success btn-sm mr-2" onclick="loadMyModal(\'general-byr_ae.php?id='.$bayaran_List['id_anakemas'].'\', \'#ajax-modal\')" title="Kemaskini Maklumat Pembayaran"><i class="flaticon2-poll-symbol"></i>Kemaskini Maklumat Pembayaran</a>';
							}
							?>			
						</div>
					</div>					
				</div>
			</div>
		</div>
	<?php
	}
	?>
</div>
<!--end::Accordion-->
<script>
//print function
$('#printPaparBtn').click(function() {
	//alert('printBtn');
	var dataOption = [];
	dataOption.id = "<?=$myID?>";
	dataOption.program = "<?=$id_program?>";
	post("general-print_borang_sub.php", dataOption);
});
</script>
<script>
//print function
$('#printSlipBtn').click(function() {
	//alert('printBtn');
	var dataOption = [];
	dataOption.id = "<?=$myID?>";
	dataOption.program = "<?=$id_program?>";
	post("general-print_slip_sub.php", dataOption);
});
</script>
<script>
"use strict";
var KTDatatablesBasicScrollable = function() {

    var initTable2 = function() {
        var table = $('#kt_datatable2');

        // begin second table
        table.DataTable({
            scrollY: '50vh',
            scrollX: true,
            scrollCollapse: true,
        });
    };

    return {

        //main function to initiate the module
        init: function() {
            initTable2();
        },

    };

}();

jQuery(document).ready(function() {
    KTDatatablesBasicScrollable.init();
});
</script>