<?php

if($_COOKIE['sejahtera_role'] == 10)
{
	include_once ("includes/setup_pemohon.inc.php");
}
else
{
	include_once ("includes/setup.inc.php");
}
//Header("Content-type: application/xml");
Header("Cache-Control: no-cache");

$num_errors = 0;
$id_pemohon = fetch_postget_val("id", $_POST, $_GET);
$id_pengesahan = fetch_postget_val("id_pengesahan", $_POST, $_GET);
$id_perakuan = fetch_postget_val("id_perakuan", $_POST, $_GET);
$dateTimeNow = dateTimeNow();

//get program from pemohon
$indicator_Query = "SELECT *
					FROM m_pemohon
					WHERE id_pemohon = ?";
$PDO_stmt = $PDO->prepare($indicator_Query);
$PDO_stmt->bindValue(1, $id_pemohon, PDO::PARAM_INT);
$PDO_stmt->execute();
$pemohon_List = $PDO_stmt->fetch(PDO::FETCH_ASSOC);

$id_statusmohon = 4;
$ulasan_sah = '';
$log = 5;
$id_aktifmohon = 1;
$id_sah = $my_details->usr_id;
$tkh_sah = $dateTimeNow;

if($id_pengesahan == 2)
{
	$id_statusmohon = 2;
	$ulasan_sah = fetch_postget_val("ulasan_sah", $_POST, $_GET);
	$log = 7;	
	$id_aktifmohon = 1;
	$id_sah = 0;
	$tkh_sah = '0000-00-00 00:00:00';
}
else if($id_pengesahan == 3)
{
	$id_statusmohon = 5;
	$ulasan_sah = fetch_postget_val("ulasan_sah", $_POST, $_GET);
	$log = 6;	
	$id_aktifmohon = 2;
	$id_sah = 0;
	$tkh_sah = '0000-00-00 00:00:00';
}
else if($id_pengesahan == 4)
{
	$id_statusmohon = 6;
	$ulasan_sah = fetch_postget_val("ulasan_sah", $_POST, $_GET);
	$log = 8;	
	$id_aktifmohon = 2;
	$id_sah = 0;
	$tkh_sah = '0000-00-00 00:00:00';
}

if($id_perakuan == 1)
{
	$id_statusmohon = 4;
	$ulasan_saheft = '';
	$flag_saheft = 1;
	$log2 = 9;
}
else if($id_perakuan == 2)
{
	$id_statusmohon = 2;
	$ulasan_saheft = fetch_postget_val("ulasan_saheft", $_POST, $_GET);
	$flag_saheft = 0;
	$log2 = 12;	
}

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('id_statusmohon', $id_statusmohon, PDO::PARAM_INT);
	$indicator_Array[] = array('id_aktifmohon', $id_aktifmohon, PDO::PARAM_INT);
	$indicator_Array[] = array('tkh_aktifmohon', $dateTimeNow, PDO::PARAM_STR);
	$indicator_Array[] = array('ulasan_sah', $ulasan_sah, PDO::PARAM_STR);
	$indicator_Array[] = array('ulasan_saheft', $ulasan_saheft, PDO::PARAM_STR);
	$indicator_Array[] = array('flag_saheft', $flag_saheft, PDO::PARAM_INT);
	$indicator_Array[] = array('id_sah', $id_sah, PDO::PARAM_INT);
	$indicator_Array[] = array('tkh_sah', $tkh_sah, PDO::PARAM_STR);
	
	if($id_perakuan == 1)
	{
		$indicator_Array[] = array('id_saheft', $my_details->usr_id, PDO::PARAM_INT);
		$indicator_Array[] = array('tkh_saheft', $dateTimeNow, PDO::PARAM_STR);
	}
	
	$indicator_Array[] = array('id_kemaskini', $my_details->usr_id, PDO::PARAM_INT);
	$indicator_Array[] = array('tkh_kemaskini', $dateTimeNow, PDO::PARAM_STR);
	$whereCondition = "WHERE id_pemohon = $id_pemohon";
	$PASS_COUNTER += PDO_UPDATE($PDO, 'm_pemohon', $indicator_Array, $whereCondition);

	$PASS_VALUE += 1;
	$PASS_COUNTER += PDO_INSERT_ACTION_AUDIT($PDO, $id_pemohon, $log, $my_details->usr_id, $my_details->user_level, $dateTimeNow, $my_details->user_jab, $my_details->user_dun, $my_details->user_daerah, $ulasan_sah, '', '', $my_details->user_parlimen, $pemohon_List['id_program']);
	
	if($id_perakuan > 0)
	{
		$PASS_VALUE += 1;
		$PASS_COUNTER += PDO_INSERT_ACTION_AUDIT($PDO, $id_pemohon, $log2, $my_details->usr_id, $my_details->user_level, $dateTimeNow, $my_details->user_jab, $my_details->user_dun, $my_details->user_daerah, $ulasan_sah, '', '', $my_details->user_parlimen, $pemohon_List['id_program']);
	}

	//*****************************************
	//COMMIT if PASS_COUNTER == PASS_VALUE
	//*****************************************
	if ($PASS_COUNTER == $PASS_VALUE)
	{
		$PDO->commit();
		echo $PASS_COUNTER;
		
		$indicator_Query = "SELECT A.*, B.nama AS nama_pegawai, B.emel AS emel_pegawai, C.desk_program
			  FROM m_pemohon A
			  LEFT JOIN t_pengguna B ON B.id_pengguna = A.id_terima
			  LEFT JOIN i_program C ON C.id_program = A.id_program
			  WHERE A.id_pemohon = ?";
		$PDO_stmt = $PDO->prepare($indicator_Query); 
		$PDO_stmt->bindValue(1, $id_pemohon, PDO::PARAM_INT);
		$PDO_stmt->execute();
		$pegawai = $PDO_stmt->fetch(PDO::FETCH_ASSOC);
		
		if($id_pengesahan == 2 || $id_perakuan == 2)
		{			
			//EMAIL FUNCTION START				
			
			require_once('classes/php-mailer/PHPMailerAutoload.php');
			$to_email = $pegawai['emel_pegawai'];
			$subject = $TITLEQUERYPEGAWAI;
				
			$message = $SALAM.$pegawai['nama_pegawai'];
			$message .= '<br><br>';
			$message .= 'Pemohon : <b>'.$pegawai['nama_pemohon'].' ('.$pegawai['kpbaru_pemohon'].')</b>.';
			$message .= '<br>';
			$message .= 'Program : <b>'.$pegawai['desk_program'].'</b>.';
			$message .= '<br>';
			$message .= 'Maklumbalas permohonan : <b>'.$ulasan_sah.'</b>.';
			$message .= '<br><br>';
			$message .= $CONTENTQUERYPEGAWAI;
			$message .= '<br><br>';
			$message .= $TQ;
			$message .= '<br>';
			$message .= $SUK1;
			$message .= '<br>';
			$message .= $SUK2;
			
			$mail = new PHPMailer(true);
			try {

				$mail->IsSMTP();                    						// Set mailer to use SMTP
				$mail->Host = $SMTP_HOST;									// Specify main and backup server
				$mail->SMTPAuth = $SMTP_AUTH;								// Enable SMTP authentication
				$mail->Username = $SMTP_USER;								// SMTP username
				$mail->Password = $SMTP_PASS;								// SMTP password
				//$mail->SMTPSecure = 'tls';								// Enable encryption, 'ssl' also accepted
				$mail->Port = $SMTP_PORT;									// TCP port to connect to
				$mail->AddAddress($to_email);								// Add another recipient
				$mail->SetFrom($SMTP_FROM, $MAILFROM);

				$mail->IsHTML(true);										// Set email format to HTML
				$mail->CharSet = 'UTF-8';
				$mail->Subject = $subject;
				$mail->Body    = $message;
				$mail->Send();
				$arrResult = array ('response'=>'success');

			} 
			catch (phpmailerException $e) {
				$arrResult = array ('response'=>'error','errorMessage'=>$e->errorMessage());
			} 
			catch (Exception $e) {
				$arrResult = array ('response'=>'error','errorMessage'=>$e->getMessage());
			}	
			
			//EMAIL FUNCTION END			
		}	
	}
	else
	{
		$PDO->rollBack();
		echo -1;
	}
}
catch (Exception $e) 
{
	$PDO->rollBack();
	echo "Failed: " . $e->getMessage();
}

?> 

	





