<?php
/*
* date last modified 28-11-2017
* Edited by PI-Digi Logical Solutions
* HTML Files - add_user.html.php
* Mysql Tables - tbl_designation, tbl_staff
*/
include('includes/imgFunctions.php');
include('includes/functions.php'); # general functions




$debug = true;

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

if(isset($_REQUEST['user_id']) && $_REQUEST['user_id']!=''){
	$userId = trim($_REQUEST['user_id']);
	$curDate = date('Y-m-d H:i:s');	

	if(isset($_POST['send'])) {
		
		if(isset($_POST['message']) && strlen($_POST['message'])>0) {
			$message = trim($_POST['message']);
		} else {
			$error = 'Please enter message';
		}	
		
				
		if(empty($error)) {
			
			//send push to user
				$exec_token = mysqli_query($con, "SELECT fcm_token, device FROM tbl_fcm_tokens WHERE user_id='".$userId."'");
				if(isset($exec_token) && mysqli_num_rows($exec_token)>0) {
					while($res_token = mysqli_fetch_array($exec_token)) {
						if($res_token['fcm_token']!='') {							
							$userName = getUserName($userId);
							$title = SITE_NAME.' Message';
							$body = 'Hi '.$userName.', '.$message;
							if($res_token['device']=='android') {
								sendGCMUserAdmin($title, $body, $res_token['fcm_token'], "user", $userId, $userName, 'user');																	
							} elseif($res_token['device']=='ios') {
								sendIosUserPush($title, $body, $res_token['fcm_token'], "user");
							}
						}
					}
				}
					
			/*$insertQry = "INSERT INTO tbl_admin_push(type, user_id, message, date) VALUES (
			'single',
			 '".mysqli_real_escape_string($con, $userId)."',
			 '".mysqli_real_escape_string($con, $message)."',
			 '".mysqli_real_escape_string($con, $curDate)."')";
			
			$execInsert = mysqli_query($con,$insertQry);
			$insert_id = mysqli_insert_id($con);		
			
			if($execInsert) {*/
						
				$success = 'Message sent Successfully';
				header('Location:index.php?page=send_push_user&user_id='.$userId.'&success='.$success);
				exit();			
			//}				
		} 
	}



} else {
	header('Location:index.php?page=manage_user&p='.$p);
	exit();
}

?>