<?php 
ob_clean();
include("../../../include/startup.php"); 
if(isset($_REQUEST['action']) && $_REQUEST['action']=='add_message'){
	////////////// For Insert and Update ///////////////////////////
	utility::addslashesOnAllInput(array("query_string"));
    
	$page_action = $_REQUEST['page_action_message'];	
	$id = $_REQUEST['id'];
	$job_id = $_REQUEST['job_id'];	
	$con_id = utility::getTableFieldById("job_admin_mapping","admin_id","job_id",$job_id);
	if(isset($_SESSION['job_user_id']) && $_SESSION['job_user_id'] !=""){
		$admin_id= $_SESSION['job_user_id'];
		$job_user_id= $_SESSION['job_user_id'];
	}else{
		$admin_id= $_SESSION['user_id'];
		$job_user_id = '0';
	}
    //$admin_id = $_SESSION['user_id'];
	$message_text = $_REQUEST['message_text'];
	$message_id = $_REQUEST['message_id'];
	$message_date	 = utility::setFormatedDate($_REQUEST['message_date']);
	$source	 = $_REQUEST['source'];
	$created_on = date("Y-m-d");
	$updated_by = $_SESSION["user_id"];
		
	if($page_action == "add"){
		$sql= "INSERT INTO message (job_id,admin_id,job_user_id,message_text,message_id,message_date,source,updated_by,created_on) VALUES('$job_id','$admin_id','$job_user_id','$message_text','$message_id','$message_date','$source','".$admin_id."','$created_on')";
		mysqli_query($conn,$sql);
		$id_sms = mysqli_insert_id();
		///////////////////////////// Send SMS ///////////////////////////////
	
		/* include_once("../include/sms/EsendexSendService.php");
		include("../include/sms/smsconfig.php");
		$sendService = new EsendexSendService( $username, $password, $accountReference );
		
		//$mobilenos='07594279660';// this is Iram number
		
		$mobilenos = utility::getTableFieldById("job","phone_no","id",$job_id);
		$mobilenos = utility::validate_crypt_data($mobilenos);
		$mobilenos = trim($mobilenos);
		$mobilenos = str_replace(" ","",$mobilenos);
		
		if(!(isset($mobilenos) && $mobilenos !="")){
			$mobilenos = utility::getTableFieldById("job","landline_phone_no","id",$job_id);
			$mobilenos = utility::validate_crypt_data($mobilenos);
			$mobilenos = trim($mobilenos);
			$mobilenos = str_replace(" ","",$mobilenos);
		}
		
		if(!(isset($mobilenos) && $mobilenos !="")){
			$mobilenos = utility::getTableFieldById("job","phone_no3","id",$job_id);
			$mobilenos = utility::validate_crypt_data($mobilenos);
			$mobilenos = trim($mobilenos);
			$mobilenos = str_replace(" ","",$mobilenos);
		}
		
		$message_gateway_response ="";
		
		//if($_SERVER['HTTP_HOST'] !="localhost" || 1==1){
		if($_SERVER['HTTP_HOST'] !="localhost"){
		$result = $sendService->SendMessageFull($originator, $mobilenos, $message_text, $type, $validityPeriod);
		
		$sms_respone = serialize($result);
		
		if(isset($result['Result'])){
			$message_gateway_response .= $result['Result'];
		}
		if(isset($result['MessageIDs'])){
			$message_gateway_response .= "<br>".$result['MessageIDs'];
		}
		$update_query="UPDATE $table_name SET sms_respone='".$sms_respone."' WHERE id='".$id_sms."' ";
		mysqli_query($conn,$update_query);
		} */
		////////////// End Send SMS ///////////////////////////////////////
	}else{
		$sql= "UPDATE message SET job_id = '$job_id',job_user_id = '$job_user_id',admin_id = '$admin_id',message_text = '$message_text',message_id = '$message_id',message_date = '$message_date',updated_by = '".$_SESSION['user_id']."' WHERE id = $id";
		mysqli_query($conn,$sql);
	}
	
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='get_edit_data_message'){
	$id = $_REQUEST['id'];
	$sql= "SELECT * FROM message WHERE id = $id";
    $result_edit = mysqli_query($conn,$sql);
    $row_edit = mysqli_fetch_assoc($result_edit);

    $data_array = array();
	foreach($row_edit as $key => $value){
		if($key == "message_date"){
			$value = utility::getFormatedDateReturn($value);
			$data_array["message_date"] = $value;
		}else{
			$data_array[$key] = $value;
		}	
	}
	$returnArray["data_array"] = $data_array;
	$returnArray["status"] = "success";
	
    echo json_encode($returnArray, JSON_FORCE_OBJECT);
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='delete_message'){
	$message_id = $_REQUEST['message_id'];
	mysqli_query($conn,"DELETE FROM message WHERE id = '$message_id'");
	$returnArray["status"] = "success";
	echo json_encode($returnArray);
}else{
	////////////// For Listing data through JSON ///////////////////////////
	$job_id = $_REQUEST["job_id"];
    $query_for_message = "SELECT * FROM message WHERE job_id = '$job_id' ORDER BY id DESC ";
	$sql_for_message = mysqli_query($conn,$query_for_message);
	$result_for_message = mysqli_num_rows($sql_for_message);
	$message_for_clients = "";
	if($result_for_message > 0){
		$message_for_clients .= '{';
		$message_for_clients .= '"data" : [';
		while($row_message_for_clients = mysqli_fetch_assoc($sql_for_message)){			
			if(isset($row_message_for_clients['job_user_id']) && $row_message_for_clients['job_user_id'] != 0){
				$msg_by= $gsi->getJobUserById($row_message_for_clients['admin_id']); 
			}else{
				$msg_by= Admin::getAdminsById($row_message_for_clients['admin_id']); 
			}
			
			$message_for_clients .= '{"job_id":"'  . ucfirst(utility::getTableFieldById('job','first_name','id',$row_message_for_clients['job_id'])) . '",';
			$message_for_clients .= '"message_name":"'  .$gsi->getMessageTemplateById($row_message_for_clients['message_id']). '",';
			$message_for_clients .= '"message_text":"'  .utility::ellipsis(preg_replace('/\s\s+/', ' ',$row_message_for_clients['message_text']),75). '",';
			$message_for_clients .= '"message_by":"'  . $msg_by . '",';
			$message_for_clients .= '"message_date":"'  . utility::getFormatedDateReturn($row_message_for_clients['message_date']) . '",';
			$message_for_clients .= '"id":"'  . $row_message_for_clients['id'] . '"},';
		}
		$message_for_clients = substr($message_for_clients,0,-1);
		$message_for_clients .= "]";
		$message_for_clients .= '}';
	}else{
		$message_for_clients .= '{';
		$message_for_clients .= '"data" : [';
		$message_for_clients .= "]";
		$message_for_clients .= '}';
	}
	echo $message_for_clients;
}
die;
?>