<?php
/*
* date last modified 28-11-2017
* Edited by PI-Digi Logical Solutions
* HTML Files - add_user.html.php
* Mysql Tables - tbl_designation, tbl_user
*/
include_once('PHPMailer/class.phpmailer.php');
require_once('./classes/class.noseo_pagination.php'); # pagination class
include('includes/functions.php'); # general functions


$debug = true;

if(isset($_REQUEST['p']) && $_REQUEST['p']!=''){
	$p = trim($_REQUEST['p']);
} else {
	$p = 1;
}

$pageVars = '';

$categoryRs = mysqli_query($con, "SELECT * FROM tbl_service_categories WHERE parent_id = '0'");


if(isset($_REQUEST['id']) && $_REQUEST['id']!=''){		
	$editId = mysqli_real_escape_string($con, trim($_REQUEST['id']));	
		
	//if assign action
	if(isset($_REQUEST['technician_id']) && $_REQUEST['technician_id']!=''){
		$techId = mysqli_real_escape_string($con, trim($_REQUEST['technician_id']));
		if(isset($_REQUEST['action']) && $_REQUEST['action']=='assign'){
			//assign the technician to this task
			$update = mysqli_query($con, "UPDATE tbl_job SET technician_id = '".$techId."', status = 'waiting' WHERE id = '".$editId."'");
			if($update){
				//get user and tech details
				$taskRec = mysqli_fetch_array(mysqli_query($con, "SELECT user_id FROM tbl_job WHERE id = '".$editId."'"));
				$userName = getUserName($taskRec['user_id']);
				
				$techRec = mysqli_fetch_array(mysqli_query($con, "SELECT name, email FROM tbl_technician WHERE id = '".$techId."'"));
				$techName = $techRec['name'];
				$techEmail = $techRec['email'];
				
				//send mail to technician
				//get from email address
				$settingsRec = mysqli_fetch_array(mysqli_query($con, "SELECT from_email FROM tbl_settings WHERE id = '1'"));
				$fromEmail = $settingsRec['from_email'];
				$mail = new PHPMailer();
				$mail->From = $fromEmail;
				$mail->FromName = SITE_NAME;
				$mail->IsHTML(true);	
				$mail->Subject = SITE_NAME.' New task';	
		
				
				$mail->AddAddress($techEmail, $techName);					
				//$to_name = $name;			
		
						$email_content = '<table width="600" border="0" align="center" cellpadding="0" cellspacing="0">
					  <tr>
						<td height="68" align="left" valign="top"><a href="'.BASE_URL.'" target="_blank"><img src="'.BASE_URL.'images/logo.png" /></a></td>
						<td align="right" valign="bottom" style="color:#ff8d07;">&nbsp;</td>
					  </tr>
					   <tr>
						<td height="5" colspan="2" align="left" valign="top"></td>
					  </tr>
					  <tr>
						<td height="49" colspan="2" align="left" valign="top" bgcolor="#970198" style="padding:10px 0px 0px 20px; color:#FFFFFF; font-size:24px; font-family:Times New Roman, Times,   serif;">New task from user</td>
					  </tr>
					  <tr>
						<td colspan="2" align="left" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0" style="border:#970198 solid 1px;">
						  <tr>
							<td height="4" colspan="4" align="left" valign="top" bgcolor="#970198"></td>
						  </tr>
						  <tr>
							<td height="8" colspan="4" align="left" valign="top"></td>
						  </tr>
							<tr>
							<td width="258" align="left" valign="top" style="color:#4e4e4e; font-size:13px; line-height:16px; padding:0px 0px 0px 9px;">Hello  '. $techName .',</td>
						  </tr>
							  <tr>
								<td height="8" align="left" valign="top" style="color:#4e4e4e; font-size:11px;  padding:0px 0px 0px 9px;"></td>
							</tr>
						  <tr>
							<td height="10" colspan="4" align="left" valign="top"></td>
						  </tr>
						  <tr>
							<td colspan="4" align="left" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
							  <tr>
								<td width="8" align="left" valign="top">&nbsp;</td>
								<td width="581" height="140" align="left" valign="top" bgcolor="#f0f0f0" style="border:#c9ccc5 solid 1px; line-height:18px; padding:10px 0px 0px 8px; font-size:11px; font-weight:normal; line-height:27px;">
									<p></p>
									<p><b>'.SITE_NAME.' assigned you in a task from the user '.$userName.'<b></p>
									<p>Task ID: #'.$editId.'</p>
									<p></p>
									<p></p>
									<p></p>
								  </td>
								<td width="7" align="left" valign="top">&nbsp;</td>
							  </tr>
							</table></td>
						  </tr><tr>
							<td colspan="4" align="left" valign="top">&nbsp;</td>
						  </tr>
						</table></td>
					  </tr>
					</table>';
				$mail->Body = $email_content;
				$mail->Send();	
				
				$success = 'Task sent to technician';
								
				$exec_token = mysqli_query($con, 'SELECT fcm_token, device FROM tbl_fcm_tokens WHERE tech_id='.$techId);
				if(isset($exec_token) && mysqli_num_rows($exec_token)>0) {
					while($res_token = mysqli_fetch_array($exec_token)) {	
						if($res_token['fcm_token']!='') {							
							$title = SITE_NAME.' assigned you in a task from the user '.$userName;
							$body = 'Task ID: #'.$editId;
							if($res_token['device']=='android') {								
								sendGCM($title, $body, $res_token['fcm_token'], "tech");
							} else {
								sendIosTechPush($title, $body, $res_token['fcm_token'], "tech");
							}					
						}
					}
				}
				
				
			} else {
				$error = 'Something went wrong';
			}
		} 

					
	}		

	if(isset($success) && $success!=''){
		header('Location:index.php?page=view_task_project&id='.$editId.'&p='.$p.'&success='.$success);
		exit();
	}



	$editRs = mysqli_query($con, "SELECT * FROM tbl_job WHERE id = '".$editId."'");
	if(mysqli_num_rows($editRs)>0){
		$resEdit = mysqli_fetch_array($editRs);		
						
	} else {
		$error = 'Project does not exist!';
	}

}   

?>