<?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);
$sahmati = fetch_postget_val('sahmati',$_POST, $_GET);
$telahbayar_sc = fetch_postget_val('telahbayar_sc',$_POST, $_GET);
$telahbayarprogram_sc = fetch_postget_val('telahbayarprogram_sc',$_POST, $_GET);
$tahun_sc = fetch_postget_val('tahun_sc',$_POST, $_GET);
$bayaranfasa_sc = fetch_postget_val('bayaranfasa_sc',$_POST, $_GET);
$khairatkat_sc = fetch_postget_val('khairatkat_sc',$_POST, $_GET);
$tahun_sah = fetch_postget_val('tahun_sah',$_POST, $_GET);
$tahun_mati = fetch_postget_val('tahun_mati',$_POST, $_GET);

$mbayar_table = "m_bayaran_KH";
//*****************************************************
// 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_sahmati', 'sub_id' => 'id_sahmati', 'main_id' => 'id_sahmati'),
		array('name' => 'i_khairatkat', 'sub_id' => 'id_khairatkat', 'main_id' => 'id_khairatkat'),
		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'),
		//array('name' => 'm_waris', 'sub_id' => 'id_pemohon', 'main_id' => 'id_pemohon'),
		//array('name' => 'i_bank', 'sub_id' => 'id_bank', 'main_id' => 'id_bankwaris', 'main_name'=>'m_waris'),
		//array('name' => 'i_poskod', 'sub_id' => 'id_poskod', 'main_id' => 'id_poskodwaris', 'main_name'=>'m_waris'),
		//array('name' => 'i_negeri', 'sub_id' => 'id_negeri', 'main_id' => 'id_negeriwaris', 'main_name'=>'m_waris'),
	),
);

//*****************************************************
// 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_keyinmati', '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' => 'tkh_mati', 'dt' => 4, 'table' => 'm_pemohon', 'print'=>1,
			'formatter' => function( $d, $row ) {
				
				return formatDate($d, 'd M Y');
			}
	),
    array( 'db' => 'no_sijilmati', 'dt' => 5, 'table' => 'm_pemohon', 'print'=>1 ),
    array( 'db' => 'desk_program', 'dt' => 6, 'table' => 'i_program', 'print'=>1 ),
    array( 'db' => 'desk_sahmati', 'dt' => 7, 'table' => 'i_sahmati', 'print'=>1 ),
	array( 'dt' => 8, 'user_defined' => ''),
    array( 'db' => 'id_pemohon', 'dt' => 9, 'table' => 'm_pemohon' ),
    array( 'db' => 'id_program', 'dt' => 10, 'table' => 'm_pemohon' ),
    array( 'db' => 'id_sahmati', 'dt' => 11, 'table' => 'm_pemohon' ),
    array( 'db' => 'desk_khairatkat', 'dt' => 12, 'table' => 'i_khairatkat' ),
    array( 'db' => 'flag_querymati', 'dt' => 13, 'table' => 'm_pemohon' ),
    array( 'db' => 'id_khairatkat', 'dt' => 14, 'table' => 'm_pemohon' ),
    array( 'db' => 'desk_dun', 'dt' => 15, 'table' => 'i_dun', 'print'=>1 ),
	/*array( 'db' => 'nama_waris', 'dt' => 15, 'table' => 'm_waris', 'print'=>1 ),
	array( 'db' => 'kpbaru_waris', 'dt' => 16, 'table' => 'm_waris', 'print'=>1 ),
	array( 'db' => 'desk_bank', 'dt' => 17, 'table' => 'i_bank', 'print'=>1 ),
	array( 'db' => 'no_akaunwaris', 'dt' => 18, 'table' => 'm_waris', 'print'=>1 ),
	array( 'db' => 'tel_hpwaris', 'dt' => 19, 'table' => 'm_waris', 'print'=>1 ),
	array( 'db' => 'alamat_waris', 'dt' => 20, 'table' => 'm_waris', 'print'=>1 ),
	array( 'db' => 'alamat_waris2', 'dt' => 21, 'table' => 'm_waris', 'print'=>1 ),
	array( 'db' => 'alamat_waris3', 'dt' => 22, 'table' => 'm_waris', 'print'=>1 ),
	array( 'db' => 'desk_poskod', 'dt' => 23, 'table' => 'i_poskod', 'print'=>1 ),
	array( 'db' => 'bandar_waris', 'dt' => 24, 'table' => 'm_waris', 'print'=>1 ),
	array( 'db' => 'desk_negeri', 'dt' => 25, 'table' => 'i_negeri', 'print'=>1 ),*/
);

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

$whereArray = array(
	array('m_pemohon', 'id_statushidup', 2),
	//array('m_pemohon', 'id_dun', 0, '>'),
	//array('m_waris', 'status', 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($sahmati == 2)
{
	$whereArray[] = array('m_pemohon', 'id_sahmati', 2);
}
else if($sahmati == 1)
{
	$whereArray[] = array('m_pemohon', 'id_sahmati', 1);	
}

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

if($telahbayar_sc == 1)
{
	$whereArray[] = array('m_pemohon', 'flag_bayarkhairat', 0);
}
else if($telahbayar_sc == 2)
{
	$whereArray[] = array('m_pemohon', 'flag_bayarkhairat', 1);
}

if($tahun_sah > 0)
{
	$whereArray[] = array('m_pemohon', 'tkh_keyinmati', "'".$tahun_sah."-01-01 00:00:00'", '>=');
}

if($tahun_mati > 0)
{
	$whereArray[] = array('m_pemohon', 'tkh_mati', "'".$tahun_mati."-01-01'", '>=');
}

if($telahbayarprogram_sc == 1) //belum bayar tunai
{
	$indicator_Query = "SELECT A.*
					  FROM i_kawalan A
					  WHERE A.id_program = ?";
	$PDO_stmt = $PDO->prepare($indicator_Query);
	$PDO_stmt->bindValue(1, $program, PDO::PARAM_INT);
	$PDO_stmt->execute();
	$kawalan_List = $PDO_stmt->fetch(PDO::FETCH_ASSOC);	
	
	$cutoff = $kawalan_List['tkh_cutoff_tunai'];
	$cutoff_aktif = $kawalan_List['tkh_cutoff_aktifmohon'];
	$thn_cutoff = $kawalan_List['tahun_bayaran_tunai'];
	
	if($id_program != 4)
	{
		$whereArray[] = array('m_pemohon', 'id_dun', 0, '!=');
	}
	$whereArray[] = array(
						array('m_pemohon', 'tkh_aktifmohon', "'0000-00-00 00:00:00'"),
						array('m_pemohon', 'tkh_aktifmohon', "'".$cutoff_aktif." 23:59:59'", '<=')
						);
	$whereArray[] = array('m_pemohon', 'tkh_sah', "'".$cutoff." 23:59:59'", '<=');	
	$whereArray[] = array('m_pemohon', 'id_pemohon', ' (SELECT id_pemohon FROM l_penerima) ', 'NOT IN');
}
else if($telahbayarprogram_sc == 2) //telah bayar
{
	$whereArray[] = array('m_pemohon', 'id_pemohon', ' (SELECT id_pemohon FROM l_penerima) ', 'IN');
}

$whereArray[] = array('m_pemohon', 'id_program', '(1,2,4)', 'IN');
//$whereArray[] = array('m_pemohon', 'id_dun', 0, '>');

//echo SSP::myComplex($_POST, $sql_details, buildSelectQuery($columns), buildFromQuery($tableArray), $tableArray['main'].'.'.$tableArray['primary_key'], $columns, 1, buildWhereQuery($whereArray));
//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][9];
		$id_program = $temp_result['data'][$num][10];
		$id_sahmati = $temp_result['data'][$num][11];
		$desk_khairatkat = $temp_result['data'][$num][12];
		$flag_sahmati = $temp_result['data'][$num][13];
		$id_khairatkat = $temp_result['data'][$num][14];
		
		$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>';
		
		if($id_sahmati == 1)
		{
			if($flag_sahmati == 1)
			{
				$edit_khairat = '';
				if($my_details->user_level <= 4 || $my_details->user_level == 8)
				{					
					$edit_khairat = '<a class="btn btn-sm btn-clean btn-icon mr-2" onclick="loadMyModal(\'general-khairat.php?id='.$id_pemohon.'\', \'#ajax-modal\')" title="Edit"><span class="svg-icon svg-icon-primary svg-icon-2x">'.$Edit_svg.'</span></a>';					
				}
				
				$temp_result['data'][$num][8] = $edit_khairat.$log_btn;	
				
				$query = 'DIQUERY';
			}
			else
			{	
				$sah_khairat = '';
				if($my_details->user_level == 1 || $my_details->user_level == 3 || $my_details->user_level == 5 || $my_details->user_level == 9)
				{					
					$sah_khairat = '<a class="btn btn-sm btn-clean btn-icon mr-2" onclick="loadMyModal(\'general-khairat_sah.php?id='.$id_pemohon.'&program='.$id_program.'\', \'#ajax-modal\')" title="Pengesahan"><span class="svg-icon svg-icon-primary svg-icon-2x">'.$File_svg.'</span></a>';	
				}
				
				$temp_result['data'][$num][8] = $sah_khairat.$log_btn;	
				
				$query = '';
			}
			
			$temp_result['data'][$num][7] = $temp_result['data'][$num][7].'<br><b>'.$query.'</b>';
		}
		else
		{			
			$temp_result['data'][$num][7] = $temp_result['data'][$num][7].'<br><b>'.$desk_khairatkat.'</b>';
			$temp_result['data'][$num][8] = $log_btn;	
		}
		
		//only show + button if single daerah, tahun & fasa
		$tambah_btn = '';
		if($telahbayar_sc == 1 && $sahmati == 2 && ($id_khairatkat != 2 && $id_khairatkat != 3))
		{
			$tambah_btn = '<a class="btn btn-sm btn-default btn-text-primary btn-hover-primary btn-icon mr-2" id="tambahBtn_'.$id_pemohon.'" onclick="tambah(this, '.$id_pemohon.', 0)" title="Tambah"><span class="svg-icon svg-icon-success svg-icon-2x">'.$Plus_svg.'</span></a>';
		}
		$temp_result['data'][$num][8] .= $tambah_btn;
	}
	echo json_encode(
		$temp_result
	);
}