<?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);
$program = fetch_postget_val('program',$_POST, $_GET);
$jenis = fetch_postget_val('jenis',$_POST, $_GET);
$status = fetch_postget_val('status',$_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_aduan',
	'primary_key' => 'id_aduan',
	'sub' => array(
		array('name' => 'i_jenisaduan', 'sub_id' => 'id_jenisaduan', 'main_id' => 'id_jenisaduan'),
		array('name' => 'i_statusaduan', 'sub_id' => 'id_statusaduan', 'main_id' => 'id_statusaduan'),
	),
);

//*****************************************************
// 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_keyin', 'dt' => 1, 'table' => 'm_aduan',
			'formatter' => function( $d, $row ) {
				
				return formatDate($d, 'd M Y');
			}
	),
    array( 'db' => 'nama_pengadu', 'dt' => 2, 'table' => 'm_aduan' ),
    array( 'db' => 'tajuk', 'dt' => 3, 'table' => 'm_aduan' ),
    array( 'db' => 'desk_jenisaduan', 'dt' => 4, 'table' => 'i_jenisaduan' ),
    array( 'db' => 'desk_statusaduan', 'dt' => 5, 'table' => 'i_statusaduan' ),
	array( 'dt' => 6, 'user_defined' => ''),
    array( 'db' => 'id_aduan', 'dt' => 7, 'table' => 'm_aduan' ),
    array( 'db' => 'id_statusaduan', 'dt' => 8, 'table' => 'm_aduan' ),
    array( 'db' => 'id_assign', 'dt' => 9, 'table' => 'm_aduan' ),
    array( 'db' => 'id_pemohon', 'dt' => 10, 'table' => 'm_aduan' ),
    array( 'db' => 'flag_form', 'dt' => 11, 'table' => 'm_aduan' ),
);

//*****************************************************
// WHERE SQL
// Array ($table_name, $column_name, $value, $symbol(default : '='))
//*****************************************************
$usr_id_sys = $my_details->usr_id_sys;
$usr_id = $my_details->usr_id;

$whereArray = array(
);	
if($my_details->user_level == 10)
{
	$whereArray[] = array('m_aduan', 'id_pemohon', $usr_id_sys, '=');
}
else if($my_details->user_level != 1 && $my_details->user_level != 3)
{
	$whereArray[] = array('m_aduan', 'id_assign', $usr_id, '=');
}

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

if($jenis > 0)
{
	$whereArray[] = array('m_aduan', 'id_jenisaduan', $jenis);
}

if($status > 0)
{
	$whereArray[] = array('m_aduan', 'id_statusaduan', $status );
}
//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++)
	{
		
		$tajuk = $temp_result['data'][$num][3];
		$id_aduan = $temp_result['data'][$num][7];
		$id_statusaduan = $temp_result['data'][$num][8];
		$id_assign = $temp_result['data'][$num][9];		
		$id_pemohon = $temp_result['data'][$num][10];		
		$flag_form = $temp_result['data'][$num][11];		
		
		if($id_statusaduan == 1 && $my_details->user_level != 10)
		{
			$temp_result['data'][$num][6] = 
			'<a class="btn btn-sm btn-clean btn-icon mr-2" onclick="loadMyModal(\'in_aduan-status.php?id='.$id_aduan.'\', \'#ajax-modal\')" title="Papar">
			<span class="svg-icon svg-icon-primary svg-icon-2x">'.$File_svg.'</span></a>
			<a class="btn btn-sm btn-clean btn-icon mr-2" id="deleteBtn" title="Selesai" onclick="deleteFunction('.$id_aduan.', \''.$tajuk.'\')">
			<span class="svg-icon svg-icon-danger svg-icon-2x">'.$Activate_svg.'</span></a>';
		}
		else
		{
			$temp_result['data'][$num][6] = 
			'<a class="btn btn-sm btn-clean btn-icon mr-2" onclick="loadMyModal(\'in_aduan-status.php?id='.$id_aduan.'\', \'#ajax-modal\')" title="Papar">
			<span class="svg-icon svg-icon-primary svg-icon-2x">'.$File_svg.'</span></a>';			
		}		
	}
	echo json_encode(
		$temp_result
	);
}