<?php

if($_COOKIE['sejahtera_role'] == 10)
{
	include_once ("includes/setup_pemohon.inc.php");
}
else
{
	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);
$terima_sc = fetch_postget_val('terima_sc',$_POST, $_GET);
$sah_sc = fetch_postget_val('sah_sc',$_POST, $_GET);
$saheft_sc = fetch_postget_val('saheft_sc',$_POST, $_GET);
$id_program = fetch_postget_val('program',$_POST, $_GET);
$flow_type = fetch_postget_val('flow_type',$_POST, $_GET);
$telahbayar_sc = fetch_postget_val('telahbayar_sc',$_POST, $_GET);
$tahun_sc = fetch_postget_val('tahun_sc',$_POST, $_GET);
$tahun_sah = fetch_postget_val('tahun_sah',$_POST, $_GET);
$bayaranfasa_sc = fetch_postget_val('bayaranfasa_sc',$_POST, $_GET);
$id_peranan = $my_details->user_level;
$name_id_program = $id_program;
if ($id_program < 10 && (!(substr($id_program, 0, 1) === '0')))
{
	$name_id_program = '0'.$id_program;
}

$id_statusmohon = 4;

$PDO_stmt = $PDO->prepare("
	SELECT *
	FROM i_program
	WHERE id_program = ?
");
$PDO_stmt->bindValue(1, $id_program, PDO::PARAM_INT);
$PDO_stmt->execute();
$program_List = $PDO_stmt->fetch(PDO::FETCH_ASSOC);

$mbayar_table = "m_bayaran_".$program_List['kod_program'];
$id_mbayar_table = "id_bayaran_".$program_List['kod_program'];

//*****************************************************
// 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['main_name'] = main table name (optional), if no set, will use "main table name"
// sub['join_type'] = type of join. Default = 'LEFT'
// sub['aka'] = put alternative name for table
//*****************************************************
$tableArray = array(
	'main' => 'm_anakemas',
	'primary_key' => 'id_anakemas',
	'sub' => array(
		array('name' => 'i_statusmohon', 'sub_id' => 'id_statusmohon', 'main_id' => 'id_statusmohon'),
		array('name' => 'm_pemohon', 'sub_id' => 'id_pemohon', 'main_id' => 'id_pemohon'),
		array('name' => 'm_bayaran_AE', 'sub_id' => 'id_anakemas', 'main_id' => 'id_anakemas'),
		array('name' => 'i_dun', 'sub_id' => 'id_dun', 'main_id' => 'id_dun', 'main_name'=>'m_pemohon'),
		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' => 'i_bank', 'sub_id' => 'id_bank', 'main_id' => 'id_bankanakemas'),
		array('name' => 't_pengguna', 'sub_id' => 'id_pengguna', 'main_id' => 'id_terima'),
		array('name' => 't_pengguna', 'sub_id' => 'id_pengguna', 'main_id' => 'id_sah', 'aka' => 'peg_sah'),
	),
);

//*****************************************************
// 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( 'bil_col' => '', 'dt' => 0),
	array( 'db' => 'nama_anakemas', 'dt' => 1, 'table' => 'm_anakemas' , 'print'=>1 ),
	array( 'db' => 'kpbaru_anakemas', 'dt' => 2, 'table' => 'm_anakemas' , 'print'=>1 ),
	array( 'db' => 'no_sijillahir', 'dt' => 3, 'table' => 'm_anakemas' , 'print'=>1 ),
	array( 'db' => 'tkh_sah', 'dt' => 4, 'table' => 'm_anakemas' , 'print'=>1 ),
	array( 'dt' => 5, 'user_defined' => ''),
	array( 'db' => 'id_anakemas', 'dt' => 6, 'table' => 'm_anakemas' ),
	array( 'db' => 'id_dun', 'dt' => 7, 'table' => 'm_pemohon' ),
	array( 'db' => 'flag_saheft', 'dt' => 8, 'table' => 'm_anakemas' ),
	array( 'db' => 'ulasan_terima', 'dt' => 9, 'table' => 'm_anakemas' ),
	array( 'db' => 'ulasan_sah', 'dt' => 10, 'table' => 'm_anakemas' ),
	array( 'db' => 'id_statusmohon', 'dt' => 11, 'table' => 'm_anakemas' ),
	array( 'db' => 'nama_pemohon', 'dt' => 12, 'table' => 'm_pemohon', 'print'=>1 ),
	array( 'db' => 'kpbaru_pemohon', 'dt' => 13, 'table' => 'm_pemohon', 'print'=>1 ),
	array( 'db' => 'desk_bank', 'dt' => 14, 'table' => 'i_bank', 'print'=>1 ),
	array( 'db' => 'no_akaunanakemas', 'dt' => 15, 'table' => 'm_anakemas', 'print'=>1 ),
	array( 'db' => 'id_pemohon', 'dt' => 16, 'table' => 'm_anakemas' ),
	array( 'db' => 'desk_dun', 'dt' => 17, 'table' => 'i_dun', 'print'=>1 ),
	array( 'db' => 'tkh_daftar', 'dt' => 18, 'table' => 'm_anakemas', 'print'=>1),
	array( 'db' => 'tkh_terima', 'dt' => 19, 'table' => 'm_anakemas', 'print'=>1),
	array( 'db' => 'nama', 'dt' => 20, 'table' => 't_pengguna', 'print'=>1),
	array( 'db' => 'tkh_sah', 'dt' => 21, 'table' => 'm_anakemas', 'print'=>1),
	array( 'db' => 'peg_sahnama', 'dt' => 22, 'table' => 'peg_sah', 'akaFrom' => 'nama', 'print'=>1),
);


//*****************************************************
// WHERE SQL
// Array ($table_name, $column_name, $value, $symbol(default : '='))
//*****************************************************
$whereArray = array(
	array('m_anakemas', 'id_program', $id_program),
	array('m_anakemas', 'id_statusmohon', $id_statusmohon),
);

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($terima_sc > 0)
{
	$whereArray[] = array('m_anakemas', 'id_terima', $terima_sc);
}

if($sah_sc > 0)
{
	$whereArray[] = array('m_anakemas', 'id_sah', $sah_sc);
}

if($saheft_sc == 1)
{
	$whereArray[] = array('m_anakemas', 'flag_saheft', 0);
}
else if($saheft_sc == 2)
{
	$whereArray[] = array('m_anakemas', 'flag_saheft', 1);
}

//if($telahbayar_sc == 1 && $tahun_sc > 0)
if($telahbayar_sc == 1)
{
	$whereArray[] = array('m_anakemas', 'flag_bayaran', 0);
	//$whereArray[] = array('m_anakemas', 'id_anakemas', ' (SELECT id_anakemas FROM '.$mbayar_table.' WHERE tahun_bayar = '.$tahun_sc.') ', 'NOT IN');
}
else if($telahbayar_sc == 2)
{
	$whereArray[] = array('m_anakemas', 'flag_bayaran', 1);
	//$whereArray[] = array('m_anakemas', 'id_anakemas', ' (SELECT id_anakemas FROM '.$mbayar_table.' WHERE tahun_bayar = '.$tahun_sc.') ', 'IN');
}
else if($telahbayar_sc == 3)
{
	$whereArray[] = array('m_anakemas', 'flag_gantian', 1);
}

if($bayaranfasa_sc > 0)
{
	//$whereArray[] = array('m_anakemas', 'id_anakemas', ' (SELECT id_anakemas FROM '.$mbayar_table.' WHERE id_bayaranfasa = '.$bayaranfasa_sc.') ', 'IN');	
	$whereArray[] = array('m_bayaran_AE', 'id_bayaranfasa', $bayaranfasa_sc);
}

if($tahun_sah > 0)
{
	$whereArray[] = array('m_anakemas', 'tkh_sah', '\'%'.$tahun_sah.'%\'', 'LIKE');
}

//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++)
	{
		$start_index = 6;
		$id_anakemas = $temp_result['data'][$num][$start_index];	
		$id_dun = $temp_result['data'][$num][$start_index + 1];	
		$flag_saheft = $temp_result['data'][$num][$start_index + 2];	
		$ulasan_terima = $temp_result['data'][$num][$start_index + 3];	
		$ulasan_sah = $temp_result['data'][$num][$start_index + 4];	
		$id_statusmohon = $temp_result['data'][$num][$start_index + 5];	
		$id_pemohon = $temp_result['data'][$num][$start_index + 10];	
		
		$temp_result['data'][$num][1] = '<a href="javascript:;" onclick="loadMyModal(\'general-maklumat_sub.php?id='.$id_anakemas.'&program='.$id_program.'\', \'#ajax-modal\')" title="Maklumat Anak Emas">'.$temp_result['data'][$num][1].'</a>';
		
		$terima_btn = '';
		if($my_details->user_level <= 4)
		{
			$terima_btn = '<a class="btn btn-sm btn-default btn-text-primary btn-hover-primary btn-icon mr-2" onclick="loadMyModal(\'general-terima_tind_sub.php?id='.$id_anakemas.'&program='.$id_program.'\', \'#ajax-modal\')" title="Terima Dokumen"><span class="svg-icon svg-icon-primary svg-icon-2x">'.$Check_svg.'</span></a>';
		}
		
		$sah_btn = '';
		if($my_details->user_level == 1 || $my_details->user_level == 3 || $my_details->user_level == 5)
		{		
			$sah_btn = '<a class="btn btn-sm btn-default btn-text-primary btn-hover-primary btn-icon mr-2" onclick="loadMyModal(\'general-sah_tind_sub.php?id='.$id_anakemas.'&program='.$id_program.'\', \'#ajax-modal\')" title="Pengesahan Permohonan"><span class="svg-icon svg-icon-primary svg-icon-2x">'.$Check_svg.'</span></a>';
		}
		
		$sah_eft = '<a class="btn btn-sm btn-default btn-text-primary btn-hover-primary btn-icon mr-2" onclick="loadMyModal(\'general-sah_eft_sub.php?id='.$id_anakemas.'&program='.$id_program.'\', \'#ajax-modal\')" title="Pengesahan EFT"><span class="svg-icon svg-icon-primary svg-icon-2x">'.$Check_svg.'</span></a>';
		
		$log_btn = '<a class="btn btn-sm btn-default btn-text-primary btn-hover-primary btn-icon mr-2" onclick="loadMyModal(\'general-log_sub.php?id='.$id_anakemas.'\', \'#ajax-modal\')" title="Log Permohonan"><span class="svg-icon svg-icon-primary svg-icon-2x">'.$Log_svg.'</span></a>';
		
		$kemaskini_btn = '';
		if($my_details->user_level <= 3)
		{			
			$kemaskini_btn = '<a class="btn btn-sm btn-default btn-text-primary btn-hover-primary btn-icon mr-2" href="p05_borang.php?type=1&pemohon='.$id_pemohon.'" target="_blank" title="Kemaskini"><span class="svg-icon svg-icon-primary svg-icon-2x">'.$Edit_svg.'</span></a>';
		}
		
		//only show + button if single daerah, tahun & fasa
		$tambah_btn = '';
		if($telahbayar_sc == 1 && $my_details->user_level <= 5)
		{
			$tambah_btn = '<a class="btn btn-sm btn-default btn-text-primary btn-hover-primary btn-icon mr-2" id="tambahBtn_'.$id_anakemas.'" onclick="tambah(this, '.$id_anakemas.', 0)" title="Tambah"><span class="svg-icon svg-icon-success svg-icon-2x">'.$Plus_svg.'</span></a>';
		}
		$sah_eft = '';
		$temp_result['data'][$num][$start_index - 1] = $kemaskini_btn.$sah_eft.$log_btn.$tambah_btn;	
	}
	echo json_encode(
		$temp_result
	);
}
?>