<?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_anakemas = fetch_postget_val("id", $_POST, $_GET);
$id_perakuan = fetch_postget_val("id_perakuan", $_POST, $_GET);
$dateTimeNow = dateTimeNow();

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

$id_pemohon = $pemohon_List['id_pemohon'];

$id_statusmohon = 3;
$ulasan_terima = '';
$log = 20;
if($id_perakuan == 2)
{
	$id_statusmohon = 1;
	$ulasan_terima = fetch_postget_val("ulasan_terima", $_POST, $_GET);
	$log = 21;	
}

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('ulasan_terima', $ulasan_terima, PDO::PARAM_STR);
	$indicator_Array[] = array('ulasan_sah', '', PDO::PARAM_STR);
	if($id_perakuan == 1)
	{
		$indicator_Array[] = array('id_terima', $my_details->usr_id, PDO::PARAM_INT);
		$indicator_Array[] = array('tkh_terima', formatDate(fetch_postget_val("tkh_terima", $_POST, $_GET), "Y-m-d"), PDO::PARAM_STR);
		$indicator_Array[] = array('tkh_terima_server', $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_anakemas = $id_anakemas";
	$PASS_COUNTER += PDO_UPDATE($PDO, 'm_anakemas', $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_terima, $id_anakemas, '', $my_details->user_parlimen, $pemohon_List['id_program']);

	$PASS_VALUE += 1; //NUMBER OF QUERIES = 1
	//*****************************************
	//DELETE ALL FROM m_dokumen
	//*****************************************
	$whereCondition = "WHERE id_anakemas = $id_anakemas";
	$PASS_COUNTER += PDO_DELETE($PDO, 'm_dokumen', $whereCondition);
	
	//*****************************************
	//NEW dokumen
	//*****************************************
	$allDokumen = explode(',', fetch_postget_val("dok", $_POST, $_GET));
	for($num = 0 ; $num < count($allDokumen) ; $num++)
	{
		if($allDokumen[$num] != '')
		{
			$dokumen_Array = array();
			$dokumen_Array[] = array('id_pemohon', $id_pemohon, PDO::PARAM_INT);
			$dokumen_Array[] = array('id_anakemas', $id_anakemas, PDO::PARAM_INT);
			$dokumen_Array[] = array('ref_dokumen', $allDokumen[$num], PDO::PARAM_INT);
			$dokumen_Array[] = array('id_keyin', $my_details->usr_id, PDO::PARAM_INT);
			$dokumen_Array[] = array('tkh_keyin', $dateTimeNow, PDO::PARAM_STR);

			$PASS_VALUE += 1; //NUMBER OF QUERIES = 1
			$PASS_COUNTER += PDO_INSERT($PDO, 'm_dokumen', $dokumen_Array);
		}
	}

	//*****************************************
	//COMMIT if PASS_COUNTER == PASS_VALUE
	//*****************************************
	if ($PASS_COUNTER == $PASS_VALUE)
	{
		$PDO->commit();
		echo $PASS_COUNTER;	
		
		$indicator_Query = "SELECT A.*
			  FROM m_pemohon A
			  WHERE A.id_pemohon = ?";
		$PDO_stmt = $PDO->prepare($indicator_Query); 
		$PDO_stmt->bindValue(1, $id_pemohon, PDO::PARAM_INT);
		$PDO_stmt->execute();
		$pemohon = $PDO_stmt->fetch(PDO::FETCH_ASSOC);
		
		if($id_perakuan == 2)
		{			
			//EMAIL FUNCTION START				
			
			require_once('classes/php-mailer/PHPMailerAutoload.php');
			$to_email = $pemohon['emel'];
			$subject = $TITLEQUERYPEMOHON;
				
			$message = $SALAM.$pemohon['nama_pemohon'].' (IBU/BAPA/PENJAGA kepada '.$pemohon_List['nama_anakemas'].' - '.$pemohon_List['kpbaru_anakemas'].')';
			$message .= '<br><br>';
			$message .= 'Maklumbalas permohonan anak emas: <b>'.$ulasan_terima.'</b>.';
			$message .= '<br><br>';
			$message .= $CONTENTQUERYPEMOHON;
			$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();
}

?> 

	





