<?php
/*
* Template Name: Search Results
*/
get_header();

global $wpdb,
$accommodation_tbl,
$facilities_tbl,
$destinations_tbl,
$hotels_tbl,
$countries_tbl,
$hotels_terminals_tbl,
$hotels_facilities_tbl,
$categories_tbl,
$chains_tbl;

/*if($_POST){
  unset($_POST['submit']);
  foreach($_POST as $postkey => $postvalue){
    $_GET[$postkey] = $postvalue;
  }
}*/

if($_GET){
    //echo $_SERVER['REQUEST_URI'];
}

if(empty($_GET['search_keyword'])){
  wp_redirect(site_url());
}


$hotels = search_hotels($_GET);

$keywords = array(
   '29' => 'Romantic hotels',
   '31' => 'Design',
   '34' => 'Business hotels',
   '36' => 'Golf hotels',
   '37' => 'Beach hotels',
   '38' => 'City',
   '39' => 'Hotels with spa',
   '42' => 'Historical',
   '43' => 'Ski hotels',
   '44' => 'Rural hotels',
   '45' => 'Sport hotels',
   '81' => 'Family hotels',
   '83' => 'Internet',
   '84' => 'Wheelchair-friendly',
   '85' => 'Restaurant',
   '86' => 'Spa',
   '87' => 'Parking',
   '88' => 'TV',
   '89' => 'Gym',
   '90' => 'Air conditioned',
   '92' => 'Heating',
   '100' => 'Hotels with charm',
   '101' => 'Swimming Pool'
);
asort($keywords);

//if search term id changed
if(isset($_POST['submit'])){
  $new_url = '';
  foreach($_GET as $getkey => $getvalue){
    if($getkey == 'search_keyword'){
      $getvalue = $_POST['search_keyword'];
    }
    if($getkey == 'hotel_id'){
      $getvalue = $_POST['hotel_id'];
    }
    if($getkey == 'destination'){
      
      $getvalue = urlencode($_POST['destination']);
    }
    $new_url .= $getkey.'='.$getvalue.'&';
  }
  $new_url = rtrim($new_url,'&');

  //echo $new_url;
  wp_redirect(site_url('search-results/?'.$new_url));
}


?>
<div id="loader">
    <div class="loading"></div> 
</div>


    <div class="container">

      <div class="row">
        <div class="col-sm-12">
          <ol class = "breadcrumb">
             <li><a href="<?php echo site_url(); ?>">Casa</a></li>
             <li class="active">Catálogo de Hoteles</li>
          </ol>
        </div>

        <!-- <div class="col-sm-12">
          <h1 class="sea_h1">Catálogo de Hoteles</h1>
          <h4 class="mt10">Refinar su catálogo de hoteles</h4>
          <div class="row mt20">
            <div class="col-sm-3">
              <form method="post" action="">
                <div class="input-group custom-search-form">
                  <!-- <input type="text" name="search_term" class="form-control" value="<?php echo $_GET['search_keyword']; ?>">
                  <span class="input-group-btn">
                  <button class="btn btn-primary" type="submit" name="submit">
                  <span class="glyphicon glyphicon-search"></span>
                 </button>
                 </span> --
                


                  <input type="text" name="search_keyword" class="form-control" id="search_keyword" placeholder="Search..." required autocomplete="off">
                  <input type="hidden" name="hotel_id" id="hotel_id" value="0" />
                  <input type="hidden" name="destination" id="destination_id" value="0" />
                  <!-- <div id="suggesstion-box"></div> --
                  <ul id="suggest-list">
                    <li class="parentResult">
                      <span class="heading">Destinos</span>
                      <ul id="destinations"></ul>
                    </li>
                    <li class="parentResult">
                      <span class="heading">Hoteles</span>
                      <ul id="hotels"></ul>
                    </li>
                  </ul>

                  <span class="input-group-btn">
                    <button class="btn btn-primary" type="submit" name="submit">
                        <span class="glyphicon glyphicon-search"></span>
                    </button>
                  </span>

                 </div>
              </form>
            </div>
          </div>
        </div> -->

        <span class="clearfix"></span>
      </div>
      
      <div class="row mt30">
        <div class="col-sm-3">

        <h1 class="sea_h1">Catálogo de Hoteles</h1>
          <h4 class="mt10">Refinar su catálogo de hoteles</h4>
          <div class="row mt20">
            <div class="col-sm-12">
              <form method="post" action="">
                <div class="input-group custom-search-form">
                  <!-- <input type="text" name="search_term" class="form-control" value="<?php echo $_GET['search_keyword']; ?>">
                  <span class="input-group-btn">
                  <button class="btn btn-primary" type="submit" name="submit">
                  <span class="glyphicon glyphicon-search"></span>
                 </button>
                 </span> -->
                


                  <input type="text" name="search_keyword" class="form-control" id="search_keyword" placeholder="Search..." required autocomplete="off">
                  <input type="hidden" name="hotel_id" id="hotel_id" value="0" />
                  <input type="hidden" name="destination" id="destination_id" value="0" />
                  <!-- <div id="suggesstion-box"></div> -->
                  <ul id="suggest-list">
                    <li class="parentResult">
                      <span class="heading">Destinos</span>
                      <ul id="destinations"></ul>
                    </li>
                    <li class="parentResult">
                      <span class="heading">Hoteles</span>
                      <ul id="hotels"></ul>
                    </li>
                  </ul>

                  <span class="input-group-btn">
                    <button class="btn btn-primary" type="submit" name="submit">
                        <span class="glyphicon glyphicon-search"></span>
                    </button>
                  </span>

                 </div>
              </form>
            </div>
          </div>



          <div class="sear_letext">
            <!-- Sidebar-->
            <?php include('search-results-sidebar.php'); ?>
          </div>
        </div>
        <div class="col-sm-9">
          
          <div class="row">

          <!-- This div is uesd for ajax data fetch -->
          <section class="col-xs-12 col-sm-12 col-md-12" id="search-results">

          <?php
          if(!empty($hotels)){
            echo '<h4 class="sea_h1">'.$hotels[0]->total_records.' Results Found</h4>';
            foreach ($hotels as $hotel) {
              //echo $hotel->hotel_id;
              $images = get_hotel_images($hotel->hotel_id);
              $facilities = get_hotel_facilities($hotel->hotel_id,70,8);
              $image = NORMAL_IMAGE_PATH.$images[0]->path;
              $img_url= itg_timthumb_image($image, 200,320);
              $hotel_link = create_hotel_link($hotel->hotel_id);
          ?>
             <article class="search-result box">

              <div class="row">
                  <div class="col-xs-12 col-sm-12 col-md-5">
                  <a href="#<?php //echo site_url('hotel/'.$hotel->hotel_id.'/'.$hotel->name); ?>" title="<?php echo $hotel->name; ?>" class="thumbnail">
                    <img src="<?php echo $img_url; ?>" alt="<?php echo $hotel->name; ?>" />
                  </a>
                </div>
              
                <div class="col-xs-12 col-sm-12 col-md-7 mt20">
                  <div class="mtv">
                  <h3><a href="<?php echo $hotel_link; ?>"><?php echo $hotel->name; ?></a></h3>
                  <p><?php echo ucwords(strtolower($hotel->address.', '.$hotel->city.', '.$hotel->country_name)); ?></p>            
                  <!-- <a href="#" title="Lorem ipsum"><span class="plus">Compare
                    <i class="glyphicon glyphicon-plus"></i>
                  </span></a> -->

                  <ul class="meta-search">
                    <?php 
                    foreach($facilities as $facility){
                        echo '<li><i class="fa fa-check-square-o"></i><span>'.$facility->name.'</span></li>';
                    }
                    ?>
                  </ul>

                  <div class="clearfix"></div>

                  <div class="mt20 virt_text"><a class="top rooms_check" href="javascript:void(0)" hotel-code="<?php echo $hotel->code; ?>">Consultar disponibilidad de habitaciones</a></div>

                  <div class="clearfix"></div>
                  </div>
                    
                </div>
              </div>

              <div class="bottom slidingDiv roomdetails_<?php echo $hotel->code; ?>">
                <!--<p><?php //echo $hotel->description; ?></p>--> 
              </div>

              <span class="clearfix"></span>
            </article> 
          <?php 
            } 
          }else{
            echo '<h4>Lo sentimos, no se han encontrado resultados!</h4>';
          }
          ?>
  
        </section>
          </div>
        </div>
      </div>
    </div>



<?php get_footer(); ?>