<?php
ob_clean();
include("../../../include/startup.php"); 
if(isset($_REQUEST['action']) && $_REQUEST['action']=='get_admin_by_job'){
	$job_id = $_REQUEST['job_id'];
	$admin_ids = "";
	$sql_for_admin = "SELECT * FROM job_admin_mapping WHERE job_id = '$job_id' ";
	$rs_for_admin = mysqli_query($conn,$sql_for_admin);
	if(mysqli_num_rows($rs_for_admin) > 0){
		while($row_for_admin = mysqli_fetch_assoc($rs_for_admin)){
			$admin_ids .= $row_for_admin["admin_id"].",";
		}
		$admin_ids = substr($admin_ids,0,-1);
	}
	if(isset($admin_ids) && $admin_ids !=""){
		print Admin::getAdminList("admin_id",$admin_id,"100%",true,"form-control","required",$admin_ids); 
	}else{
		echo "<span style='color:red;' id='span'>No consultant assign to this job. First assign consultant.</span>";
		//print Admin::getAdminList("admin_id",$admin_id,"100%",false,"form-control","required",999999); 
	}
	die;
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='get_address_by_job'){
	$job_id = $_REQUEST['job_id'];
	echo $krscrm->getAddressByJobId($job_id);
	die;
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='get_descrip_by_job'){
	$job_id = $_REQUEST['job_id'];
	echo $krscrm->getDescripByJobId($job_id);
	die;
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='upload_document'){
	//$job_id = $_REQUEST["job_id"];
	$returnArray = array();
	$uploadfileArray = array();
	$i = 0;
	$j = 0;
	include("resize-class.php");
	foreach($_FILES['uploadfile']['name'] as $key => $uploadfile){
		if(isset($uploadfile) && $uploadfile !=""){
			$path = $uploadfile;
			$ext = pathinfo($path, PATHINFO_EXTENSION);
			if($ext == "jpg" || $ext == "jpeg" || $ext == "png" || $ext == "pdf" || $ext == "doc" || $ext == "docx" || $ext == "xls" || $ext == "xlsx" || $ext == "csv"){
				$i++;
				
				$digits = 5;
				$random_number = rand(pow(10, $digits-1), pow(10, $digits)-1);
				$uploaddir = '../document/';
				$image_name_array = explode(".",$uploadfile);
				$new_uploadfile_name = $image_name_array[0]."_".$random_number.".".$image_name_array[1];
				$uploadfile = $uploaddir . $new_uploadfile_name;
				move_uploaded_file($_FILES['uploadfile']['tmp_name'][$key], $uploadfile);
				array_push($uploadfileArray,$new_uploadfile_name);
				################     Resize image to 200x 200 START ####################
								
					list($width, $height, $type, $attr) = getimagesize($uploadfile);			
					$resizeObj = new resize($uploadfile);			
					$resizeObj -> resizeImage(200, 200, 'crop');			
					//$resizeObj -> resizeImage(200, 200);			
					$resizeObj -> saveImage($uploadfile, 100);
					
				################     Resize image to 200x 200 END HERE ####################
			}else{
				$j++;
			}
		}
	}
	if($i > 0){
		if(isset($_SESSION['uploadfileArray'])){
			$_SESSION['uploadfileArray'] = array_merge($uploadfileArray,$_SESSION['uploadfileArray']);
		}else{
			$_SESSION['uploadfileArray'] = $uploadfileArray;
		}
		$returnArray['uploadfileArray']	= $uploadfileArray;
		$returnArray['status'] = "success";
		$returnArray['message']	= "$i Document(s) added sucessfully";
		$returnArray['error_message'] = "$j Document(s) not uploaded. Please upload only png/jpg/pdf or docx document";
	}else{
		$returnArray['status'] = "failure";
	}
	echo json_encode($returnArray);	
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='upload_image'){
	//$job_id = $_REQUEST["job_id"];
	$returnArray = array();
	$uploadfileArray = array();
	$i = 0;
	$j = 0;
	$orientation = $_REQUEST['orientation'];
	 ini_set("display_errors", 0);

	if(isset($_FILES['file']['name']) && $_FILES['file']['name'] !=""){
		$i++;		
		$digits = 5;
		$random_number = rand(pow(10, $digits-1), pow(10, $digits)-1);
		$uploaddir = '../document/';
		$image_name_array = explode(".",$_FILES['file']['name']);
		$new_uploadfile_name = $image_name_array[0]."_".$random_number.".".$image_name_array[1];
		$uploadfile = $uploaddir . $new_uploadfile_name;
		move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile);
		
		$path = $_FILES['file']['name'];
		$ext = pathinfo($path, PATHINFO_EXTENSION);
		
		if($ext == "jpg" or $ext == "jpeg"){
			$img_res = imagecreatefromjpeg($uploadfile);			
		}
		if($ext == "gif"){
			$img_res = imagecreatefromgif($uploadfile);
		}
		if($ext == "png"){
			$img_res = imagecreatefrompng($uploadfile);
		}
		if($orientation!=1){
			switch($orientation) {
				case 1:
				return true;
				
				// Flipped on the horizontal axis (might do it at some point in the future)
				case 2:
				  //By @kormanowsky: imageflip() returns TRUE or FALSE so it's wrong to assign its return value to $final_img
				  imageflip($img_res, IMG_FLIP_HORIZONTAL);
				break;
				
				// Turned 180 deg
				case 3:
				  //imageflip($img_res, IMG_FLIP_BOTH);
				   $img_res = imagerotate($img_res, 180, 0); 
				break;
				
				// Upside-Down
				case 4:
				  imageflip($img_res, IMG_FLIP_VERTICAL);
				break;
				
				// Turned 90 deg to the left and flipped
				case 5:
				  imageflip($img_res, IMG_FLIP_VERTICAL);
				// Turned 90 deg to the left
				case 6:
				  //$img_res = imagerotate($img_res, -90, 0);
				break;
				
				// Turned 90 deg to the right and flipped
				case 7:
				  imageflip($img_res, IMG_FLIP_VERTICAL);
				// Turned 90 deg to the right
				case 8:
				  $img_res = imagerotate($img_res, 90, 0); 
				break;
				
			  }
				
			//imagejpeg($img_res);
			imagejpeg($img_res, $uploaddir.$new_uploadfile_name);
			imagedestroy($img_res);
		}
		
		
		array_push($uploadfileArray,$new_uploadfile_name);
	}
	if($i > 0){
		if(isset($_SESSION['uploadfileArray'])){
			$_SESSION['uploadfileArray'] = array_merge($uploadfileArray,$_SESSION['uploadfileArray']);
		}else{
			$_SESSION['uploadfileArray'] = $uploadfileArray;
		}
		$returnArray['uploadfileArray']	= $uploadfileArray;
		$returnArray['status'] = "success";
		$returnArray['message']	= "$i Document(s) added sucessfully";
		$returnArray['error_message'] = "$j Document(s) not uploaded. Please upload only png/jpg/pdf or docx document";
	}else{
		$returnArray['status'] = "failure";
	}
	echo json_encode($returnArray);	
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='upload_single_document'){
	//$job_id = $_REQUEST["job_id"];
	unset($_SESSION['uploadfileArray']);
	$returnArray = array();
	$uploadfileArray = array();
	$i = 0;
	$j = 0;
	include("resize-class.php");
	foreach($_FILES['uploadfile']['name'] as $key => $uploadfile){
		if(isset($uploadfile) && $uploadfile !=""){
			$path = $uploadfile;
			$ext = strtolower(pathinfo($path, PATHINFO_EXTENSION));
			if($ext == "jpg" || $ext == "jpeg" || $ext == "png" || $ext == "pdf" || $ext == "doc" || $ext == "docx" || $ext == "xls" || $ext == "xlsx" || $ext == "csv"){
				$i++;
				
				$digits = 5;
				$random_number = rand(pow(10, $digits-1), pow(10, $digits)-1);
				$uploaddir = '../document/';
				$image_name_array = explode(".",$uploadfile);
				$new_uploadfile_name = $image_name_array[0]."_".$random_number.".".$image_name_array[1];
				$uploadfile = $uploaddir . $new_uploadfile_name;
				move_uploaded_file($_FILES['uploadfile']['tmp_name'][$key], $uploadfile);
				array_push($uploadfileArray,$new_uploadfile_name);
				################     Resize image to 200x 200 START ####################
								
					list($width, $height, $type, $attr) = getimagesize($uploadfile);			
					$resizeObj = new resize($uploadfile);			
					$resizeObj -> resizeImage(200, 200, 'crop');			
					//$resizeObj -> resizeImage(200, 200);			
					$resizeObj -> saveImage($uploadfile, 100);
					
				################     Resize image to 200x 200 END HERE ####################
			}else{
				$j++;
			}
		}
	}
	if($i > 0){
		if(isset($_SESSION['uploadfileArray'])){
			$_SESSION['uploadfileArray'] = array_merge($uploadfileArray,$_SESSION['uploadfileArray']);
		}else{
			$_SESSION['uploadfileArray'] = $uploadfileArray;
		}
		$returnArray['uploadfileArray']	= $uploadfileArray;
		$returnArray['status'] = "success";
		$returnArray['message']	= "$i Document(s) added sucessfully";
		$returnArray['error_message'] = "$j Document(s) not uploaded. Please upload only png/jpg/pdf or docx document";
	}else{
		$returnArray['status'] = "failure";
	}
	echo json_encode($returnArray);	
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='upload_multiple_document'){
	//$job_id = $_REQUEST["job_id"];
	unset($_SESSION['uploadfileArray']);
	$returnArray = array();
	$uploadfileArray = array();
	$i = 0;
	$j = 0;
	//include("resize-class.php");
	foreach($_FILES['uploadfile']['name'] as $key => $uploadfile){
		if(isset($uploadfile) && $uploadfile !=""){
			$path = $uploadfile;
			$ext = strtolower(pathinfo($path, PATHINFO_EXTENSION));
			if($ext == "jpg" || $ext == "jpeg" || $ext == "png" || $ext == "pdf" || $ext == "doc" || $ext == "docx" || $ext == "xls" || $ext == "xlsx" || $ext == "csv"){
				$i++;
				
				$digits = 5;
				$random_number = rand(pow(10, $digits-1), pow(10, $digits)-1);
				$uploaddir = '../document/';
				$image_name_array = explode(".",$uploadfile);
				$new_uploadfile_name = $image_name_array[0]."_".$random_number.".".$image_name_array[1];
				$uploadfile = $uploaddir . $new_uploadfile_name;
				move_uploaded_file($_FILES['uploadfile']['tmp_name'][$key], $uploadfile);
				array_push($uploadfileArray,$new_uploadfile_name);
				################     Resize image to 200x 200 START ####################
								
					/*list($width, $height, $type, $attr) = getimagesize($uploadfile);			
					$resizeObj = new resize($uploadfile);			
					$resizeObj -> resizeImage(200, 200, 'crop');			
					//$resizeObj -> resizeImage(200, 200);			
					$resizeObj -> saveImage($uploadfile, 100);*/
					
				################     Resize image to 200x 200 END HERE ####################
			}else{
				$j++;
			}
		}
	}
	if($i > 0){
		if(isset($_SESSION['uploadfileArray'])){
			$_SESSION['uploadfileArray'] = array_merge($uploadfileArray,$_SESSION['uploadfileArray']);
		}else{
			$_SESSION['uploadfileArray'] = $uploadfileArray;
		}
		$returnArray['uploadfileArray']	= $uploadfileArray;
		$returnArray['status'] = "success";
		$returnArray['message']	= "$i Document(s) added sucessfully";
		$returnArray['error_message'] = "$j Document(s) not uploaded. Please upload only png/jpg/pdf or docx document";
	}else{
		$returnArray['status'] = "failure";
	}
	echo json_encode($returnArray);	
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='upload_single_image'){
	//$job_id = $_REQUEST["job_id"];	
	ob_clean();
	$returnArray = array();
	$orientation = $_REQUEST['orientation'];
	
	unset($_SESSION['uploadfileArray']);
	$uploadfileArray = array();
	$i = 0;
	$j = 0;
	
	 ini_set("display_errors", 0);

	if(isset($_FILES['file']['name']) && $_FILES['file']['name'] !=""){
		$i++;		
		$digits = 5;
		$random_number = rand(pow(10, $digits-1), pow(10, $digits)-1);
		$uploaddir = '../document/';
		$image_name_array = explode(".",$_FILES['file']['name']);
		$new_uploadfile_name = $image_name_array[0]."_".$random_number.".".$image_name_array[1];
		$uploadfile = $uploaddir . $new_uploadfile_name;
		move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile);
		
				
		$path = $_FILES['file']['name'];
		$ext = pathinfo($path, PATHINFO_EXTENSION);
		
		if($ext == "jpg" or $ext == "jpeg"){
			$img_res = imagecreatefromjpeg($uploadfile);			
		}
		if($ext == "gif"){
			$img_res = imagecreatefromgif($uploadfile);
		}
		if($ext == "png"){
			$img_res = imagecreatefrompng($uploadfile);
		}
		if($orientation!=1){
			switch($orientation) {
				case 1:
				return true;
				
				// Flipped on the horizontal axis (might do it at some point in the future)
				case 2:
				  //By @kormanowsky: imageflip() returns TRUE or FALSE so it's wrong to assign its return value to $final_img
				  imageflip($img_res, IMG_FLIP_HORIZONTAL);
				break;
				
				// Turned 180 deg
				case 3:
				  //imageflip($img_res, IMG_FLIP_BOTH);
				   $img_res = imagerotate($img_res, 180, 0); 
				break;
				
				// Upside-Down
				case 4:
				  imageflip($img_res, IMG_FLIP_VERTICAL);
				break;
				
				// Turned 90 deg to the left and flipped
				case 5:
				  imageflip($img_res, IMG_FLIP_VERTICAL);
				// Turned 90 deg to the left
				case 6:
				  $img_res = imagerotate($img_res, -90, 0);
				break;
				
				// Turned 90 deg to the right and flipped
				case 7:
				  imageflip($img_res, IMG_FLIP_VERTICAL);
				// Turned 90 deg to the right
				case 8:
				  $img_res = imagerotate($img_res, 90, 0); 
				break;
				
			  }
				
			//imagejpeg($img_res);
			imagejpeg($img_res, $uploaddir.$new_uploadfile_name);
			imagedestroy($img_res);
		}
		
		
		if(isset($_SESSION['live'])){
			array_push($uploadfileArray,$new_uploadfile_name);			
		}else{
			array_push($uploadfileArray,$new_uploadfile_name);
		}
	}
	
	
	if($i > 0){
		if(isset($_SESSION['uploadfileArray'])){
			$_SESSION['uploadfileArray'] = array_merge($uploadfileArray,$_SESSION['uploadfileArray']);
		}else{
			$_SESSION['uploadfileArray'] = $uploadfileArray;
		}
		$returnArray['uploadfileArray']	= $uploadfileArray;
		$returnArray['status'] = "success";
		$returnArray['message']	= "$i Document(s) added sucessfully";
		$returnArray['error_message'] = "$j Document(s) not uploaded. Please upload only png/jpg/pdf or docx document";
	}else{
		$returnArray['status'] = "failure";
	}
	echo json_encode($returnArray);	
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='delete_document'){
	$document_name = $_REQUEST['document_name'];
	$uploaddir = '../../../document/';
	$old_image	=	$uploaddir.$document_name;	
	if(file_exists($old_image)){
		if (($key = array_search($document_name, $_SESSION['uploadfileArray'])) !== false) {
			unset($_SESSION['uploadfileArray'][$key]);
		}
		unlink($old_image);
		$returnArray['status'] = "success";
	}else{
		$returnArray['status'] = "failure";
	}
	echo json_encode($returnArray);	
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='delete_document_from_list'){
	$document_id = $_REQUEST['document_id'];
	$document_name = utility::getTableFieldById('documents','document_name','id',$document_id);
	$uploaddir = '../../../document/';
	$old_image	=	$uploaddir.$document_name;	
	if(file_exists($old_image)){
		mysqli_query($conn,"DELETE FROM documents WHERE id = '$document_id' ");
		unlink($old_image);
		$returnArray['status'] = "success";
	}else{
		$returnArray['status'] = "failure";
	}
	echo json_encode($returnArray);	
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='update_quotation'){
	//die('aaaaaaaaaaaaaa');
	$job_id = $_REQUEST['job_id'];	
	$accepted_date_given = ($_REQUEST['quotation_status'] == "accepted_date_given") ? 1 : 0;
	$quotation_accepted = ($_REQUEST['quotation_status'] == "quotation_accepted") ? 1 : 0;
	$quotation_declined = ($_REQUEST['quotation_status'] == "quotation_declined") ? 1 : 0;
	$to_quote			= ($_REQUEST['quotation_status'] == "to_quote") 		  ? 1 : 0;
	$quoted				= ($_REQUEST['quotation_status'] == "quoted") 			  ? 1 : 0;
	/* echo "UPDATE job SET quotation_accepted = '$quotation_accepted',quotation_declined = '$quotation_declined' WHERE id = '$job_id' ";
	die; */
	mysqli_query($conn,"UPDATE job SET to_quote='$to_quote', quoted='$quoted', accepted_date_given='$accepted_date_given', quotation_accepted = '$quotation_accepted',quotation_declined = '$quotation_declined' WHERE id = '$job_id' ");
	echo $_REQUEST['quotation_status'];
	exit();
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='confirm_multiple_delete'){	
	include('confirm_multiple_delete.inc.php');
	die;
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='delete_single_appointment'){	
	ob_clean();
	$returnArray = array();
	$appointment_id = $_REQUEST['appointment_id'];
	$uploaddir = '../document/';
	$id	= $_REQUEST['id'];
	mysqli_query($conn,"DELETE FROM appointment WHERE id = $appointment_id ");
	/* $sql = "SELECT * FROM documents WHERE appointment_id = '$appointment_id' ";
	$rs = mysqli_query($conn,$sql);
	if(mysqli_num_rows($rs) > 0){
		while($data = mysqli_fetch_assoc($rs)){
			$document_id = $data["id"];
			$document_name = $data["appointment_document"];
			$old_image	=	$uploaddir.$document_name;	
			if(file_exists($old_image)){
				mysqli_query($conn,"DELETE FROM documents WHERE id = '$document_id' ");
				unlink($old_image);
			}
		}
	} */
	$returnArray['message']	= "Appointment deleted sucessfully";
	echo json_encode($returnArray);	
}
else if(isset($_REQUEST['action']) && $_REQUEST['action']=='delete_multiple_appointment'){	
	ob_clean();
	$returnArray = array();
	$appointment_id = $_REQUEST['appointment_id'];
	$common_appointment_id = $_REQUEST['common_appointment_id'];
	$uploaddir = '../document/';
	$id	= $_REQUEST['id'];
	mysqli_query($conn,"DELETE FROM appointment WHERE id = $appointment_id ");
	$sql = "SELECT * FROM documents WHERE appointment_id = '$appointment_id' ";
	$rs = mysqli_query($conn,$sql);
	if(mysqli_num_rows($rs) > 0){
		while($data = mysqli_fetch_assoc($rs)){
			$document_id = $data["id"];
			$document_name = $data["appointment_document"];
			$old_image	=	$uploaddir.$document_name;	
			if(file_exists($old_image)){
				mysqli_query($conn,"DELETE FROM documents WHERE id = '$document_id' ");
				unlink($old_image);
			}
		}
	}
	
	$sel_qry = "SELECT * FROM appointment WHERE common_appointment_id='".$common_appointment_id."' AND appointment_date>='".date('Y-m-d H:i:s')."' ";
		$sel_sql = mysqli_query($conn,$sel_qry);
		if(mysqli_num_rows($sel_sql)){
			while($app_row=mysqli_fetch_array($sel_sql)){
				$id = $app_row['id'];
				$sql = "SELECT * FROM documents WHERE appointment_id = '$id' ";
				$rs = mysqli_query($conn,$sql);
				if(mysqli_num_rows($rs) > 0){
					while($data = mysqli_fetch_assoc($rs)){
						$document_id = $data["id"];
						$document_name = $data["document_name"];
						$old_image	=	$uploaddir.$document_name;	
						if(file_exists($old_image)){
							mysqli_query($conn,"DELETE FROM documents WHERE id = '$document_id' ");
							unlink($old_image);
						}
					}
				}
				mysqli_query($conn,"DELETE FROM appointment WHERE id = $id ");
			}
		}
	$returnArray['message']	= "Appointment deleted sucessfully";
	echo json_encode($returnArray);	
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='task_status_update'){
	$task_id = $_REQUEST['task_id'];
	$is_done = $_REQUEST['is_done'];
	$task_done_date	= date("Y-m-d H:i:s");
	
	$updated_by		= $_SESSION['user_id'];
	
	$task_sql = "UPDATE appointment SET is_done = '$is_done', task_done_date = '$task_done_date', updated_by = '$updated_by' WHERE id = '$task_id' ";
	mysqli_query($conn,$task_sql);
	
	$returnArray["status"] = "success";
	echo json_encode($returnArray);
}
die;
?>