<?php
include_once 'includes/processes.php';
$Process = new Login_Process;
$Process->check_status($_SERVER['SCRIPT_NAME']);

$logID = filter_var($_GET['log_id'], FILTER_SANITIZE_STRING);
$caseCategory = filter_var($_GET['case_category'], FILTER_SANITIZE_STRING);
$callerName = filter_var($_GET['caller_name'], FILTER_SANITIZE_STRING);
$callerPhone = filter_var($_GET['caller_phone'], FILTER_SANITIZE_STRING);
$callerEmail = filter_var($_GET['caller_email'], FILTER_SANITIZE_STRING);
$casePNR = filter_var($_GET['case_pnr'], FILTER_SANITIZE_STRING);
$caseDescription = filter_var($_GET['case_description'], FILTER_SANITIZE_STRING);
$caseComment = filter_var($_GET['case_comment'], FILTER_SANITIZE_STRING);
$caseDept = filter_var($_GET['case_dept'], FILTER_SANITIZE_STRING);

$caseData = $Process->getCaseInfo($logID);

if(empty($caseCategory)) {
	echo'<br /><div class="alert alert-danger" style="padding:5px"><i class="fa fa-times"></i> Error! select a case category</div>';
	exit;
}
if(empty($callerPhone)) {
	echo'<br /><div class="alert alert-danger" style="padding:5px"><i class="fa fa-times"></i> Error! Input caller phone number</div>';
	exit;
}
$dbLink = $Process->connect_db();
$sql  = "update apl_case_log set log_category = '$caseCategory', caller_name = '$callerName', caller_phone = '$callerPhone', caller_email = '$callerEmail', case_pnr = '$casePNR', case_description ='$caseDescription',
         log_status = 'Open' where log_id = '$logID'";
$runQ = $Process->RunQuery($sql,$dbLink);

if(!empty($caseComment)) {
	$query = "insert into apl_case_log_comment values(default,'$logID','$caseComment','".$_SESSION['userid']."','".time()."')";
	$runQ2 = $Process->RunQuery($query,$dbLink);
}

if($caseData->log_dept!=$caseDept) {
	$query3 = "update apl_case_log set log_dept = '$caseDept' where log_id = '$logID'";
	$runQ3 = $Process->RunQuery($query3,$dbLink);
	//insert into case assignment
	$query4 = "insert into apl_case_assignment values(default,'$logID','$caseDept','".$_SESSION['userid']."','0','".time()."')";
	$runQ4 = $Process->RunQuery($query4,$dbLink);
}

//comment line below to see sql built;
//echo $sql;
if($caseData->log_dept!=$caseDept) {

		$deptData = $Process->getDepartmentInfo($caseDept);
		$deptName = $deptData->dept_name;
		//send email to department staff
		$sendMail = $Process->sendEmailtoDeptMembers($caseDept,$logID);

	echo'<br /><div class="alert alert-success" style="padding:5px"><i class="fa fa-check"></i> Case saved and moved to <strong>'.$deptName.'</strong></div>';

}else{
	echo'<br /><div class="alert alert-success" style="padding:5px"><i class="fa fa-check"></i> Case saved</div>';
}

$dbLink = null;
?>
