<?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();
$maklumbalas = fetch_postget_val("maklumbalas", $_POST, $_GET);

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

try
{
	$PASS_COUNTER = 0; //COUNTER FOR DOUBLE CONFIRM
	$PASS_VALUE = 2; //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_aduan', $id, PDO::PARAM_STR);
	$indicator_Array[] = array('maklumbalas', $maklumbalas, PDO::PARAM_STR, 1);
	$indicator_Array[] = array('id_keyin', $id_pemohon, PDO::PARAM_INT);
	$indicator_Array[] = array('id_peranan', $my_details->user_level, PDO::PARAM_INT);
	$indicator_Array[] = array('tkh_keyin', $dateTimeNow, PDO::PARAM_STR);
	$PASS_COUNTER += PDO_INSERT($PDO, 'm_aduanmbls', $indicator_Array);
	$mbls_id = $PDO->lastInsertId();
	
	$indicator_Array2 = array();
	$indicator_Array2[] = array('id_aduanmbls', $mbls_id, PDO::PARAM_STR);
	$whereCondition = "WHERE id_aduan = ".$id;
	$PASS_COUNTER += PDO_UPDATE($PDO, 'm_aduan', $indicator_Array2, $whereCondition);

	//*****************************************
	//COMMIT if PASS_COUNTER == PASS_VALUE
	//*****************************************
	if ($PASS_COUNTER == $PASS_VALUE)
	{
		$PDO->commit();
		echo $PASS_COUNTER;
		
		$indicator_Query = "SELECT A.*, B.emel AS emel_pentadbir
			  FROM m_aduan A
			  LEFT JOIN t_pengguna B ON B.id_pengguna = A.id_assign
			  WHERE A.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);
		
		$assign_aduan = $aduan['id_assign'];
		$assign_emel = $aduan['emel_pentadbir'];
				
		if($my_details->user_level == 10)
		{
			//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 = $TITLEREPLY;
			
			$message = $SALAM;
			$message .= '<br><br>';
			$message .= 'Tajuk Aduan : <b>'.$aduan['tajuk'].'</b>.';
			$message .= '<br>';
			$message .= 'Maklumbalas terkini : <b>'.$maklumbalas.'</b>.';
			$message .= '<br><br>';
			$message .= $CONTENTREPLY;
			$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
				if($assign_aduan > 0 && $assign_emel != '')
				{
					$mail->AddAddress($assign_emel);								// Add another recipient					
				}
				else
				{
					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
		{						
			if($aduan['emel_pengadu'] != '')
			{			
				//EMAIL FUNCTION START				
				
				require_once('classes/php-mailer/PHPMailerAutoload.php');
				$to_email = $aduan['emel_pengadu'];
				$subject = $TITLEREPLY;
					
				$message = $SALAM.$aduan['nama_pengadu'];
				$message .= '<br><br>';
				$message .= 'Tajuk Aduan : <b>'.$aduan['tajuk'].'</b>.';
				$message .= '<br>';
				$message .= 'Maklumbalas terkini : <b>'.$maklumbalas.'</b>.';
				$message .= '<br><br>';
				$message .= $CONTENTREPLY;
				$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();
}

?>