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

$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 m_pemohon 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 id_pemohon = ? AND id_program = ?";
$PDO_stmt = $PDO->prepare($indicator_Query);
$PDO_stmt->bindValue(1, $myID, PDO::PARAM_INT);
$PDO_stmt->bindValue(2, $id_program, PDO::PARAM_INT);
$PDO_stmt->execute();
$indicator_List['m_pemohon'] = $PDO_stmt->fetch(PDO::FETCH_ASSOC);

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

//get all dokumen
$docQuery = "SELECT id_dokumen, desk_dokumen
			  FROM i_dokumen 
			  WHERE aktif = 1
			  ORDER BY susunan";
$doc_Result = $PDO->prepare($docQuery);
$doc_Result->execute();
// put all into hash for easier comparison
$docPages = array();
while($res = $doc_Result->fetch(PDO::FETCH_ASSOC)) {
	$docPages[] = $res;
}

//get all borang info
$indicator_Query = "SELECT *
				  FROM u_borangdiv 
				  WHERE id_borangdiv NOT IN (1,2,5,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;
}

if($id_program == 0)
{
	$id_program = 5;	
}

$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_pemohon']['kpbaru_pemohon'];
$filename = 'qrcodes/'.$indicator_List['m_pemohon']['kpbaru_pemohon'].'.png';
QRcode::png($url, $filename, $errorCorrectionLevel, $matrixPointSize, 1);	
?>
<!--begin::Accordion-->
<div style="text-align:left;margin-bottom:5px">
	<?php
	if($my_details->user_level != 10 && $indicator_List['m_pemohon']['id_statusmohon'] >= 2)
	{
	?>
		<img src="<?=$filename?>" style="width:100px;height:100px">
		<?php
		if($indicator_List['m_pemohon']['id_statusmohon'] > 1 && $indicator_List['m_pemohon']['id_statusmohon'] < 4)
		{
		?>
			<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['m_pemohon']['id_carabayar'] == 2 || $indicator_List['m_pemohon']['id_byrkepada'] == 2) && $indicator_List['m_pemohon']['flag_saheft'] == 0 && $indicator_List['m_pemohon']['id_program'] != 4)
		{
		?>
			<a class="btn btn-success" id="printEftBtn">Cetak Pengesahan EFT</a>
		<?php
		}
		
		if(($indicator_List['m_pemohon']['id_aktifmohon'] == 1 || $indicator_List['m_pemohon']['id_aktifmohon'] == 9) && (in_array($my_details->user_level, array(1,2,3,4,6,8,15)) || (in_array($my_details->user_level, array(5,7,16,9)) && $indicator_List['m_pemohon']['id_statusmohon'] == 4)))
		{
			if(in_array($my_details->user_level, array(1,3)) || (in_array($my_details->user_level, array(8,9)) && $indicator_List['m_pemohon']['id_program'] == 4) || (in_array($my_details->user_level, array(2,4,6,15,5,7,16)) && $indicator_List['m_pemohon']['id_program'] != 4))
			{
			?>
			<a class="btn btn-success" href="p<?php echo $name_id_program;?>_borang.php?pemohon=<?=$myID?>" target="_blank">Kemaskini Borang Permohonan</a>
			<?php
			}
			
			if(in_array($my_details->user_level, array(1,2,3,4,5)) && $indicator_List['m_pemohon']['id_statusmohon'] == 4)
			{
			?>
			<a class="btn btn-success" href="p<?php echo $name_id_program;?>_borang.php?update_bank=1&pemohon=<?=$myID?>" target="_blank">Kemaskini EFT Pemohon</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 == 3)
			{
				$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">';
					echo '<div class="card-body">';
						echo '<table><br>';
						$isset_nosiri = 0;
							foreach($borangList[$div_item['id_borangdiv']] as $key => $item)
							{
								if($isset_nosiri == 0 && $div_key == 3 && $id_program != 5)
								{
									$currentValue = $indicator_List[$div_item['main_table']]['no_siri'];
									echo '<tr>';
										echo '<td>No. Siri</td>';
										echo '<td style="width:10%">:</td>';
										echo '<td><b>'.(($currentValue=='')?'-':$currentValue).'</b></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);
									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>';
									}
								}
								else if($item['id_inputtype'] == 11) //datatable
								{
									$indicator_Query = "SELECT *
													  FROM u_datatableitem 
													  WHERE id_borangitem = ".$item['id_borangitem'].' order by datatable_index';
									$PDO_stmt = $PDO->prepare($indicator_Query);
									$PDO_stmt->execute();
									$datatableItemList = [];
									while($result = $PDO_stmt->fetch(PDO::FETCH_ASSOC))
									{
										$datatableItemList[$result['type']][] = $result;
									}
									
									$indicator_Query = "SELECT *
														FROM {$datatableItemList[1][0]['table_name']} 
														WHERE id_pemohon = {$myID}";
									$PDO_stmt = $PDO->prepare($indicator_Query);
									$PDO_stmt->execute();
									$datatableResultList = [];
									$count = 1;
									echo '<table class="table table-separate table-head-custom table-checkable" id="kt_datatable2">';
									echo '<tr>';
										echo '<th>Bil</th>';
										foreach($datatableItemList[3] as $key=>$item)
										{
											echo '<th>'.$item['column_label'].'</th>';
										}
									echo '</tr>';
									while($result = $PDO_stmt->fetch(PDO::FETCH_ASSOC))
									{
										echo '<tr>';
											echo '<td>'.($count++).'</td>';
											foreach($datatableItemList[3] as $key=>$item)
											{
												echo '<td>'.$result[$item['column_name']].'</td>';
											}
										echo '</tr>';
									}
									echo '</table>';
								}
								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')
									{
										$icno = $indicator_List['m_pemohon']['kpbaru_pemohon'];
										$year = substr($icno , 0, 2);
										$year = (($year < date('y'))?('20'.$year):('19'.$year));
										$month = substr($icno , 2, 2);
										$day = substr($icno , 4, 2);							
										
										//$age = dateDifference(date('Y-m-d') , $year.'-'.$month.'-'.$day, '%y' );
										$age = date('Y') - $year;
				
										$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 '<td><b>'.$age.' Tahun</b></td>';
										echo '</tr>';
									}
								}
							}
						echo '</table>';
					echo '</div>';
				echo '</div>';
			echo '</div>';
		}
	if($indicator_List['m_pemohon']['id_program'] != 0)
	{
		?>
		<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['m_pemohon']['tkh_daftar'],'d M Y'));
										
										$tkh_terima = $indicator_List['m_pemohon']['tkh_terima'];
										if($tkh_terima == '0000-00-00 00:00:00' && $indicator_List['m_pemohon']['tkh_sah'] == '0000-00-00 00:00:00')
										{
											$tkh_terima = 'Dokumen Belum Diterima';										
										}
										else if($tkh_terima == '0000-00-00 00:00:00' && $indicator_List['m_pemohon']['tkh_sah'] != '0000-00-00 00:00:00')
										{
											$tkh_terima = formatDate($indicator_List['m_pemohon']['tkh_daftar'],'d M Y');										
										}
										else
										{
											$tkh_terima = formatDate($indicator_List['m_pemohon']['tkh_terima'],'d M Y');										
										}
										
										createtableLabels('Tarikh Terima Dokumen', '10', $tkh_terima);
										
										if($indicator_List['m_pemohon']['id_terima'] > 0)
										{
											$dun_terima = '<br>KADUN : '.$indicator_List['m_pemohon']['dun_terima'];
											$daerah_terima = '<br>DAERAH : '.$indicator_List['m_pemohon']['daerah_terima'];
											
											if($indicator_List['m_pemohon']['dun_terima'] == '')
											{											
												$dun_terima = '';
											}
											
											if($indicator_List['m_pemohon']['daerah_terima'] == '')
											{											
												$daerah_terima = '';
											}										
											createtableLabels('Pegawai Terima Dokumen', '10', $indicator_List['m_pemohon']['nama_terima'].$dun_terima.$daerah_terima);
										}
										if($indicator_List['m_pemohon']['id_sah'] > 0)
										{
											$dun_sah = '<br>KADUN : '.$indicator_List['m_pemohon']['dun_sah'];
											$daerah_sah = '<br>DAERAH : '.$indicator_List['m_pemohon']['daerah_sah'];
											
											if($indicator_List['m_pemohon']['dun_sah'] == '')
											{											
												$dun_sah = '';
											}
											
											if($indicator_List['m_pemohon']['daerah_sah'] == '')
											{											
												$daerah_sah = '';
											}
											createtableLabels('Tarikh Pengesahan', '10', formatDate($indicator_List['m_pemohon']['tkh_sah'],'d M Y'));
											createtableLabels('Pegawai Pengesahan', '10', $indicator_List['m_pemohon']['nama_sah'].$dun_sah.$daerah_sah);
										}
										if($indicator_List['m_pemohon']['flag_saheft'] > 0)
										{
											$dun_saheft = '<br>KADUN : '.$indicator_List['m_pemohon']['dun_saheft'];
											$daerah_saheft = '<br>DAERAH : '.$indicator_List['m_pemohon']['daerah_saheft'];
											
											if($indicator_List['m_pemohon']['dun_saheft'] == '')
											{											
												$dun_saheft = '';
											}
											
											if($indicator_List['m_pemohon']['daerah_saheft'] == '')
											{											
												$daerah_saheft = '';
											}
											createtableLabels('Tarikh Pengesahan EFT', '10', formatDate($indicator_List['m_pemohon']['tkh_saheft'],'d M Y'));
											createtableLabels('Pegawai Pengesahan EFT', '10', $indicator_List['m_pemohon']['nama_saheft'].$dun_saheft.$daerah_saheft);
										}
									?>
								</tbody>
							</table>								
						</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.php", dataOption);
});
</script>
<script>
//print function
$('#printSlipBtn').click(function() {
	//alert('printBtn');
	var dataOption = [];
	dataOption.id = "<?=$myID?>";
	dataOption.program = "<?=$id_program?>";
	post("general-print_slip.php", dataOption);
});
</script>
<script>
$('#printEftBtn').click(function() {
	//alert('printBtn');
	var dataOption = [];
	dataOption.id = "<?=$myID?>";
	dataOption.program = "<?=$id_program?>";
	post("general-print_eft.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>