<?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
 */
 
include_once ("includes/setup.inc.php");	
//*****************************************************
//Get all POST and GET value
//*****************************************************
$sqlOnly = fetch_postget_val('sqlOnly',$_POST, $_GET);
$dun_sc = fetch_postget_val('dun_sc',$_POST, $_GET);
$parlimen_sc = fetch_postget_val('parlimen_sc',$_POST, $_GET);
$daerah_sc = fetch_postget_val('daerah_sc',$_POST, $_GET);
$bangsa_sc = fetch_postget_val('bangsa_sc',$_POST, $_GET);
$program = fetch_postget_val('program',$_POST, $_GET);
$aktifmohon_sc = fetch_postget_val('aktifmohon_sc',$_POST, $_GET);
$statusmohon_sc = fetch_postget_val('statusmohon_sc',$_POST, $_GET);

//*****************************************************
// 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' => 'm_pemohon',
	'primary_key' => 'id_pemohon',
	'sub' => array(
		array('name' => 'i_program', 'sub_id' => 'id_program', 'main_id' => 'id_program'),
		array('name' => 'i_statusmohon', 'sub_id' => 'id_statusmohon', 'main_id' => 'id_statusmohon'),
		array('name' => 'i_aktifmohon', 'sub_id' => 'id_aktifmohon', 'main_id' => 'id_aktifmohon'),
		array('name' => 'i_dun', 'sub_id' => 'id_dun', 'main_id' => 'id_dun'),
		array('name' => 'i_daerah', 'sub_id' => 'id_daerah', 'main_id' => 'id_daerah', 'main_name'=>'i_dun'),
		array('name' => 'i_parlimen', 'sub_id' => 'id_parlimen', 'main_id' => 'id_parlimen', 'main_name'=>'i_dun'),
	),
);

//*****************************************************
// 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),
	array( 'db' => 'tkh_daftar', 'dt' => 1, 'table' => 'm_pemohon', 'print'=>1,
			'formatter' => function( $d, $row ) {
				
				return formatDate($d, 'd M Y');
			}
	),
    array( 'db' => 'nama_pemohon', 'dt' => 2, 'table' => 'm_pemohon', 'print'=>1 ),
    array( 'db' => 'kpbaru_pemohon', 'dt' => 3, 'table' => 'm_pemohon', 'print'=>1 ),
    array( 'db' => 'desk_program', 'dt' => 4, 'table' => 'i_program', 'print'=>1 ),
    array( 'db' => 'desk_statusmohon', 'dt' => 5, 'table' => 'i_statusmohon', 'print'=>1 ),
    array( 'db' => 'desk_aktifmohon', 'dt' => 6, 'table' => 'i_aktifmohon', 'print'=>1 ),
	array( 'dt' => 7, 'user_defined' => ''),
    array( 'db' => 'id_pemohon', 'dt' => 8, 'table' => 'm_pemohon' ),
    array( 'db' => 'id_program', 'dt' => 9, 'table' => 'm_pemohon' ),
);

//*****************************************************
// WHERE SQL
// Array ($table_name, $column_name, $value, $symbol(default : '='))
//*****************************************************

$whereArray = array(
	array('m_pemohon', 'id_statushidup', 1),
	array('m_pemohon', 'id_aktifmohon', 1, '!='),
);	

if($dun_sc > 0)
{
	$whereArray[] = array('m_pemohon', 'id_dun', $dun_sc);
}

if($parlimen_sc > 0)
{
	$whereArray[] = array('i_parlimen', 'id_parlimen', $parlimen_sc);
}

if($daerah_sc > 0)
{
	$whereArray[] = array('i_daerah', 'id_daerah', $daerah_sc);
}

if($bangsa_sc == 1)
{
	$whereArray[] = array('m_pemohon', 'id_bangsa', 1);
}
else if($bangsa_sc == 2)
{
	$whereArray[] = array('m_pemohon', 'id_bangsa', 2);
}
else if($bangsa_sc == 3)
{
	$whereArray[] = array('m_pemohon', 'id_bangsa', 3);
}
else if($bangsa_sc == 5)
{
	$whereArray[] = array(
		array('m_pemohon', 'id_bangsa', 0),
		array('m_pemohon', 'id_bangsa', 3, '>'),
	);
}

if($program > 0)
{
	$whereArray[] = array('m_pemohon', 'id_program', $program);
}

if($aktifmohon_sc == 0)
{
	$whereArray[] = array('m_pemohon', 'id_aktifmohon', 1, '!=');
}
else if($aktifmohon_sc > 0)
{
	$whereArray[] = array('m_pemohon', 'id_aktifmohon', $aktifmohon_sc);
}

if($statusmohon_sc > 0)
{
	$whereArray[] = array('m_pemohon', 'id_statusmohon', $statusmohon_sc);
}
//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
{	
	$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++)
	{
		$id_pemohon = $temp_result['data'][$num][8];
		$id_program = $temp_result['data'][$num][9];
		
		$temp_result['data'][$num][2] = '<a href="javascript:;" onclick="loadMyModal(\'general-maklumat.php?id='.$id_pemohon.'&program='.$id_program.'\', \'#ajax-modal\')" title="Maklumat Pemohon">'.$temp_result['data'][$num][2].'</a>';
		
		$log_btn = '<a class="btn btn-sm btn-default btn-text-primary btn-hover-primary btn-icon mr-2" onclick="loadMyModal(\'general-log.php?id='.$id_pemohon.'\', \'#ajax-modal\')" title="Log Permohonan"><span class="svg-icon svg-icon-primary svg-icon-2x">'.$Log_svg.'</span></a>';
		
		$temp_result['data'][$num][7] = $log_btn;
	}
	echo json_encode(
		$temp_result
	);
}