<?php
 
if($_COOKIE['sejahtera_role'] == 10)
{
	include_once ("includes/setup_pemohon.inc.php");
}
else
{
	include_once ("includes/setup.inc.php");
}

/*
 * DataTables example server-side processing script.
 *
 * Please note that this script is intentionally extremely simply to show how
 * server-side processing can be implemented, and probably shouldn't be used as
 * the basis for a large complex system. It is suitable for simple use cases as
 * for learning.
 *
 * See http://datatables.net/usage/server-side for full details on the server-
 * side processing requirements of DataTables.
 *
 * @license MIT - http://datatables.net/license_mit
 */
 
 
//*****************************************************
//Get all POST and GET value
//*****************************************************
$sqlOnly = fetch_postget_val('sqlOnly',$_POST, $_GET);
$program = fetch_postget_val('program',$_POST, $_GET);
$borangItem = fetch_postget_val('borangItem',$_POST, $_GET);
$pemohon_borang = fetch_postget_val('pemohon_borang',$_POST, $_GET);

//get all u_datatableitem
$indicator_Query = "SELECT *
				  FROM u_datatableitem 
				  WHERE id_borangitem = ".$borangItem;
$PDO_stmt = $PDO->prepare($indicator_Query);
$PDO_stmt->execute();
$datatableItemList = [];
while($result = $PDO_stmt->fetch(PDO::FETCH_ASSOC))
{
	$datatableItemList[$result['type']][] = $result;
}

//*****************************************************
// MAIN TABLE AND JOIN TABLE
// main = main table name in the database,
// sub['name'] = join table name
// sub['sub_id'] = join table identifier
// sub['main_id'] = main table identifier
// sub['join_type'] = type of join. Default = 'LEFT'
// sub['aka'] = put alternative name for table
//*****************************************************
$tableArray = array(
	'main' => $datatableItemList[1][0]['table_name'],
	'primary_key' => $datatableItemList[1][0]['column_name'],
);
foreach($datatableItemList[2] as $key=>$item)
{
	//array('name' => 't_peranan', 'sub_id' => 'id_peranan', 'main_id' => 'id_peranan'),
	$tableArray['sub'][] = array('name' => $item['table_name'], 'sub_id' => $item['column_name'], 'main_id' => $item['otherInfo']);
}
//*****************************************************
// DATATABLE COLUMN
// db = column name in the database,
// dt = DataTables column identifier
// table = database table name
// akaFrom = use if and only if there is aka name for table
//*****************************************************
$columns = array(
    array( 'user_defined' => '', 'dt' => 0)
);
$tindakan_array = [];
foreach($datatableItemList[3] as $key=>$item)
{
	$temp = array();
	if($item['column_name'] != ''){$temp['db'] = $item['column_name'];}
	if($item['datatable_index'] != ''){$temp['dt'] = $item['datatable_index'];}
	if($item['table_name'] != ''){$temp['table'] = $item['table_name'];}
	if($item['user_defined'] != ''){$temp['user_defined'] = $item['user_defined'];}
	$columns[] = $temp;
	if($item['table_name'] == '' && $temp['user_defined'] != '')
	{
		$tindakan_array[] = $temp['dt'];
	}
}


//var_dump($columns);
//var_dump(json_encode(array( 'db' => 'kod_rentas', 'dt' => 2, 'table' => 'i_bank' )));
//var_dump(json_encode(array( 'db' => 'digit_bank', 'dt' => 3, 'table' => 'i_bank' )));
//*****************************************************
// WHERE SQL
// Array ($table_name, $column_name, $value, $symbol(default : '='))
//*****************************************************
$whereArray = array(
	//theoritically all should link to pemohon only, so hardcode here
	array($datatableItemList[1][0]['table_name'], 'id_pemohon', $pemohon_borang, '=')
);
//echo SSP::myComplex($_POST, $sql_details, buildSelectQuery($columns), buildFromQuery($tableArray), $tableArray['main'].'.'.$tableArray['primary_key'], $columns, 1, null);
//echo "whereArray = ".count($whereArray);
//exit(0);
//*****************************************************
// SSP GET JSON
// No need to modify below
//*****************************************************
if($sqlOnly == 1)
{
	//LEAVE EMPTY FOR FUTURE USAGE
}
else
{
	//special case for anakemas
	if($program == 5)
	{
		$indicator_Query = "SELECT id_statusmohon
						  FROM m_pemohon
						  WHERE id_pemohon = ?";
		$PDO_stmt = $PDO->prepare($indicator_Query);
		$PDO_stmt->bindValue(1, $pemohon_borang, PDO::PARAM_INT);
		$PDO_stmt->execute();
		$result = $PDO_stmt->fetch(PDO::FETCH_ASSOC);
		$statusmohon = $result['id_statusmohon'];
	}
	$temp_result = SSP::myComplex($_POST, $sql_details, buildSelectQuery($columns), buildFromQuery($tableArray), $tableArray['main'].'.'.$tableArray['primary_key'], $columns, 0, buildWhereQuery($whereArray));
	for($num = 0; $num < count($temp_result['data']); $num++)
	{
		foreach($tindakan_array as $key=>$item)
		{
			$temp_result['data'][$num][$item] = str_replace('REPLACE_BORANGITEM', $borangItem, $temp_result['data'][$num][$item]);
			$temp_result['data'][$num][$item] = str_replace('REPLACE_PROGRAM', $program, $temp_result['data'][$num][$item]);
			$temp_result['data'][$num][$item] = str_replace('REPLACE_EDITSVG', $Edit_svg, $temp_result['data'][$num][$item]);
			$temp_result['data'][$num][$item] = str_replace('REPLACE_ACTIVATESVG', $Activate_svg, $temp_result['data'][$num][$item]);
			$temp_result['data'][$num][$item] = str_replace('REPLACE_FILESVG', $File_svg, $temp_result['data'][$num][$item]);
			$temp_result['data'][$num][$item] = str_replace('REPLACE_LAPORANSVG', $Laporan_svg, $temp_result['data'][$num][$item]);
			
			//special case for anakemas
			if($program == 5 && (strpos($temp_result['data'][$num][$item], '<SPLIT>') !== false))
			{
				$buttons = explode('<SPLIT>', $temp_result['data'][$num][$item]);
				if($my_details->user_level == 10)
				{
					if($temp_result['data'][$num][7] <= 1)
					{						
						$temp_result['data'][$num][$item] = $buttons[0].$buttons[1];
					}
					else if($temp_result['data'][$num][7] >= 2)
					{
						$temp_result['data'][$num][$item] = $buttons[2];
					}
				}
				else if($my_details->user_level <= 5)
				{
					if($temp_result['data'][$num][7] <= 1)
					{						
						$temp_result['data'][$num][$item] = $buttons[2];
					}
					else if($temp_result['data'][$num][7] >= 2)
					{
						$temp_result['data'][$num][$item] = $buttons[0].$buttons[2];
					}
				}
				else
				{
					$temp_result['data'][$num][$item] = $buttons[2];
				}
				unset($temp_result['data'][$num][7]);
			}
		}
	}
	echo json_encode(
		$temp_result
	);
}