<?php 

if($id_pemohon > 0)
{
	if($type == 1) // no need care program if type == 1
	{
		$indicator_Query = "SELECT A.*
				  FROM m_pemohon A
				  WHERE id_pemohon = ?";
		$PDO_stmt = $PDO->prepare($indicator_Query);
		$PDO_stmt->bindValue(1, $id_pemohon, PDO::PARAM_INT);
	}
	else
	{
		$indicator_Query = "SELECT A.*
						  FROM m_pemohon A
						  WHERE id_pemohon = ? AND id_program = ?";
		$PDO_stmt = $PDO->prepare($indicator_Query);
		$PDO_stmt->bindValue(1, $id_pemohon, 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);
	
	$idstatusmohon = $indicator_List['m_pemohon']['id_statusmohon'];
}

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

if($id_pemohon > 0) //if is existing borang, no need show these div
{
	$exclude_borangdiv = '(1,2,9)';
	if($need_akuan)
	{
		$exclude_borangdiv = '(1,2)';
	}
	else if($update_bank == 1)
	{
		$exclude_borangdiv = '(1,2,3,4,5,6,9,10,11)';
	}
	$indicator_Query = "SELECT *
					  FROM u_borangdiv 
					  WHERE id_borangdiv NOT IN {$exclude_borangdiv}
					  ORDER BY susunan";

}
else
{
	$indicator_Query = "SELECT *
					  FROM u_borangdiv 
					  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.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(isset($borangList[$div_key]) && count($borangList[$div_key]) > 0)
	{
		$borangTabMenu[$div_key] = $div_item;
	}
}
/*
$indicator_Query = "SELECT B.*
				  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
				  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;
}
*/
if($id_program == 1)
{
	$kawalan = 1;
}
else if($id_program == 2)
{
	$kawalan = 2;
}
else if($id_program == 3)
{
	$kawalan = 3;
}
else if($id_program == 4)
{
	$kawalan = 4;
}
else if($id_program == 7)
{
	$kawalan = 5;
}

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

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

$id_pemohonterima = $paymentList['l_penerima']['id_pemohon'];
$thn_pemohonterima = $paymentList['l_penerima']['tahun_bayar'];

//if($id_pemohonterima == 0 && $indicator_List['m_pemohon']['id_statusmohon'] == 4 && $dateTimeNow < $kawalan_List['tkh_buka'])
if($indicator_List['m_pemohon']['id_statusmohon'] == 4 && $dateTimeNow < $kawalan_List['tkh_buka'])
{	
	echo '<br><h4><p style="color:red;margin-left:20px;margin-right:10px;">&nbsp&nbsp*Pengemaskinian KADUN & Maklumat EFT yang telah disahkan sedang ditutup buat masa ini dari '.formatDate($kawalan_List['tkh_tutup'],'d M Y').' sehingga '.formatDate($kawalan_List['tkh_buka'],'d M Y').'</p></h4>';
}
?>	

<!--begin::Wizard-->
<div class="wizard wizard-1" id="kt_wizard" data-wizard-state="step-first" data-wizard-clickable="false">
	<!--begin::Wizard Nav-->
	<div class="wizard-nav border-bottom">
		<div class="wizard-steps p-8 p-lg-10">
			<?php
				$counter = 1;
				foreach($borangTabMenu as $div_key => $div_item)
				{
					echo '<div class="wizard-step" data-wizard-type="step">';
						echo '<div class="wizard-label">';
							echo '<i class="wizard-icon '.$div_item['title_icon'].'"></i>';
							echo '<h3 class="wizard-title">'.$counter.'. '.$div_item['title'].'</h3>';
						echo '</div>';
						if($counter < count($borangTabMenu))
						{
							echo $Wizard3_svg;		
						}											
					echo '</div>';
				
					$counter++;
					
				}
			?>
		</div>
	</div>
	<!--end::Wizard Nav-->
	<!--begin::Wizard Body-->
	<div class="row justify-content-center my-10 px-8 my-lg-15 px-lg-10">
		<div class="col-xl-12 col-xxl-12">
			<input type="hidden" id="id_borang_program" value="<?=$id_program?>">
			<input type="hidden" id="id_pemohon_borang" value="<?=$id_pemohon?>">
			<input type="hidden" id="formReadOnly" value="<?=$readonly?>">
			<!--begin::Wizard Form-->
			<!--<form class="form" id="kt_form">-->
				<?php
					$counter = 0;
					$actionJS = [];
					$datatableJS = [];
					$dropzoneJS = [];
					foreach($borangTabMenu as $div_key => $div_item)
					{
						//START special checking for bank div
						$orireadonly = $readonly;
						if($div_key == 7 && $my_details->user_level == 10 && in_array($indicator_List[$div_item['main_table']]['id_statusmohon'], array(3)))
						{
							$readonly = 1;
						}
						else if($div_key == 7 && $indicator_List[$div_item['main_table']]['id_statusmohon'] == 4 && $indicator_List[$div_item['main_table']]['id_carabayar'] == 2 && $indicator_List[$div_item['main_table']]['flag_saheft'] == 1 && $id_pemohonterima == 0)
						//else if($div_key == 7 && $indicator_List[$div_item['main_table']]['id_statusmohon'] == 4)
						{
							//if($dateTimeNow < $kawalan_List['tkh_buka']) 
							if($dateTimeNow < $kawalan_List['tkh_buka'] && in_array($my_details->user_level, array(4,5,6,7,10,11,12,13,14,15,16,17,18))) 
							{
								$readonly = 1;
							}
							else
							{
								$readonly = $orireadonly;
							}
						}
						else
						{
							$readonly = $orireadonly;
						}
						//END special checking for bank div
						
						if(($div_key == 4 || $div_key == 8 || $div_key == 9) && in_array($idstatusmohon, array(1,2,4)))
						{
							$readonly = 0;
						}
						
						$actionArray = [];
						$validator = [];
						echo '<div class="pb-5" data-wizard-type="step-content" '.($counter==0?'data-wizard-state="current"':'').'>';
							echo '<h3 class="mb-10 font-weight-bold text-dark">'.$div_item['title'].'</h3>';
							
							if($div_item['id_borangdiv'] == 9 && $my_details->user_level == 10)
							{
								echo '<div class="accordion accordion-toggle-arrow" id="accordionExample4">
									<div class="card">
										<div class="card-header" id="headingOne4">
											<div class="card-title" data-toggle="collapse" data-target="#collapseOne4">
											<i class="flaticon2-layers-1"></i>Maklumat Pemohon</div>
										</div>
										<div id="collapseOne4" class="collapse show" data-parent="#accordionExample4">
											<div class="card-body">';
										
											echo '<table>';
												echo '<tr>';
													echo '<td>Nama</td>';
													echo '<td style="width:10%">:</td>';
													echo '<td><b>'.$indicator_List['m_pemohon']['nama_pemohon'].'</b></td>';
												echo '</tr>';
												echo '<tr>';
													echo '<td>No Kad Pengenalan</td>';
													echo '<td style="width:10%">:</td>';
													echo '<td><b>'.$indicator_List['m_pemohon']['kpbaru_pemohon'].'</b></td>';
												echo '</tr>';
											echo '</table>';
											
											echo '</div>
										</div>
									</div><br>
								</div>';
							}						
							
							echo '<input type="hidden" id="divKey_'.$counter.'" value=\''.$div_key.'\'>';
							echo '<input type="hidden" id="isInfoPulled_'.$counter.'" value=\'0\'>';
							//echo '<input type="hidden" id="divPemohonId_'.$counter.'" value=\''.$id_pemohon.'\'>';
							//echo '<input type="hidden" id="mainDivIdTable_'.$counter.'" value=\''.$div_item['main_table'].'\'>';
							//echo '<input type="hidden" id="mainDivIdKey_'.$counter.'" value=\''.$div_item['main_table_pk'].'\'>';
							echo '<input type="hidden" id="mainDivId_'.$counter.'" value=\''.$indicator_List[$div_item['main_table']][$div_item['main_table_pk']].'\'>';
							echo '<form class="form" id="borangForm_'.$counter.'">';
							foreach($borangList[$div_item['id_borangdiv']] as $key => $item)
							{
								switch ($item['id_inputtype']) 
								{
									case '1': //textbox
										if((in_array($item['id_borangitem'], array(5)) && $indicator_List[$div_item['main_table']]['nama_pemohon'] != '') || (in_array($item['id_borangitem'], array(31,32)) && ($my_details->user_level > 5 && $my_details->usr_id != 439) && $indicator_List[$div_item['main_table']]['kpbaru_waris'] != '' && $idstatusmohon >= 3))
										{
											$orireadonly_name = 1;
											createTextBox($item['label'], $item['input_name'], $item['placeholder']!=''?$item['placeholder']:$item['label'], $indicator_List[$div_item['main_table']][$item['value']], $item['label_size'], $orireadonly_name, $item['input_size'], $item['div_size'], $item['extra_param'], $item['extra_param'], $item['help_text'], '', $item['maxlength']);											
										}
										else
										{
											createTextBox($item['label'], $item['input_name'], $item['placeholder']!=''?$item['placeholder']:$item['label'], $indicator_List[$div_item['main_table']][$item['value']], $item['label_size'], $readonly, $item['input_size'], $item['div_size'], $item['extra_param'], $item['extra_param'], $item['help_text'], '', $item['maxlength']);
										}
										break;
									case '2': //dropdown									
							
										$indicator_Query = "SELECT A.id_program
														  FROM m_pemohon A
														  WHERE A.id_pemohon = ?";
										$PDO_stmt = $PDO->prepare($indicator_Query);
										$PDO_stmt->bindValue(1, $indicator_List[$div_item['main_table']]['id_pemohon'], PDO::PARAM_INT);
										$PDO_stmt->execute();
										$oriprog_List = $PDO_stmt->fetch(PDO::FETCH_ASSOC);
										
										if($item['id_borangitem'] == 15 && in_array($id_program, array(1,2,3,4,7)) && $idstatusmohon >= 3 && $id_pemohonterima == 0)
										{
											$orireadonly = $readonly;
											
											if($dateTimeNow < $kawalan_List['tkh_buka']) 
											{
												$orireadonly = 1;
											}											
									
											createDropDown($PDO, $item['label'], $item['extra_param'], $item['input_name'], $indicator_List[$div_item['main_table']][$item['value']], $item['placeholder'], $item['input_size'], $item['label_size'], $item['div_size'], $orireadonly,'desk_', $item['help_text']);
										}
										else
										{									
											createDropDown($PDO, $item['label'], $item['extra_param'], $item['input_name'], $indicator_List[$div_item['main_table']][$item['value']], $item['placeholder'], $item['input_size'], $item['label_size'], $item['div_size'], $readonly,'desk_', $item['help_text']);
										}
										break;
									case '3': //dropdown_sql
										createDropDown_sql($PDO, $item['label'], $item['extra_param'], $item['input_name'], $indicator_List[$div_item['main_table']][$item['value']], $item['placeholder'], $item['input_size'], $item['label_size'], $item['div_size'], $readonly, $item['help_text']);
										break;
									case '5': //custom
										echo $item['extra_param'];
										break;
									case '6': //action pass
										$actionArray['passAction_'.$counter][] = $item['value'];
										break;
									case '7': //action insert
										//script_url<BRE>borang_div<BRE>table_name<BRE>id_table<BRE>id_insert<BRE>id_update
										$action_value = 'general-borang_insert.php'.'<BRE>'.$div_key.'<BRE>'.$item['value'];
										$allInfo = explode('<BRE>', $item['value']);
										echo '<input type="hidden" id="currentIdName_'.$counter.'" value=\''.$allInfo[1].'\'>';
										echo '<input type="hidden" id="currentIdValue_'.$counter.'" value=\''.$indicator_List[$allInfo[0]][$allInfo[1]].'\'>';
										echo '<input type="hidden" id="saveAction_'.$counter.'" value=\''.$action_value.'\'>';
										echo '<input type="hidden" id="active" value=\''.$active.'\'>';
										break;
									case '8': //textbox uncopyable
										createTextBox($item['label'], $item['input_name'], $item['placeholder']!=''?$item['placeholder']:$item['label'], $indicator_List[$div_item['main_table']][$item['value']], $item['label_size'], $readonly, $item['input_size'], $item['div_size'], $item['extra_param'], 'text', '', ' oncopy="return false" onpaste="return false" ');
										break;
									case '9': //action check equal
										$actionArray['equalAction_'.$counter][] = $item['value'];
										break;
									case '10': //action hide if match value
										//borang_carabayar<HIDE>1<HIDE>borang_no_akaun
										$jsItem = explode('<HIDE>', $item['value']);
										$actionJS[$jsItem[0]]['value'] = $jsItem[1];
										$actionJS[$jsItem[0]]['item'][] = $jsItem[2];
										break;
									case '11': //datatable
										//get column here
										$tbColumn = array();
										$tbColumn[] = array('Bil', 15);
										$indicator_Query = "SELECT *
														  FROM u_datatableitem 
														  WHERE id_borangitem = ? AND type = 3
														  ORDER BY datatable_index";
										$PDO_stmt = $PDO->prepare($indicator_Query);
										$PDO_stmt->bindValue(1, $item['id_borangitem'], PDO::PARAM_STR);
										$PDO_stmt->execute();
										while($result = $PDO_stmt->fetch(PDO::FETCH_ASSOC))
										{
											if($result['column_label'] != '')
											{
												$tbColumn[] = array($result['column_label'], 10);
											}
										}
										
										//some hardcode value replacement here, too bad
										$btn_param = 'id=0';
										$btn_param .= '&program='.$id_program;
										$btn_param .= '&program='.$id_program;
										$btn_param .= '&borangitem='.$item['id_borangitem'];
										$btn_param .= '&reloadBtn='.$item['input_name'].'_reloadbtn';
										
										$popup_page = 'general-borang_tambah.php';
										if($id_program == 5) //special case for anak emas program
										{
											$popup_page = 'general-borang_tambah_anakemas.php';
										}
										$item['extra_param'] = str_replace('<tambahForm>', $popup_page.'?program='.$id_program.'&pemohon='.$indicator_List['m_pemohon']['id_pemohon'].'&id=0&borangitem='.$item['id_borangitem'].'&reloadBtn='.$item['input_name'].'_reloadbtn', $item['extra_param']);
										$item['extra_param'] = str_replace('<tambahBtnSvg>', $tambahBtnSvg, $item['extra_param']);
										$item['extra_param'] = str_replace('<dtModal>', $item['input_name'].'-modal', $item['extra_param']);
										
										createTable($item['input_name'], $tbColumn, $item['label'], $item['placeholder'], 0, $item['extra_param'], $item['input_name'].'-modal', $item['div_size']);
										
										//create a hidden button here to refresh
										echo '<button type="button" id="'.$item['input_name'].'_reloadbtn" onclick="reloadTable_'.$item['id_borangitem'].'()" style="display:none" ></button>';
										$datatableJS[] = $item;
										
										break;
									case '12': //hidden input
										echo '<input type="hidden" id="'.$item['input_name'].'" value=\''.$indicator_List[$div_item['main_table']][$item['value']].'\'>';
										break;
									case '13': //action pass
										$actionArray['updateIdAction_'.$counter][] = $item['value'];
										break;
									case '14': //action tarik info
										$actionArray['pullInfoAction_'.$counter][] = $item['id_borangitem'].'<TR>'.$item['value'];
										break;
									case '15': //action blok if wujud
										$actionArray['blockAction_'.$counter][] = $item['id_borangitem'].'<TR>1<TR>'.$item['value'];
										break;
									case '16': //action blok if wujud
										$actionArray['blockAction_'.$counter][] = $item['id_borangitem'].'<TR>2<TR>'.$item['value'];
										break;
									case '17': //dropzone
										//get all i_dokumenprogram
										$dok_Query = "SELECT i_dokumen.id_dokumen, i_dokumenprogram.flag_wajib, i_dokumen.desk_dokumen
														FROM i_dokumenprogram 
														LEFT JOIN i_dokumen ON i_dokumenprogram.id_dokumen = i_dokumen.id_dokumen
														WHERE id_program = $id_program";
										$dok_Result = $PDO->prepare($dok_Query);
										$dok_Result->execute();
										// put all into hash for easier comparison
										$dokList = array();
										$dokWajibList = array();
										while($res = $dok_Result->fetch(PDO::FETCH_ASSOC)) {
											if($res['flag_wajib'] == 1)
											{
												$dokWajibList[]= $res;
											}
											else
											{
												$dokList[]= $res;
											}
										}
										echo '<h3>Senarai Dokumen Yang Diperlukan</h3>';
										//echo '<div class="checkbox-list"><br>';
										$doc_counter = 1;
											foreach($dokWajibList as $wkey => $witem) 
											{
												//echo '<label class="checkbox '.($readonly?'checkbox-disabled':'').'">';
													//echo '<input class="wajibClass" type="checkbox" '.($readonly?'disabled="disabled"':'').' id="doc_'.$witem['id_dokumen'].'" name="doc_'.$witem['id_dokumen'].'">';
													//echo '<span></span>';
													//echo $doc_counter++.'. '.$witem['desk_dokumen'].'<a title="Dokumen Wajib">'.$Wajib_svg.'</a><br>';
													echo $doc_counter++.'. '.$witem['desk_dokumen'].' <b>(DOKUMEN WAJIB)</b><br>';
												//echo '</label>';
												$checkDocList .= $witem['id_dokumen'].',';
											}
											foreach($dokList as $wkey => $witem) 
											{
												//echo '<label class="checkbox '.($readonly?'checkbox-disabled':'').'">';
													//echo '<input class="normalClass" type="checkbox" '.($readonly?'disabled="disabled"':'').' id="doc_'.$witem['id_dokumen'].'" name="doc_'.$witem['id_dokumen'].'">';
													//echo '<span></span>';
													echo $doc_counter++.'. '.$witem['desk_dokumen'].'<br>';
												//echo '</label>';
											}
											echo '<hr>';
										//echo '</div>';
										echo '<div class="form-group row">
												<div class="col-md-'.$item['div_size'].'">
													<input type="hidden" id="file_count_'.$item['input_name'].'" value="0">
													<div class="dropzone dropzone-default" id="'.$item['input_name'].'">
														<div class="dropzone-msg dz-message needsclick">
															<h3 class="dropzone-msg-title">'.$item['label'].'</h3>
															<span class="dropzone-msg-desc">'.$item['placeholder'].'</span>
														</div>
													</div>
													*Klik pada ruangan diatas untuk muatnaik dokumen (hanya format gambar/pdf sahaja)
												</div>
											</div>';
										if(count($dropzoneJS) == 0)//if not exist yet
										{
											createModal('large_photo-modal');
										}
										$dropzoneJS[] = $item;
										break;
									case '18': //checkbox
										echo '<label class="checkbox">';
											echo '<input type="checkbox" id="'.$item['input_name'].'" '.$item['extra_param'].' >';
											echo '<span></span>';
											echo $item['label'];
										echo '</label>';
										break;
									case '20': //date textbox
										createDateTB($item['label'], $item['input_name'], $item['placeholder']!=''?$item['placeholder']:$item['label'], $indicator_List[$div_item['main_table']][$item['value']], $item['label_size'], $readonly, $item['input_size'], $item['div_size']);
										break;
								  default:
									echo '';
								}
								
								//build a json that will be use for validation here
								if($item['checking_type'] != '' && $item['is_check_input'] == 1)
								{
									$validator[] = array($item['input_name'], $item['label'], $item['checking_type']);
								}
						
								if($div_key == 8 && $idstatusmohon > 1)
								{
									echo '<button type="button" id="submitupdateBtn" class="btn btn-primary font-weight-bolder text-uppercase px-9 py-4" data-wizard-type="action-update">Hantar & Simpan Pengemaskinian</button>';
								}	
								
							}
							
							//set all hidden input for action
							foreach($actionArray as $actKey => $actItem)
							{
								echo '<input type="hidden" id="'.$actKey.'" value=\''.join(',', $actItem).'\'>';
							}
							
							echo '<input type="hidden" id="formValidator_'.$counter.'" value=\''.json_encode($validator).'\'>';
							echo '</form>';
						echo '</div>';
						$counter++;
					}
				?>
				<div class="d-flex justify-content-between border-top mt-5 pt-10">
					<div class="mr-2">
						<button type="button" class="btn btn-light-primary font-weight-bolder text-uppercase px-9 py-4" data-wizard-type="action-prev">Sebelum</button>
					</div>
					<div>
						<?php
							if((($id_pemohon == 0 || $need_akuan) && ($idstatusmohon == 1 || $idstatusmohon == 0)))
							{
								echo '<button type="button" id="submitBtn" disabled class="btn btn-success font-weight-bolder text-uppercase px-9 py-4" data-wizard-type="action-submit">Hantar</button>';
							}
						?>
						<button type="button" id="nextBtn" class="btn btn-primary font-weight-bolder text-uppercase px-9 py-4" data-wizard-type="action-next">Simpan & Seterusnya</button>
					</div>
				</div>
			<!--</form>-->
			<!--end::Wizard Form-->
		</div>
	</div>
	<!--end::Wizard Body-->
</div>
