<?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);
$poskod_sc = fetch_postget_val('poskod_sc',$_POST, $_GET);
$caramohon_sc = fetch_postget_val('caramohon_sc',$_POST, $_GET);
$carabayar_sc = fetch_postget_val('carabayar_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);
$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;
}

if ($flow_type == 1)
{
	$id_statusmohon = 2;
}
else if ($flow_type == 2)
{
	$id_statusmohon = 3;
}
else if ($flow_type == 3)
{
	$id_statusmohon = 4;
}

//*****************************************************
// 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_pemohon',
	'primary_key' => 'id_pemohon',
	'sub' => array(
		array('name' => 'i_caramohon', 'sub_id' => 'id_caramohon', 'main_id' => 'id_caramohon'),
		array('name' => 'i_carabayar', 'sub_id' => 'id_carabayar', 'main_id' => 'id_carabayar'),
		array('name' => 'i_statushidup', 'sub_id' => 'id_statushidup', 'main_id' => 'id_statushidup'),
		array('name' => 'i_statusmohon', 'sub_id' => 'id_statusmohon', 'main_id' => 'id_statusmohon'),
		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
//*****************************************************
if($flow_type == 1)
{
	$columns = array(
		array( 'bil_col' => '', 'dt' => 0),
		array( 'db' => 'nama_pemohon', 'dt' => 1, 'table' => 'm_pemohon', 'print'=>1  ),
		array( 'db' => 'kpbaru_pemohon', 'dt' => 2, 'table' => 'm_pemohon', 'print'=>1  ),
		array( 'db' => 'tkh_daftar', 'dt' => 3, 'table' => 'm_pemohon', 'print'=>1  ),
		array( 'db' => 'desk_caramohon', 'dt' => 4, 'table' => 'i_caramohon', 'print'=>1  ),
		array( 'dt' => 5, 'user_defined' => ''),
		array( 'db' => 'id_pemohon', 'dt' => 6, 'table' => 'm_pemohon' ),
		array( 'db' => 'id_dun', 'dt' => 7, 'table' => 'm_pemohon' ),
		array( 'db' => 'id_carabayar', 'dt' => 8, 'table' => 'm_pemohon' ),
		array( 'db' => 'id_caramohon', 'dt' => 9, 'table' => 'm_pemohon' ),
		array( 'db' => 'flag_saheft', 'dt' => 10, 'table' => 'm_pemohon' ),
		array( 'db' => 'id_statushidup', 'dt' => 11, 'table' => 'm_pemohon' ),
		array( 'db' => 'ulasan_terima', 'dt' => 12, 'table' => 'm_pemohon' ),
		array( 'db' => 'ulasan_sah', 'dt' => 13, 'table' => 'm_pemohon' ),
		array( 'db' => 'id_statusmohon', 'dt' => 14, 'table' => 'm_pemohon' ),
	);
}
else if($flow_type == 2)
{
	$columns = array(
		array( 'bil_col' => '', 'dt' => 0),
		array( 'db' => 'nama_pemohon', 'dt' => 1, 'table' => 'm_pemohon', 'print'=>1 ),
		array( 'db' => 'kpbaru_pemohon', 'dt' => 2, 'table' => 'm_pemohon', 'print'=>1 ),
		array( 'db' => 'tkh_daftar', 'dt' => 3, 'table' => 'm_pemohon', 'print'=>1 ),
		array( 'db' => 'desk_caramohon', 'dt' => 4, 'table' => 'i_caramohon', 'print'=>1 ),
		array( 'dt' => 5, 'user_defined' => ''),
		array( 'db' => 'desk_carabayar', 'dt' => 6, 'table' => 'i_carabayar', 'print'=>1),
		array( 'db' => 'desk_dun', 'dt' => 7, 'table' => 'i_dun', 'print'=>1),
		array( 'db' => 'desk_parlimen', 'dt' => 8, 'table' => 'i_parlimen', 'print'=>1),
		array( 'db' => 'id_pemohon', 'dt' => 9, 'table' => 'm_pemohon' ),
		array( 'db' => 'id_dun', 'dt' => 10, 'table' => 'm_pemohon' ),
		array( 'db' => 'id_carabayar', 'dt' => 11, 'table' => 'm_pemohon' ),
		array( 'db' => 'id_caramohon', 'dt' => 12, 'table' => 'm_pemohon' ),
		array( 'db' => 'flag_saheft', 'dt' => 13, 'table' => 'm_pemohon' ),
		array( 'db' => 'id_statushidup', 'dt' => 14, 'table' => 'm_pemohon' ),
		array( 'db' => 'ulasan_terima', 'dt' => 15, 'table' => 'm_pemohon' ),
		array( 'db' => 'ulasan_sah', 'dt' => 16, 'table' => 'm_pemohon' ),
		array( 'db' => 'id_statusmohon', 'dt' => 17, 'table' => 'm_pemohon' ),
	);
}
else if($flow_type == 3)
{
	$columns = array(
		array( 'bil_col' => '', 'dt' => 0),
		array( 'db' => 'nama_pemohon', 'dt' => 1, 'table' => 'm_pemohon' , 'print'=>1 ),
		array( 'db' => 'kpbaru_pemohon', 'dt' => 2, 'table' => 'm_pemohon' , 'print'=>1 ),
		array( 'db' => 'tkh_daftar', 'dt' => 3, 'table' => 'm_pemohon' , 'print'=>1 ),
		array( 'db' => 'desk_caramohon', 'dt' => 4, 'table' => 'i_caramohon' , 'print'=>1 ),
		array( 'db' => 'desk_carabayar', 'dt' => 5, 'table' => 'i_carabayar' , 'print'=>1 ),
		array( 'dt' => 6, 'user_defined' => ''),
		array( 'db' => 'id_pemohon', 'dt' => 7, 'table' => 'm_pemohon' ),
		array( 'db' => 'id_dun', 'dt' => 8, 'table' => 'm_pemohon' ),
		array( 'db' => 'id_carabayar', 'dt' => 9, 'table' => 'm_pemohon' ),
		array( 'db' => 'id_caramohon', 'dt' => 10, 'table' => 'm_pemohon' ),
		array( 'db' => 'flag_saheft', 'dt' => 11, 'table' => 'm_pemohon' ),
		array( 'db' => 'id_statushidup', 'dt' => 12, 'table' => 'm_pemohon' ),
		array( 'db' => 'ulasan_terima', 'dt' => 13, 'table' => 'm_pemohon' ),
		array( 'db' => 'ulasan_sah', 'dt' => 14, 'table' => 'm_pemohon' ),
		array( 'db' => 'id_statusmohon', 'dt' => 15, 'table' => 'm_pemohon' ),
	);
}

//*****************************************************
// WHERE SQL
// Array ($table_name, $column_name, $value, $symbol(default : '='))
//*****************************************************
$whereArray = array(
	array('m_pemohon', 'id_program', $id_program),
	array('m_pemohon', 'id_statusmohon', $id_statusmohon),
	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)
{
	if($id_program == 4)
	{
		$whereArray[] = array('m_pemohon', 'id_daerahOKU', $daerah_sc);
	}
	else
	{
		$whereArray[] = array('i_daerah', 'id_daerah', $daerah_sc);
	}
}

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

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

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

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

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

//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][count($columns) - 9];	
		$id_dun = $temp_result['data'][$num][count($columns) - 8];	
		$id_carabayar = $temp_result['data'][$num][count($columns) - 7];	
		$id_caramohon = $temp_result['data'][$num][count($columns) - 6];	
		$flag_saheft = $temp_result['data'][$num][count($columns) - 5];	
		$id_statushidup = $temp_result['data'][$num][count($columns) - 4];	
		$ulasan_terima = $temp_result['data'][$num][count($columns) - 3];	
		$ulasan_sah = $temp_result['data'][$num][count($columns) - 2];	
		$id_statusmohon = $temp_result['data'][$num][count($columns) - 1];	
		
		$temp_result['data'][$num][1] = '<a href="javascript:;" onclick="loadMyModal(\'general-maklumat.php?id='.$id_pemohon.'&program='.$id_program.'\', \'#ajax-modal\')" title="Maklumat Pemohon">'.$temp_result['data'][$num][1].'</a>';
		
		$terima_btn = '';
		if($my_details->user_level <= 4 || $my_details->user_level == 6 || $my_details->user_level == 15 || $my_details->user_level == 8)
		{
			$terima_btn = '<a class="btn btn-sm btn-default btn-text-primary btn-hover-primary btn-icon mr-2" onclick="loadMyModal(\'general-terima_tind.php?id='.$id_pemohon.'&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 || $my_details->user_level == 7 || $my_details->user_level == 16 || $my_details->user_level == 9)
		{
			$sah_btn = '<a class="btn btn-sm btn-default btn-text-primary btn-hover-primary btn-icon mr-2" onclick="loadMyModal(\'general-sah_tind.php?id='.$id_pemohon.'&program='.$id_program.'\', \'#ajax-modal\')" title="Pengesahan Permohonan"><span class="svg-icon svg-icon-primary svg-icon-2x">'.$Check_svg.'</span></a>';
		}
		
		$sah_eft = '';
		if($my_details->user_level <= 5)
		{
			$sah_eft = '<a class="btn btn-sm btn-default btn-text-primary btn-hover-primary btn-icon mr-2" onclick="loadMyModal(\'general-sah_eft.php?id='.$id_pemohon.'&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.php?id='.$id_pemohon.'\', \'#ajax-modal\')" title="Log Permohonan"><span class="svg-icon svg-icon-primary svg-icon-2x">'.$Log_svg.'</span></a>';
		
		$papar_btn = '<a class="btn btn-sm btn-default btn-text-primary btn-hover-primary btn-icon mr-2" onclick="paparBorang('.$id_pemohon.', \''.$name_id_program.'\')" title="Papar"><span class="svg-icon svg-icon-primary svg-icon-2x">'.$File_svg.'</span></a>';
		
		$kemaskini_btn = '<a class="btn btn-sm btn-default btn-text-primary btn-hover-primary btn-icon mr-2" onclick="editBorang('.$id_pemohon.', \''.$name_id_program.'\')" title="Kemaskini"><span class="svg-icon svg-icon-primary svg-icon-2x">'.$Edit_svg.'</span></a>';
		
		if($flow_type == 1)
		{
			if($ulasan_sah == '')
			{
				//$kemaskini_btn = '';				
			}
			$temp_result['data'][$num][count($columns) - 10] = $terima_btn.$log_btn.$kemaskini_btn;	
		}
		else if($flow_type == 2)
		{
			$temp_result['data'][$num][count($columns) - 13] = $sah_btn.$log_btn;	
		}
		else if($flow_type == 3)
		{
			if($id_carabayar == 2 && $flag_saheft == 0)
			{
				$sah_eft = $sah_eft;	
				$paparcarabayar = $temp_result['data'][$num][5].'<br><b>BELUM DISAHKAN</b>';
			}
			else if($id_carabayar == 2 && $flag_saheft == 1)
			{				
				$sah_eft = '';
				$paparcarabayar = $temp_result['data'][$num][5].'<br>TELAH DISAHKAN';
			}
			else if($id_carabayar == 1)
			{				
				$sah_eft = '';
				$paparcarabayar = $temp_result['data'][$num][5];
			}
			$temp_result['data'][$num][count($columns) - 10] = $sah_eft.$log_btn.$kemaskini_btn;	
			
			$temp_result['data'][$num][5] = $paparcarabayar;
		}
	}
	echo json_encode(
		$temp_result
	);
}
?>