<?php

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

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

$indicator_Query = "SELECT *
	  FROM t_pemohon
	  WHERE id_pemohon = ?";
$PDO_stmt = $PDO->prepare($indicator_Query); 
$PDO_stmt->bindValue(1, $my_details->usr_id, PDO::PARAM_INT);
$PDO_stmt->execute();
$indicator_List = $PDO_stmt->fetch(PDO::FETCH_ASSOC);

$id_pemohon = $my_details->usr_id;
if($my_details->user_level == 10)
{
	$id_pemohon = $my_details->usr_id;	
}

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_pemohon', $id_pemohon, PDO::PARAM_STR);
	$indicator_Array[] = array('nama_pengadu', $indicator_List['nama'], PDO::PARAM_STR);
	$indicator_Array[] = array('kpbaru_pengadu', $indicator_List['login'], PDO::PARAM_STR);
	$indicator_Array[] = array('emel_pengadu', $indicator_List['emel'], PDO::PARAM_STR,1);
	$indicator_Array[] = array('tel_pengadu', $indicator_List['no_tel'], PDO::PARAM_STR);
	$indicator_Array[] = array('tajuk', fetch_postget_val("tajuk", $_POST, $_GET), PDO::PARAM_STR);
	$indicator_Array[] = array('laporan', fetch_postget_val("laporan", $_POST, $_GET), PDO::PARAM_STR,1);
	$indicator_Array[] = array('id_program', fetch_postget_val("id_program", $_POST, $_GET), PDO::PARAM_STR);
	$indicator_Array[] = array('id_dun', fetch_postget_val("id_dun", $_POST, $_GET), PDO::PARAM_STR);
	$indicator_Array[] = array('id_jenisaduan', fetch_postget_val("id_jenisaduan", $_POST, $_GET), PDO::PARAM_STR);
	$indicator_Array[] = array('id_statusaduan', 1, PDO::PARAM_INT);
	$indicator_Array[] = array('id_keyin', $id_pemohon, PDO::PARAM_INT);
	$indicator_Array[] = array('tkh_keyin', $dateTimeNow, PDO::PARAM_STR);
	$PASS_COUNTER += PDO_INSERT($PDO, 'm_aduan', $indicator_Array);

	//*****************************************
	//COMMIT if PASS_COUNTER == PASS_VALUE
	//*****************************************
	if ($PASS_COUNTER == $PASS_VALUE)
	{
		$PDO->commit();
		echo $PASS_COUNTER;
				
		//EMAIL TO SELF START 
		
		require_once('classes/php-mailer/PHPMailerAutoload.php');
		$to_email = $indicator_List['emel'];
		$subject = $TITLNEWSELF;
		
		$message = $SALAM;
		$message .= '<br><br>';
		$message .= 'Tajuk Aduan : <b>'.fetch_postget_val("tajuk", $_POST, $_GET).'</b>.';
		$message .= '<br><br>';
		$message .= $CONTENTNEWSELF;
		$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 SELF FUNCTION END	
				
		//EMAIL TO OFFICER FUNCTION START 
		
		$indicator_Query = "SELECT *
			  FROM t_pengguna
			  WHERE id_peranan IN (1,3) 
			  AND aktif = 1";
		$PDO_stmt = $PDO->prepare($indicator_Query);
		$PDO_stmt->execute();
		
		$resultadmin = array();
		while($res = $PDO_stmt->fetch(PDO::FETCH_ASSOC))
		{
			$resultadmin[] = $res; 
		}
		
		require_once('classes/php-mailer/PHPMailerAutoload.php');
		$subject = $TITLNEW;
		
		$message = $SALAM;
		$message .= '<br><br>';
		$message .= 'Tajuk Aduan : <b>'.fetch_postget_val("tajuk", $_POST, $_GET).'</b>.';
		$message .= '<br><br>';
		$message .= $CONTENTNEW;
		$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
			for($num = 0; $num < count($resultadmin); $num++)
			{
				if($resultadmin[$num]['emel'] != '')
				{
					//$mail->AddAddress($resultadmin[$num]['emel']);			// Add another recipient
					$mail->AddBCC($resultadmin[$num]['emel']);			// 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 OFFICER FUNCTION END	
	}
	else
	{
		$PDO->rollBack();
		echo -1;
	}
}
catch (Exception $e) 
{
	$PDO->rollBack();
	echo "Failed: " . $e->getMessage();
}

?>