
<?php 
error_reporting(0);

include('db_connect.php');
$response = array(); 
if(isset($_POST["item_id"])){
	$item_id = $_POST['item_id'];
    $status= $_POST['status'];
        $itemName= $_POST['itemName'];
            $token= $_POST['token'];
            $email= $_POST['email'];
            
 $query="UPDATE `grocery_list` SET `status`='$status' WHERE `item_id`='$item_id'";
 
  if(mysqli_query($conn,$query)){
   $response['error'] = false; 
			$response['message'] = "Item purchased";
			  $action = 'abc';
							$title = 'Grocery Shopping Checklist';
							$message = $email.' has purchase '.$itemName.' for you!';
							$id = $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;
}
?>