<?php
$arrayhotelsearch = array();
//funcion fecha 
function newdatesearch($date)
{
    list($m, $d, $a) = explode("/", $date);
    return $a . "-" . $m . "-" . $d;
}
//getting data
$sorte            = mysqli_escape_string($conn, $_GET['sort']);
$tiposorte        = mysqli_escape_string($conn, $_GET['typesort']);
$destino          = mysqli_escape_string($conn, $_GET['destino']);
$destino          = str_replace(' ', '', $destino);
$city_id          = mysqli_escape_string($conn, $_GET['city_id']);
require_once('inc/data-bgo/booking-data-in-get.php');
require_once('inc/data-bgo/booking-data-send.php');

$checkinhotel = newdatesearch($checkin);
$checkouthotel = newdatesearch($checkout);
$totalrooms = $rooms;
//obteniendo el numero mayor
$guestsroom0 = $adults0 + $children0;
$guestsroom1 = $adults1 + $children1;
$guestsroom2 = $adults2 + $children2;
$guestsroom3 = $adults3 + $children3;
$guestsroommayor = $guestsroom0;
if($guestsroom1 > $guestsroommayor){
	$guestsroommayor = $guestsroom1;
}
if($guestsroom2 > $guestsroommayor){
	$guestsroommayor = $guestsroom2;
}
if($guestsroom3 > $guestsroommayor){
	$guestsroommayor = $guestsroom3;
}
//echo $guestsroommayor;
$hotelearch = mysqli_query($conn, "SELECT hot.hotel_id, hot.hotel_name, hot.hotel_low_price, hot.hotel_reviews, hot.hotel_review_score, hot.hotel_image_path, hot.hotel_country, hot.hotel_address, hot.hotel_zipcode, hot.longitud, hot.latitud, hot.hotel_stars, hot.hotel_short_description, ci.city_name, roomy, disponibles FROM hotels AS hot JOIN cities AS ci ON hot.hotel_city = ci.city_id LEFT OUTER JOIN (SELECT hrt.hotel_id, hrt.room_type_id AS roomy, MAX(hrt.room_type_inventory - IFNULL(bca.cantidad,0)) AS disponibles FROM hotel_room_type AS hrt JOIN (SELECT price_description,room_type_id,room_person,room_price,price_status,price_deleted FROM hotel_room_price WHERE room_person = '2' AND price_status = 'Y') AS hrp ON hrt.room_type_id = hrp.room_type_id LEFT OUTER JOIN (SELECT room_type_id, MAX(total_room + blocked_inventory) AS cantidad FROM booking_check_avilability WHERE check_in_date >= '$checkinhotel' AND check_out_date <= '$checkouthotel' GROUP BY room_type_id) AS bca ON hrt.room_type_id = bca.room_type_id WHERE hrt.room_type_capacity >= '$guestsroommayor' AND active = '1' AND deleted = '0' GROUP BY hrt.hotel_id HAVING disponibles >= '$totalrooms' ) AS rom ON hot.hotel_id = rom.hotel_id WHERE hot.active = '1' AND hot.hotel_city = '$city_id' AND hot.limited = '0' AND hot.developer = '0' AND hot.deleted = '0'  AND disponibles >= '$totalrooms' ");
$tothotelsearch = mysqli_num_rows($hotelearch);

if ($tothotelsearch > 0) {
    while ($hotelsearchData = mysqli_fetch_array($hotelearch)) {
        $lowratehotel    = intval($hotelsearchData['hotel_low_price']);
        $hotellocalimage = "bk-admin/hotelimages/" . $hotelsearchData['hotel_image_path'];
        $camposhotel     = array(
            'hotel_id' => $hotelsearchData['hotel_id'],
            'hotel_name' => $hotelsearchData['hotel_name'],
            'hotel_price' => $lowratehotel,
            'hotel_address' => $hotelsearchData['hotel_address'],
            'hotel_city' => $hotelsearchData['city_name'],
            'hotel_country' => $hotelsearchData['hotel_country'],
            'hotel_zip' => $hotelsearchData['hotel_zipcode'],
            'hotel_stars' => $hotelsearchData['hotel_stars'],
            'hotel_reviews' => $hotelsearchData['hotel_reviews'],
            'hotel_reviews_score' => $hotelsearchData['hotel_review_score'],
            'hotel_image' => $hotellocalimage,
            'hotel_description' => $hotelsearchData['hotel_short_description'],
            'hotel_supplier' => 'BK',
            'longitud' => $hotelsearchData['longitud'],
            'latitud' => $hotelsearchData['latitud']
        );
        array_push($arrayhotelsearch, $camposhotel);
    }
}

$totaldehoteles = sizeof($arrayhotelsearch);
if ($totaldehoteles > 0) {
    if ($tiposorte == 'price') {
        foreach ($arrayhotelsearch as $key => $row) {
            $aux[$key] = $row['hotel_price'];
        }
        if ($sorte == 'pricelow') {
            array_multisort($aux, SORT_ASC, $arrayhotelsearch);
        }
        if ($sorte == 'pricehigh') {
            array_multisort($aux, SORT_DESC, $arrayhotelsearch);
        }
    }
    if ($tiposorte == 'rating') {
        foreach ($arrayhotelsearch as $key => $row) {
            $aux[$key] = $row['hotel_stars'];
        }
        if ($sorte == 'ratinglow') {
            array_multisort($aux, SORT_ASC, $arrayhotelsearch);
        }
        if ($sorte == 'ratinghigh') {
            array_multisort($aux, SORT_DESC, $arrayhotelsearch);
        }
    }
    if ($tiposorte == 'review') {
        foreach ($arrayhotelsearch as $key => $row) {
            $aux[$key] = $row['hotel_reviews'];
        }
        if ($sorte == 'reviewhigh') {
            array_multisort($aux, SORT_DESC, $arrayhotelsearch);
        }
    }
}

?>