<?php
// required headers
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: access");
header("Access-Control-Allow-Methods: GET");
header("Access-Control-Allow-Credentials: true");
header('Content-Type: application/json');
 
// include database and object files
include_once '../config/database.php';
include_once '../objects/destination.php';
 
// get database connection
$database = new Database();
$db = $database->getConnection();
 
// prepare product object
$destination = new Destination($db);
 
// set Id property of record to read
$destination->countryId = isset($_GET['countryId']) ? $_GET['countryId'] : die();
$stmt = $destination->readByCountryId();

$num = $stmt->rowCount();

if($num>0){
 // products array
    $destinations_arr=array();
    $destinations_arr["records"]=array();
 
    // retrieve our table contents
    // fetch() is faster than fetchAll()
    // http://stackoverflow.com/questions/2770630/pdofetchall-vs-pdofetch-in-a-loop
    
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
        // extract row
        // this will make $row['name'] to
        // just $name only

        extract($row);
        
        $destination_item=array(
            "Id" => $Id,
            "countryId" => $countryId,
            "Name" => $Name,
            "Description" => html_entity_decode($Description),
            "Recommended" => $Recommended,
        );
 
        array_push($destinations_arr["records"], $destination_item);
    }
 
    // set response code - 200 OK
    http_response_code(200);


    // show destinations data in json format
    echo json_encode($destinations_arr);
}
 
else{
    // set response code - 404 Not found
    http_response_code(404);
 
    // tell the user product does not exist
    echo json_encode(array("message" => "Destination does not exist."));
}
?>