<?php
error_reporting(0);

include('db_connect.php');
$response = array();
$receiver_id = '';

		if(isset($_POST['receiver_email']) ){
			$sender_email = $_POST['sender_email'];
			$receiver_email = $_POST['receiver_email'];
			$sender_id = $_POST['user_id'];
			$status = $_POST['status'];
			$date=$_POST['req_date'];

		$r=mysqli_query($conn,"select * from users where email_address='$receiver_email'"); 
		if (!$r) {
		    $response['message'] = mysqli_error($conn);
		}
		if (mysqli_num_rows($r) > 0) {
			while($row=mysqli_fetch_assoc($r))
			{
			$receiver_id = $row['id'];
			$token = $row['token'];
			}
		} else {
          $to_email = $receiver_email;
$subject = 'App Invitation';
$message = "If you don't have this app install in your phone. We are inviting you to install this app. \n\n\n".$sender_email." is inviting you to install this app and login to use this app.\n\n Link of the app : https://play.google.com/store/apps/details?id=com.shopping.checklist.to.doing.simple.list";
$headers = 'From: info@emphirezone.com';
if(mail($to_email,$subject,$message,$headers))
{
 	$response['error'] = false; 
			$response['message'] = "This account is not exist!";
}else{
  //echo "not sent";
}
		
		}
			if($receiver_id != null) {
			    
				$query=mysqli_query($conn,"select * from friend_list where user_id  ='$sender_id' and  friend_id ='$receiver_id'");
				
        		if (mysqli_num_rows($query) > 0) {
					$response['error'] = false;
			        $response['message'] = "Sorry! This User is already your Friend.";
				} else {
					$rr=mysqli_query($conn,"select * from friend_req where sender_id ='$sender_id' AND receiver_id ='$receiver_id' OR 
																		   sender_id ='$receiver_id' AND receiver_id ='$sender_id'");
					
					if (mysqli_num_rows($rr) > 0) {
						$response['error'] = false;
						$response['message'] = "This request is already in Pending!";
					} else {
							$query="INSERT INTO `friend_req` (`sender_id`, `receiver_id`, `status`, `dated`) VALUES ('$sender_id','$receiver_id','$status','$date')";
							
						  if(mysqli_query($conn,$query)) {
							$response['error'] = false; 
							$response['message'] = "Request sent!";
							
							$action = 'in.aziz.com.shopping.checklist.to.doing.simple.list.REQUESTNOTIFICATION';
							$title = 'Grocery Shopping Checklist';
							$message = $sender_email.' wants to connect with you';
							$id = $token;
							
							sendGCM($title,$message,$action,$id);
							
						  } else {
							$response['error'] = true; 
							$response['message'] = "Some error occurred please try again";
						  }
						  
						}
				}
        		
			}
		}
		echo json_encode($response);
mysqli_close($conn);

function sendGCM($title,$message,$action, $id) {
	require_once('Notifications.php');
						$notification = new Notifications();
						
					
						$notification->setTitle($title);
						$notification->setMessage($message);
						$notification->setAction($action);
						
						$firebase_token = $id;
						$firebase_api ="AAAA9R2L9rw:APA91bF8f86e4j3dzxQIDDD9WVag1FSZflcecqASCjK1SMFciV93ywymHE_tMf55y9tsl56TkYc0W3IGGN19B_GA31QiNIKJ6dEhYTRf-RZ27XjbEmBNGBj8xTLmL7NDHhNY5zu3QJhn";
						
					
						
						$requestData = $notification->getNotificatin();
						
					
							
							$fields = array(
								'to' => $firebase_token,
								'data' => $requestData,
							);
						
		
		
						// Set POST variables
						$url = 'https://fcm.googleapis.com/fcm/send';
 
						$headers = array(
							'Authorization: key=' . $firebase_api,
							'Content-Type: application/json'
						);
						
						// Open connection
						$ch = curl_init();
 
						// Set the url, number of POST vars, POST data
						curl_setopt($ch, CURLOPT_URL, $url);
 
						curl_setopt($ch, CURLOPT_POST, true);
						curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
						curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
						// Disabling SSL Certificate support temporarily
						curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
 
						curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
 
						// Execute post
						$result = curl_exec($ch);
						if($result === FALSE){
							die('Curl failed: ' . curl_error($ch));
						}
 
						// Close connection
						curl_close($ch);
						
					
					//	echo json_encode($fields,JSON_PRETTY_PRINT);
				
						// echo $result;
}
?>