
<?php 
error_reporting(0);

include('db_connect.php');
$response = array(); 
if(isset($_POST['id'])) {
	
	$receiver_id = $_POST['id'];
	$receiver_email = $_POST['email'];
    $sender_id = $_POST['sender_id'];
    $sender_token = $_POST['sender_token'];
	
  $query="DELETE FROM `friend_req` WHERE `sender_id`='$sender_id' AND `receiver_id`='$receiver_id'";
  
  if(mysqli_query($conn,$query)){
	  $user_id = $receiver_id;
	  $friend_id = $sender_id;
	  
	  $query="INSERT INTO `friend_list`(`user_id`, `friend_id`) VALUES ('$user_id' , '$friend_id'),('$friend_id' , '$user_id')";
	  
	  if(mysqli_query($conn,$query)){
				$response['error'] = false;
				$response['message'] = "Request Accepted!";
				
				$action = 'in.aziz.com.shopping.checklist.to.doing.simple.list.ACCEPTNOTIFICATION';
				$title = 'Grocery Shopping Checklist';
				$message = $receiver_email.' accept your request';
				$id = $sender_token;
								
				sendGCM($title,$message,$action,$id);
	  }
			
  } else {
		  $response['error'] = true; 
		  $response['message'] = "Some error occurred please try again!";
  }
} else {
	echo "error";
}
	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;
}
?>