<?php
include "../../../../wp-blog-header.php";

@$hotel_code = (int)$_POST['hotel_code'];
@$rooms = (int)$_POST['rooms'];
@$adults = (int)$_POST['adults'];
@$children = (int)$_POST['children'];
@$checkin = $_POST['checkin'];
@$checkout = $_POST['checkout'];


//@$coordinates = explode('|',get_coordinates($_GET['destination']));
@$coordinates = explode('|',get_coordinates("Barcelona, Spain"));
@$latitude = (double) $coordinates[0];
@$longitude = (double) $coordinates[1];

require '../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('../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", $checkin
        ),
        DateTime::createFromFormat("Y-m-d", $checkout)
  );

  /*$geolocation = new Geolocation();
  $geolocation->latitude = $latitude;
  $geolocation->longitude = $longitude;
  $geolocation->radius = 100.00;
  $geolocation->unit = 'mi';
  $rqData->geolocation = $geolocation;*/

  $rqData->hotels = [ "hotel" => [ $hotel_code ] ];

  $occupancy = new Occupancy();
  $occupancy->adults = $adults;
  $occupancy->rooms = $rooms;

  if(!empty($children)){
     $occupancy->children = $children;
     $paxes = array();
     foreach($_POST['child_age'] as $child_age){
 		$paxes[] =  new Pax(Pax::CH, (int)$child_age);
 	 }
     $occupancy->paxes = $paxes;
  }

  $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());
      $error = $e->getMessage();
      exit();
  }

  // Check availability is empty or not!
  $html = "";
  if (!$availRS->isEmpty()) {
     $hotels = $availRS->hotels->toArray();
     /*echo '<pre>';
     print_r($hotels);
     echo '</pre>';*/

      foreach($hotels['hotels'][0]['rooms'] as $rooms){
          $html .= '<div class="room_table">
                      <h3>Room Name: '.$rooms['name'].'</h3>';

                      if(!empty($rooms['rates'])){

                        $html .= '<h5>Rates</h5>
                                  <div class="table-responsive">
                                    <table class="table table-bordered table-striped  ">
                                        <thead>
                                          <tr>
                                            <th>Tasa neta</th>
                                            <th>Tipo de pago</th>
                                            <th>Nombre de la Junta</th>
                                          </tr>
                                        </thead>
                                        <tbody>';

                                          foreach($rooms['rates'] as $rates){
                                            $html .= '<tr>
                                                        <td>'.$hotels['hotels'][0]['currency'].' '.$rates['net'].'</td>
                                                        <td>'.$rates['paymentType'].'</td>
                                                        <td>'.$rates['boardName'].'</td>
                                                      </tr>';
                                          }             
                                          
                      $html .=        '</tbody>
                                    </table>
                                  </div>';
                      }
                  $html .= '</div>';

      }

  } else {
     //$html .= 'No rooms found!';
     $html .= $error;
  }

  echo $html;