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

if($id == 0)
{
	$indicator_Query = "SELECT t_pengguna.*
					  FROM t_pengguna
					  WHERE login = ?";
	$PDO_stmt = $PDO->prepare($indicator_Query);
	$PDO_stmt->bindValue(1, fetch_postget_val("pengguna_login", $_POST, $_GET), PDO::PARAM_STR);
	$PDO_stmt->execute();
	$indicator_List = $PDO_stmt->fetch(PDO::FETCH_ASSOC);
	
	if($indicator_List['id_pengguna'] > 0)
	{
		echo -888;
		exit(0);
	}
}

try
{
	$PASS_COUNTER = 0; //COUNTER FOR DOUBLE CONFIRM
	$PASS_VALUE = 1; //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('nama', fetch_postget_val("nama", $_POST, $_GET), PDO::PARAM_STR);
	$indicator_Array[] = array('no_tel', fetch_postget_val("no_tel", $_POST, $_GET), PDO::PARAM_STR);
	$indicator_Array[] = array('emel', fetch_postget_val("emel", $_POST, $_GET), PDO::PARAM_STR,1);
	$indicator_Array[] = array('id_jabatan', fetch_postget_val("id_jabatan", $_POST, $_GET), PDO::PARAM_INT);
	$indicator_Array[] = array('id_peranan', fetch_postget_val("id_peranan", $_POST, $_GET), PDO::PARAM_INT);
	$indicator_Array[] = array('id_parlimen', fetch_postget_val("id_parlimen", $_POST, $_GET), PDO::PARAM_INT);
	$indicator_Array[] = array('id_dun', fetch_postget_val("id_dun", $_POST, $_GET), PDO::PARAM_INT);
	$indicator_Array[] = array('id_daerah', fetch_postget_val("id_daerah", $_POST, $_GET), PDO::PARAM_INT);
	$indicator_Array[] = array('catatan', fetch_postget_val("catatan", $_POST, $_GET), PDO::PARAM_STR,1);

	if($id == 0)
	{
		//*****************************************
		//NEW t_master_pengguna
		//*****************************************
		
		if($my_details->user_level == 1 || $my_details->user_level == 3)
		{
			$indicator_Array[] = array('aktif', 1, PDO::PARAM_INT);
		}
		$indicator_Array[] = array('login', fetch_postget_val("pengguna_login", $_POST, $_GET), PDO::PARAM_STR);
		$indicator_Array[] = array('kpbaru', fetch_postget_val("pengguna_login", $_POST, $_GET), PDO::PARAM_STR);
		$indicator_Array[] = array('katalaluan', sha1(fetch_postget_val("pengguna_pwd", $_POST, $_GET)), PDO::PARAM_STR,1);
		$indicator_Array[] = array('id_cipta', $my_details->usr_id, PDO::PARAM_INT);
		$indicator_Array[] = array('tkh_cipta', $dateTimeNow, PDO::PARAM_STR);

		$PASS_COUNTER += PDO_INSERT($PDO, 't_pengguna', $indicator_Array);
	}
	else
	{
		//*****************************************
		//UPDATE t_master_pengguna
		//*****************************************
		if($my_details->user_level == 1 || $my_details->user_level == 3)
		{
			$indicator_Array[] = array('aktif', fetch_postget_val("aktif", $_POST, $_GET), PDO::PARAM_INT);
		}
		$indicator_Array[] = array('id_kemaskini', $my_details->usr_id, PDO::PARAM_INT);
		$indicator_Array[] = array('tkh_kemaskini', $dateTimeNow, PDO::PARAM_STR);
		$whereCondition = "WHERE id_pengguna = $id";
		//$PASS_COUNTER += PDO_UPDATE($PDO, 't_pengguna', $indicator_Array, $whereCondition);		
		$PASS_COUNTER += PDO_UPDATE_INDIKATOR_AUDIT($PDO, 't_pengguna', $id, $indicator_Array, $whereCondition, $my_details->usr_id, $my_details->user_level, $dateTimeNow);
	}

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

?>