<?php
/*
* Template Name: Hotel Search
*/
get_header(); global $wpdb;
//insert_hotels();
//insert_ratecomments();
// echo $sql = "SELECT `name` FROM `view_hotel` WHERE `hotel_id` = 1";
// $hotels = $wpdb->get_results($sql);
//     print_r($hotels); 
// die();
//global $wpdb;

//session_destroy();

/*
//$url = "https://api.test.hotelbeds.com/hotel-content-api/1.0/hotels?fields=all&language=ENG&from=1&to=2";   
$url = "https://api.test.hotelbeds.com/hotel-content-api/1.0/types/accommodations?fields=all&language=ENG&from=1&to=2";  
$header[] = "Api-Key: ".API_KEY;
$header[] = "X-Signature: ".SIGNATURE;
//$header[] = "Content-Type: application/json";
$header[] = "Accept: application/json";
//$header[] = "Accept-encoding: Gzip";

$ch = curl_init();
curl_setopt( $ch, CURLOPT_HTTPHEADER, $header );
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );  

//print_r(curl_exec($ch));
$response = json_decode(curl_exec($ch));
echo '<pre>';
print_r($response);
echo '</pre>';
$hotels_tbl = $wpdb->prefix.'hotels';
$accommodation_tbl = $wpdb->prefix.'accommodations';
$chain_tbl = $wpdb->prefix.'chain';
$category_tbl = $wpdb->prefix.'category';*/

/*
foreach($response->hotels as $hotel){

    //get category id
    $category_id = $wpdb->get_results("
        SELECT `id`
        FROM `$category_tbl`
        WHERE  `categoryCode`='".$hotel->chainCode."'
        AND `categoryGroupCode`='".$hotel->categoryGroupCode."'
    ");

    //get chain id
    $chain_id = $wpdb->get_results("
        SELECT `id`
        FROM `$accommodation_tbl`
        WHERE  `chainCode`='".$hotel->chainCode."'
    ");

    //get accomodation id
    $accommodation_id = $wpdb->get_results("
        SELECT `id`
        FROM `$accommodation_tbl`
        WHERE  `accommodationTypeCode`='".$hotel->accommodationTypeCode."'
    ");


    $hotel_data = array(
        'code'                  => $hotel->code,
        'name'                  => $hotel->name->content,
        'description'           => $hotel->description->content,
        'countryCode'           => $hotel->countryCode,
        'destinationCode'       => $hotel->destinationCode,
        'zoneCode'              => $hotel->zoneCode,
        'longitude'             => $hotel->coordinates->longitude,
        'latitude'              => $hotel->coordinates->latitude,
        'category_id'           => $category_id[0]->id,
        'chain_id'              => $chain_id[0]->id,
        'accommodation_id'      => $$accommodation_id[0]->id,
        'address'               => $hotel->address->content,
        'postalCode'            => $hotel->postalCode,
        'city'                  => $hotel->city->content,
        'email'                 => $hotel->email,
        'license'               => $hotel->license,
        'web'                   => $hotel->web,
        'S2C'                   => $hotel->S2C,
    );
    $wpdb->insert($hotels_tbl,$hotel_data);

}*/
/*
if(!curl_exec($ch)){
    die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
}
*/







/*require __DIR__ .'/vendor/autoload.php';

use hotelbeds\hotel_api_sdk\HotelApiClient;
use hotelbeds\hotel_api_sdk\model\Destination;
use hotelbeds\hotel_api_sdk\model\Geolocation;
use hotelbeds\hotel_api_sdk\model\Occupancy;
use hotelbeds\hotel_api_sdk\model\Pax;
use hotelbeds\hotel_api_sdk\model\Rate;
use hotelbeds\hotel_api_sdk\model\Stay;
use hotelbeds\hotel_api_sdk\types\ApiVersion;
use hotelbeds\hotel_api_sdk\types\ApiVersions;
use hotelbeds\hotel_api_sdk\messages\AvailabilityRS;

$reader = new Zend\Config\Reader\Ini();
$config   = $reader->fromFile(__DIR__.'/HotelApiClient.ini');
$cfgApi = $config["apiclient"];

$apiClient = new HotelApiClient($cfgApi["url"],
                                $cfgApi["apikey"],
                                $cfgApi["sharedsecret"],
                                new ApiVersion(ApiVersions::V1_0),
                                $cfgApi["timeout"]);

$rqData = new \hotelbeds\hotel_api_sdk\helpers\Availability();
$rqData->stay = new Stay(DateTime::createFromFormat("Y-m-d", "2016-11-25"),
                         DateTime::createFromFormat("Y-m-d", "2016-11-30"));

$geolocation = new Geolocation();
$geolocation->latitude = 40.730610;
$geolocation->longitude = -73.935242;
$geolocation->radius = 100.00;
$geolocation->unit = 'km';
$rqData->geolocation = $geolocation;


//$rqData->hotels = [ "hotel" => [ 1067 ] ];
//$rqData->destination = new Destination("PMI");//By default this search type is disabled
$occupancy = new Occupancy();
$occupancy->adults = 2;
$occupancy->children = 1;
$occupancy->rooms = 1;

$occupancy->paxes = [ new Pax(Pax::AD, 30, "Mike", "Doe"), new Pax(Pax::AD, 27, "Jane", "Doe"), new Pax(Pax::CH, 8, "Mack", "Doe") ];
$rqData->occupancies = [ $occupancy ];

try {
    $availRS = $apiClient->Availability($rqData);
} catch (\hotelbeds\hotel_api_sdk\types\HotelSDKException $e) {
    $auditData = $e->getAuditData();
    error_log( $e->getMessage() );
    print_r($e->getMessage());
    error_log( "Audit remote data = ".json_encode($auditData->toArray()));
    echo json_encode($auditData->toArray()); 
    exit();
} catch (Exception $e) {
    error_log( $e->getMessage() );
    print_r($e->getMessage());
    exit();
}

// Check availability is empty or not!
if (!$availRS->isEmpty()) {
   echo '<pre>';
   print_r($availRS->hotels->toArray());
   echo '</pre>';
} else {
   echo "There are no results!";
}

die();*/
/*
// Iterate all returned hotels with an Hotel object
foreach ($availRS->hotels->iterator() as $hotelCode => $hotelData)
{
        // Get all hotel data (from Hotel object $hotelData)
      
        // Iterate all rooms of each hotel
        foreach ($hotelData->iterator() as $roomCode => $roomData)
        {
                // Iterate all rate of each room
                foreach($roomData->rateIterator() as $rateKey => $rateData)
                {

                }
        }
}
*/
/*echo '<pre>';
print_r($availRS);
echo '</pre>';*/

/*echo '<pre>';
print_r($_POST);
echo '</pre>';*/

?>
<div id="loader">
    <div class="loading"></div> 
</div>
<section class="hotel_form">
    <div class="container">
      <?php include('form.php'); ?>
    </div>
</section>
<?php get_footer(); ?>