<?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_assign = fetch_postget_val("id_assign", $_POST, $_GET);
$dateTimeNow = dateTimeNow();
		
$indicator_Query = "SELECT *
	  FROM m_aduan
	  WHERE id_aduan = ?";
$PDO_stmt = $PDO->prepare($indicator_Query); 
$PDO_stmt->bindValue(1, $id, PDO::PARAM_INT);
$PDO_stmt->execute();
$aduan = $PDO_stmt->fetch(PDO::FETCH_ASSOC);

$id_assignasal = $aduan['id_assign'];

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_assign', $id_assign, PDO::PARAM_STR);
	$indicator_Array[] = array('tkh_assign', $dateTimeNow, PDO::PARAM_STR);
	$PASS_VALUE += 1;
	$whereCondition = "WHERE id_aduan = ".$id;
	$PASS_COUNTER += PDO_UPDATE($PDO, 'm_aduan', $indicator_Array, $whereCondition);
	
	$PASS_VALUE += 1;
	$PASS_COUNTER += PDO_INSERT_ACTION_ASSIGN($PDO, $id, $id_assignasal, $id_assign, $my_details->usr_id, $dateTimeNow);

	//*****************************************
	//COMMIT if PASS_COUNTER == PASS_VALUE
	//*****************************************
	if ($PASS_COUNTER == $PASS_VALUE)
	{
		$PDO->commit();
		echo $PASS_COUNTER;		
			
		$indicator_Query = "SELECT *
			  FROM t_pengguna
			  WHERE id_pengguna = ?";
		$PDO_stmt = $PDO->prepare($indicator_Query);
		$PDO_stmt->bindValue(1, $id_assign, PDO::PARAM_INT);
		$PDO_stmt->execute();
		$result = $PDO_stmt->fetch(PDO::FETCH_ASSOC);
		
		if($result['emel'] != '')
		{			
			//EMAIL FUNCTION START				
			
			require_once('classes/php-mailer/PHPMailerAutoload.php');
			$to_email = $result['emel'];
			$subject = $TITLEASSIGN;
				
			$message = $SALAM.$result['nama'];
			$message .= '<br><br>';
			$message .= 'Tajuk Aduan : <b>'.$aduan['tajuk'].'</b>.';
			$message .= '<br><br>';
			$message .= $CONTENTASSIGN;
			$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();
}

?>