<?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);
$dateTimeNow = dateTimeNow();

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();

	$indicator_Array = array();
	$indicator_Array[] = array('id_rumahorgtua', fetch_postget_val("id_rumahorgtua", $_POST, $_GET), PDO::PARAM_STR);
	$indicator_Array[] = array('ulasan_rumahorgtua', fetch_postget_val("ulasan_rumahorgtua", $_POST, $_GET), PDO::PARAM_STR,1);
	$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, 37, $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_rumahorgtua", $_POST, $_GET), '', '', $my_details->user_parlimen, $id_program);

	$kpbaru_waris = fetch_postget_val("kpbaru_waris", $_POST, $_GET);
	
	if($kpbaru_waris != '')
	{	
		$indicator_Query = "SELECT *
							FROM m_waris
							WHERE id_pemohon = ?
							AND status = 1";
		$PDO_stmt = $PDO->prepare($indicator_Query);
		$PDO_stmt->bindValue(1, $id, PDO::PARAM_INT);
		$PDO_stmt->execute();
		$waris_List = $PDO_stmt->fetch(PDO::FETCH_ASSOC);
		
		$id_waris = $waris_List['id_waris'];
		
		$waris_Array = array();
		$waris_Array[] = array('nama_waris', fetch_postget_val("nama_waris", $_POST, $_GET), PDO::PARAM_STR);
		$waris_Array[] = array('kpbaru_waris', fetch_postget_val("kpbaru_waris", $_POST, $_GET), PDO::PARAM_STR);
		$waris_Array[] = array('alamat_waris', fetch_postget_val("alamat_waris", $_POST, $_GET), PDO::PARAM_STR);
		$waris_Array[] = array('alamat_waris2', fetch_postget_val("alamat_waris2", $_POST, $_GET), PDO::PARAM_STR);
		$waris_Array[] = array('alamat_waris3', fetch_postget_val("alamat_waris3", $_POST, $_GET), PDO::PARAM_STR);
		$waris_Array[] = array('id_poskodwaris', fetch_postget_val("id_poskodwaris", $_POST, $_GET), PDO::PARAM_STR);
		$waris_Array[] = array('bandar_waris', fetch_postget_val("bandar_waris", $_POST, $_GET), PDO::PARAM_STR);
		$waris_Array[] = array('id_negeriwaris', fetch_postget_val("id_negeriwaris", $_POST, $_GET), PDO::PARAM_STR);
		$waris_Array[] = array('tel_hpwaris', fetch_postget_val("tel_hpwaris", $_POST, $_GET), PDO::PARAM_STR);
		$waris_Array[] = array('tel_rumahwaris', fetch_postget_val("tel_rumahwaris", $_POST, $_GET), PDO::PARAM_STR);
		$waris_Array[] = array('emel_waris', fetch_postget_val("emel_waris", $_POST, $_GET), PDO::PARAM_STR);
		$waris_Array[] = array('id_bankwaris', fetch_postget_val("id_bankwaris", $_POST, $_GET), PDO::PARAM_STR);
		$waris_Array[] = array('no_akaunwaris', fetch_postget_val("no_akaunwaris", $_POST, $_GET), PDO::PARAM_STR);
		
		if($id_waris > 0)
		{	
			if($waris_List['kpbaru_waris'] == $kpbaru_waris)
			{
				$PASS_VALUE += 1;
				$whereCondition = "WHERE id_waris = $id_waris";
				$PASS_COUNTER += PDO_UPDATE($PDO, 'm_waris', $waris_Array, $whereCondition);	
			}
			else
			{			
				$updatewaris_Array = array();
				$updatewaris_Array[] = array('status', 0, PDO::PARAM_STR);
				$PASS_VALUE += 1;
				$whereCondition = "WHERE id_waris = $id_waris";
				$PASS_COUNTER += PDO_UPDATE($PDO, 'm_waris', $updatewaris_Array, $whereCondition);	
				
				$waris_Array[] = array('status', 1, PDO::PARAM_STR);
				$waris_Array[] = array('id_pemohon', $id, PDO::PARAM_STR);
				$PASS_VALUE += 1;
				$PASS_COUNTER += PDO_INSERT($PDO, 'm_waris', $waris_Array);			
			}
		}
		else
		{
			$waris_Array[] = array('status', 1, PDO::PARAM_STR);
			$waris_Array[] = array('id_pemohon', $id, PDO::PARAM_STR);
			$PASS_VALUE += 1;
			$PASS_COUNTER += PDO_INSERT($PDO, 'm_waris', $waris_Array);
		}
	}

	//*****************************************
	//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();
}

?>