<?php

if($_POST['baseName'] != '')
{
	/*
	$file = "general-list_ajax.php";
	if($_GET['type'] == 2)
	{
		$file = "general-list_sub_ajax.php";		
	}
	if(preg_match('/general\-maklumat$/', $_POST['baseName']))
	{
		$file = "general-maklumat_ajax.php";
		$manual_TOP = 31;
	}
	if(preg_match('/p05\_pemohon$/', $_POST['baseName']))
	{
		$file = "general-sub_pemohon_list_ajax.php";
	}
	else if(preg_match('/\_pemohon$/', $_POST['baseName']))
	{
		$file = "general-pemohon_list_ajax.php";
	}
	else if(preg_match('/in\_/', $_POST['baseName']))
	{
		$file = $_POST['baseName']."-ajax.php";
	}
	else if(preg_match('/la\_harian/', $_POST['baseName']))
	{
		$file = "la_harian-pemohon_ajax.php";
	}
	else if(preg_match('/eft\_/', $_POST['baseName']))
	{
		$file = $_POST['baseName']."-ajax.php";
	}
	else if(preg_match('/se\_/', $_POST['baseName']))
	{
		$file = $_POST['baseName']."-ajax.php";
		if(preg_match('/se\_meninggal/', $_POST['baseName']))
		{
			$manual_TOP = 31;
		}
	}
	*/
	$file = $_POST['baseName']."-ajax.php";
	require($file);
}

if($_POST['printAll'] == 1)
{
	//set max record = 100 for now
	//always record start from 0
	//$record_start = 0;
	//$record_max = 100;
	$_POST['start'] = 0;
	$_POST['length'] = -1;
}

//***********************************************************************************************************************
// ADDITIONAL SQL FIELD if any
//***********************************************************************************************************************


//***********************************************************************************************************************
// GET DATA
//***********************************************************************************************************************	
	//$result = SSP::myComplex($_POST, $sql_details, buildSelectQuery($columns), buildFromQuery($tableArray), $tableArray['main'].'.'.$tableArray['primary_key'], $columns, 0, buildWhereQuery($whereArray));
	//$dataArray = $result['data'];
	$FROM_QUERY = buildFromQuery($tableArray);
	$temp_result = SSP::myComplex($_POST, $sql_details, buildSelectQuery($columns), $FROM_QUERY, $tableArray['main'].'.'.$tableArray['primary_key'], $columns, 0, buildWhereQuery($whereArray));
	
	if($_POST['baseName'] == 'eft_laporan')
	{
		for($num = 0; $num < count($temp_result['data']); $num++)
		{
			$id_waris = $temp_result['data'][$num][11];		
			$id_pemohon = $temp_result['data'][$num][13];		
			$id_bank = $temp_result['data'][$num][14];	

			if($id_bank == 0)
			{			
				$indicator_Query = "SELECT A.*, B.desk_bank
								  FROM m_pemohon A
								  LEFT JOIN i_bank B ON B.id_bank = A.id_bank
								  WHERE A.id_pemohon = ?";
				$PDO_stmt = $PDO->prepare($indicator_Query);
				$PDO_stmt->bindValue(1, $id_pemohon, PDO::PARAM_INT);
				$PDO_stmt->execute();
				$pemohon_List = $PDO_stmt->fetch(PDO::FETCH_ASSOC);	
				
				$temp_result['data'][$num][6] = $pemohon_List['desk_bank'];
				$temp_result['data'][$num][7] = $pemohon_List['no_akaun'];			
			}
			
			if($id_waris > 0)
			{
				$temp_result['data'][$num][6] = 'Waris : '.$temp_result['data'][$num][12].'<br>Bank : '.$temp_result['data'][$num][6];
			}
			
		}
	}
	else if($_POST['baseName'] == 'se_meninggal')
	{
		$indicator_Query = "SELECT m_waris.id_pemohon, m_waris.nama_waris, m_waris.kpbaru_waris, i_bank.desk_bank, m_waris.no_akaunwaris,
							m_waris.tel_hpwaris, m_waris.alamat_waris, m_waris.alamat_waris2, m_waris.alamat_waris3, i_poskod.desk_poskod, m_waris.bandar_waris, i_negeri.desk_negeri 
							FROM m_waris
							LEFT JOIN i_bank ON m_waris.id_bankwaris = i_bank.id_bank 
							LEFT JOIN i_poskod ON m_waris.id_poskodwaris = i_poskod.id_poskod 
							LEFT JOIN i_negeri ON m_waris.id_negeriwaris = i_negeri.id_negeri 
						  WHERE m_waris.status = 1";
		$PDO_stmt = $PDO->prepare($indicator_Query);
		$PDO_stmt->execute();
		$waris_List = [];
		while($result = $PDO_stmt->fetch(PDO::FETCH_ASSOC))
		{
			$waris_List[$result['id_pemohon']] = $result;
		}
		
		$columns[] = array( 'db' => 'nama_waris', 'dt' => 16, 'table' => 'm_waris', 'print'=>1 );
		$columns[] = array( 'db' => 'kpbaru_waris', 'dt' => 17, 'table' => 'm_waris', 'print'=>1 );
		$columns[] = array( 'db' => 'desk_bank', 'dt' => 18, 'table' => 'i_bank', 'print'=>1 );
		$columns[] = array( 'db' => 'no_akaunwaris', 'dt' => 19, 'table' => 'm_waris', 'print'=>1 );
		$columns[] = array( 'db' => 'tel_hpwaris', 'dt' => 20, 'table' => 'm_waris', 'print'=>1 );
		$columns[] = array( 'db' => 'alamat_waris', 'dt' => 21, 'table' => 'm_waris', 'print'=>1 );
		$columns[] = array( 'db' => 'alamat_waris2', 'dt' => 22, 'table' => 'm_waris', 'print'=>1 );
		$columns[] = array( 'db' => 'alamat_waris3', 'dt' => 23, 'table' => 'm_waris', 'print'=>1 );
		$columns[] = array( 'db' => 'desk_poskod', 'dt' => 24, 'table' => 'i_poskod', 'print'=>1 );
		$columns[] = array( 'db' => 'bandar_waris', 'dt' => 25, 'table' => 'm_waris', 'print'=>1 );
		$columns[] = array( 'db' => 'desk_negeri', 'dt' => 26, 'table' => 'i_negeri', 'print'=>1 );
		
		for($num = 0; $num < count($temp_result['data']); $num++)
		{
			$id_pemohon = $temp_result['data'][$num][9];
			$temp_result['data'][$num][16] = $waris_List[$id_pemohon]['nama_waris'];
			$temp_result['data'][$num][17] = $waris_List[$id_pemohon]['kpbaru_waris'];
			$temp_result['data'][$num][18] = $waris_List[$id_pemohon]['desk_bank'];
			$temp_result['data'][$num][19] = $waris_List[$id_pemohon]['no_akaunwaris'];
			$temp_result['data'][$num][20] = $waris_List[$id_pemohon]['tel_hpwaris'];
			$temp_result['data'][$num][21] = $waris_List[$id_pemohon]['alamat_waris'];
			$temp_result['data'][$num][22] = $waris_List[$id_pemohon]['alamat_waris2'];
			$temp_result['data'][$num][23] = $waris_List[$id_pemohon]['alamat_waris3'];
			$temp_result['data'][$num][24] = $waris_List[$id_pemohon]['desk_poskod'];
			$temp_result['data'][$num][25] = $waris_List[$id_pemohon]['bandar_waris'];
			$temp_result['data'][$num][26] = $waris_List[$id_pemohon]['desk_negeri'];
		}
	}
	
	$dataArray = $temp_result['data'];
	
//***********************************************************************************************************************
// COMMON PARAMETER FOR PDF AND EXCEL
//***********************************************************************************************************************
	
	$TITLE = $_POST['title'];
	$AUTHOR = (($my_details->fullname=='')?'admin_'.$my_details->usr_id:$my_details->fullname);
	$FILE_NAME = str_replace(' : ', '_', $TITLE).'_'.date("d_m_Y");
	$FILE_NAME = str_replace(' ', '_', $FILE_NAME).'_'.date("d_m_Y");
	$PAGE_ORIENTATION = $_POST['pageOrientation'];
	
	//***********************************************************
	//CHANGE THIS FOR TITLE
	//***********************************************************
	$myTitle = $TITLE;
	$mySubHeaderTitle = $_POST['subHeaderTitle'];
	$headers = json_decode($_POST['header']);
	$headerTitle = [];
	foreach($headers as $header)
	{
		if($header[1] > 0)
		{
			$headerTitle[] = $header;
		}
	}
	
	if($_POST['printonlyheader'] != '') //only for excel
	{
		$printonlyheader = json_decode($_POST['printonlyheader']);
		foreach($printonlyheader as $POheader)
		{
			if($POheader[1] > 0)
			{
				$headerTitle[] = $POheader;
			}
		}
	}
	
//***********************************************************************************************************************

require('commonPrint.php');
?>