<?php

include_once ("includes/setup.inc.php");
//Header("Content-type: application/xml");
Header("Cache-Control: no-cache");

$num_errors = 0;
$id = fetch_postget_val("id", $_POST, $_GET);
$id_program = fetch_postget_val("id_program", $_POST, $_GET);	
$id_aktifmohon = fetch_postget_val("id_aktifmohon", $_POST, $_GET);
$ulasan_aktifmohon = fetch_postget_val("ulasan_aktifmohon", $_POST, $_GET);
$dateTimeNow = dateTimeNow();

$indicator_Query = "SELECT A.*,B.desk_program,C.desk_statusmohon, D.*, E.desk_khairatkat, F.desk_sahmati, G.desk_bank, H.desk_aktifmohon, I.desk_statushidup
				  FROM m_pemohon A
				  LEFT JOIN i_program B ON B.id_program = A.id_program
				  LEFT JOIN i_statusmohon C ON C.id_statusmohon = A.id_statusmohon
				  LEFT JOIN m_waris D ON D.id_pemohon = A.id_pemohon
				  LEFT JOIN i_khairatkat E ON E.id_khairatkat = A.id_khairatkat
				  LEFT JOIN i_sahmati F ON F.id_sahmati = A.id_sahmati
				  LEFT JOIN i_bank G ON G.id_bank = D.id_bankwaris
				  LEFT JOIN i_aktifmohon H ON H.id_aktifmohon = A.id_aktifmohon
				  LEFT JOIN i_statushidup I ON I.id_statushidup = A.id_statushidup
				  WHERE A.id_pemohon = ?";
$PDO_stmt = $PDO->prepare($indicator_Query); 
$PDO_stmt->bindValue(1, $id, PDO::PARAM_INT);
$PDO_stmt->execute();
$info_List = $PDO_stmt->fetch(PDO::FETCH_ASSOC);

$id_aktifmohonasal = $info_List['id_aktifmohon'];
$id_statusmohon = $info_List['id_statusmohon'];

if($id_aktifmohon == $id_aktifmohonasal)
{
	echo -888;
	exit(0);
}

try
{
	$PASS_COUNTER = 0; //COUNTER FOR DOUBLE CONFIRM
	$PASS_VALUE = 0; //NUMBER OF QUERY TO EXECUTE. IMPORTANT FOR CHECKING!!

	//PDO INIT
	$PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	$PDO->beginTransaction();
	
	if($id_aktifmohon == 1 && $id_aktifmohonasal != 1)
	{
		$id_statusmohon = 1;
	}
	
	$log = 31;
	if($id_aktifmohon != 1 && $id_aktifmohonasal == 1)
	{
		$log = 30;
	}

	$indicator_Array = array();
	//$indicator_Array[] = array('id_statusmohon', $id_statusmohon, PDO::PARAM_STR);
	$indicator_Array[] = array('id_aktifmohon', $id_aktifmohon, PDO::PARAM_STR);
	$indicator_Array[] = array('tkh_aktifmohon', $dateTimeNow, PDO::PARAM_STR);
	$indicator_Array[] = array('ulasan_aktifmohon', $ulasan_aktifmohon, PDO::PARAM_STR);
	$indicator_Array[] = array('id_kemaskini', $my_details->usr_id, PDO::PARAM_INT);
	$indicator_Array[] = array('tkh_kemaskini', $dateTimeNow, PDO::PARAM_STR);
	$PASS_VALUE += 1;
	$whereCondition = "WHERE id_pemohon = $id";
	$PASS_COUNTER += PDO_UPDATE($PDO, 'm_pemohon', $indicator_Array, $whereCondition);
	
	$PASS_VALUE += 1;
	$PASS_COUNTER += PDO_INSERT_ACTION_AUDIT($PDO, $id, $log, $my_details->usr_id, $my_details->user_level, $dateTimeNow, $my_details->user_jab, $my_details->user_dun, $my_details->user_daerah, fetch_postget_val("ulasan_aktifmohon", $_POST, $_GET), '', '', $my_details->user_parlimen, $id_program);
	
	/*if($id_aktifmohon == 1 && $id_aktifmohonasal != 1)
	{
		$PASS_VALUE += 1;
		$PASS_COUNTER += PDO_INSERT_ACTION_AUDIT($PDO, $id, 31, $my_details->usr_id, $my_details->user_level, $dateTimeNow, $my_details->user_jab, $my_details->user_dun, $my_details->user_daerah, '', '', '', $my_details->user_parlimen, $id_program);
	}*/

	//*****************************************
	//COMMIT if PASS_COUNTER == PASS_VALUE
	//*****************************************
	if ($PASS_COUNTER == $PASS_VALUE)
	{
		$PDO->commit();
		echo $PASS_COUNTER;
	}
	else
	{
		$PDO->rollBack();
		echo -1;
	}
}
catch (Exception $e) 
{
	$PDO->rollBack();
	echo "Failed: " . $e->getMessage();
}

?>