<?php
// required headers
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
  
// database connection will be here
// include database and object files
include_once '../config/database.php';
include_once '../requestedSessions/requestedSessions.php';
  
// instantiate database and product object
$database = new Database();
$db = $database->getConnection();
  
// initialize object
$sessions = new RequestSessions($db);

$request_method=$_SERVER["REQUEST_METHOD"];

// get posted data
$data = json_decode(file_get_contents("php://input"));

switch($request_method)
{
    case 'POST':
        // make sure data is not empty
        if(!empty($data->coachID)){
            // set product property values
            $sessions->coachID = $data->coachID;
            $result = $sessions->coachGetRequestSessions();
            
            if($result != 'error'){
                $row = mysqli_fetch_all($result,MYSQLI_ASSOC);
                $rowcount=mysqli_num_rows($result);
                
                // check if more than 0 record found
                if($rowcount>0){
                    // set response code - 200 OK
                    http_response_code(200);
                    // show products data in json format
                    echo json_encode($row);
                    // Free the result set
                    mysqli_free_result($result);
                }else{
                    // set response code - 404 Not found
                    http_response_code(404);
                    // tell the user no result found
                    echo json_encode(array("message" => "Empty Data"));
                }
            }else{
                echo json_encode(array("message" => "Empty Data"));
            }
        }else{
            // set response code - 400 bad request
            http_response_code(400);
            // tell the user
            echo json_encode(array("message" => "Empty Data"));
        }
        
        break;
        
        default:
            // set response code - 405 bad request
            http_response_code(405);
            // tell the user
            echo json_encode(array("message" => "Invalid Method"));
            break;
}