<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
//$optres ['css'][] = $path . "modules/booking_search/css/find_hotels.css";


$optres['js'][] = $path . "assets/js/moment.min.js";
$optres['css'][] = $path . "assets/css/bootstrap-datetimepicker.min.css";
$optres['js'][] = $path . "assets/js/bootstrap-datetimepicker.min.js";
$optres['js'][] = $path . "assets/js/jquery.serializejson.min.js";
$optres['js'][] = $path . "assets/js/responsive.js";

$optres ['css'][] = $module_css_path . "amodules/booking_search/css/booking_search_light.css";
$optres ['js'][] = $path . "amodules/booking_search/js/booking_search.js";

$book_title = '';
$book_btext = '';
$aff_code = 'BOOKING000000002';
$hotel_aff_location = 'Limassol';

$book_destinations = array();
$book_regions = array();

$q = "SELECT * FROM `booking_cities` where 1=1 order by full_name asc;";
$r = @mysql_query($q);
if($r && mysql_num_rows($r) > 0) {
	while ($row = mysql_fetch_assoc($r)) {
		$book_destinations [] = array_map('stripslashes', $row);
	}
}

$q = "SELECT * FROM `booking_regions` where country_code='cy' order by region_name asc;";
$r = @mysql_query($q);
if($r && mysql_num_rows($r) > 0) {
	while ($row = mysql_fetch_assoc($r)) {
		$book_regions [] = array_map('stripslashes', $row);
	}
}
/*
?>
<div id="booking-sm">
	<div class="booking-social">
		<?php
		$sm_show_likes = true;
		$sm_class_overload = "booking-sm-page";
		$sm_file = dirname(__FILE__) . "/../sm/sm.php";
		if(file_exists($sm_file)) {
			include($sm_file);
		}
		$sm_show_likes = false;
		?>
	</div>
</div>
<?php
*/
?>
<div class="box box-primary" id="bbooking-form" data-small="" data-large="">
	<div class="box-header with-border bg-blue-active" id="booking-search-header">
		<?php
		//$small = trim($module_title_small) != "" ? ' <small>' . trim(stripslashes($module_title_small)) . '<small>' : '';
		$domain_host = $_SERVER['HTTP_HOST'];
		if($on_main_server) $domain_host = "www.cypruswebsites.com";
		//echo '<' . $module_title_tag . ' class="booking-title-tag">' . stripslashes($module_title) . $small . '</' . $module_title_tag . '>';
        ?>
        <span class="booking-title-tag">
            <img src="<?php echo $protocol . $domain_host;?>/amodules/booking_search/aff/bestprices-20.png" alt="Best Prices Guaranteed" onclick="window.open('https://www.booking.com/general.en.html?;tmpl=doc/rate_guarantee','RateGuarantee','status=1,toolbar=0,menubar=0,width=450,height=550,scrollbars=1');"/>
        </span>
        <div class="box-tools pull-right">
            <button class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus fa-2x"></i></button>
        </div><!-- /.box-tools -->
	</div>
	<div class="box-body" id="booking-search-container">
		<form id="booking-search" class="form-horizontal" role="form" action="<?php echo $protocol . $_SERVER['HTTP_HOST'];?>/amodules/booking_search/aff/aff_jump.php" target="_blank" method="get">
			<?php
			$def_checkin = date("d/m", strtotime("+1 day"));
			$def_checkout = date("d/m", strtotime("+2 day"));

            $def_data_checkin = date("Y-m-d", strtotime("+1 day"));
            $def_data_checkout = date("Y-m-d", strtotime("+2 day"));

			$def_rooms = 1;
			$def_adults = 2;
			$def_children = 0;
			$min_checkout_date = date("d/m/Y", strtotime("+1 day"));
			$max_checkout_date = date("d/m/Y", strtotime("+29 day"));
			if(isset($_SESSION['hotel_search']) && isset($_SESSION['hotel_search']['checkin']) && trim($_SESSION['hotel_search']['checkin']) != ""){

                $from_format = 'Y-m-d';
                if(strpos($_SESSION['hotel_search']['checkin'], '/') !== false) {
                    $from_format = 'd/m/Y';
                }
                //echo '<pre>', var_export($_SESSION['hotel_search'], true), '</pre>';

				$dt = DateTime::createFromFormat($from_format, $_SESSION['hotel_search']['checkin']);
				$do = DateTime::createFromFormat($from_format, $_SESSION['hotel_search']['checkout']);
                $def_checkin = $dt->format('d/m');
                $def_checkout = $do->format('d/m');

                $def_data_checkin = $dt->format('Y-m-d');
                $def_data_checkout = $do->format('Y-m-d');

				$dt_reg = strtotime($dt->format('Y-m-d'));
				$min_checkout_date = date("d/m/Y", strtotime('+1 day', $dt_reg));
				$max_checkout_date = date("d/m/Y", strtotime('+28 day', $dt_reg));
				
			}
			if(isset($_SESSION['hotel_search']) && isset($_SESSION['hotel_search']['checkout']) && trim($_SESSION['hotel_search']['checkout']) != ""){
                $from_format = 'Y-m-d';
                if(strpos($_SESSION['hotel_search']['checkout'], '/') !== false) {
                    $from_format = 'd/m/Y';
                }
				//$def_checkout = $_SESSION['hotel_search']['checkout'];
                $do = DateTime::createFromFormat($from_format, $_SESSION['hotel_search']['checkout']);
                $def_checkout = $do->format('d/m');
                $def_data_checkout = $do->format('Y-m-d');
			}
			if(isset($_SESSION['hotel_search']['adults']) && intval($_SESSION['hotel_search']['adults']) >0){
				$def_adults = intval($_SESSION['hotel_search']['adults']);
			}
			?>
			
	
			<div class="booking-checkinout">
                <div class="row">
                    <div class="col-dest">
                        <label for="ck-dest" class="control-label">Resort Area</label>
                        <select id="ck-dest" class="btext ck-dest form-control input-xs" name="ck_dest">
                            <option value="" selected="selected">All Cyprus</option>
                            <optgroup label="Regions">
                                <?php
                                foreach ($book_regions as $i=>$region_data) {
                                    $selected = "";
                                    if($module_pre_area == 'r|' . $region_data['id']) $selected = 'selected="selected"';
                                    echo '<option value="r|', $region_data['id'], '" ',$selected,'>', $region_data['region_name'], '</option>';
                                }
                                ?>
                            </optgroup>
                            <optgroup label="Areas">
                                <?php
                                foreach ($book_destinations as $i=>$city_data) {
                                    $selected = "";
                                    if($module_pre_area == 'c|' . $city_data['ufi']) $selected = 'selected="selected"';
                                    echo '<option value="c|', $city_data['ufi'], '" ',$selected,'>', $city_data['full_name'], '</option>';
                                }
                                ?>
                            </optgroup>
                        </select>
                    </div>
                    <div class="col-checkin">
                        <label for="bcheckin" class="control-label">From</label>
                        <input type="btext" id="bcheckin" data-checkin="<?=$def_data_checkin;?>" value="<?php echo $def_checkin;?>" class="bsprite-calendar-ylw btext datepicker checkin form-control input-xs datepicker pull-left" data-date-format="DD MMM" name="checkin"/>
                    </div>
                    <div class="col-checkout">
                        <label for="bcheckout" class="control-label">To</label>
                        <input type="btext" id="bcheckout" data-checkout="<?=$def_data_checkout;?>" value="<?php echo $def_checkout;?>" class="bsprite-calendar-ylw btext datepicker checkout form-control input-xs datepicker" data-date-format="DD MMM" name="checkout"/>
                    </div>
                    <div class="col-rooms">
                            <label class="control-label">Rooms</label>
                            <select id="main_rooms" name="rooms" class="form-control input-xs">
                                <option value="1">1 room</option>
                                <option value="2">2 rooms</option>
                                <option value="3">3 rooms</option>
                                <option value="4">4 rooms</option>
                                <option value="5">5 rooms</option>
                                <option value="6">6 rooms</option>
                                <option value="7">7 rooms</option>
                                <option value="8">8 rooms</option>
                                <option value="9">9 rooms</option>
                                <option value="10">10 rooms</option>
                            </select>
                    </div>
                    <div class="col-adults">
                            <label class="control-label">Adults</label>
                            <select id="main_adults" name="adults" class="form-control input-xs">
                                <option value="1">1 adult</option>
                                <option selected="" value="2">2 adults</option>
                                <option value="3">3 adults</option>
                                <option value="4">4 adults</option>
                                <option value="5">5 adults</option>
                                <option value="6">6 adults</option>
                                <option value="7">7 adults</option>
                                <option value="8">8 adults</option>
                                <option value="9">9 adults</option>
                                <option value="10">10 adults</option>
                            </select>
                    </div>
                    <div class="col-kids">
                            <label class="control-label">Children</label>
                            <select id="main_children" name="children" class="form-control input-xs">
                                <option value="0">No children</option>
                                <option value="1">1 child</option>
                                <option value="2">2 children</option>
                                <option value="3">3 children</option>
                                <option value="4">4 children</option>
                                <option value="5">5 children</option>
                                <option value="6">6 children</option>
                                <option value="7">7 children</option>
                                <option value="8">8 children</option>
                                <option value="9">9 children</option>
                                <option value="10">10 children</option>

                            </select>
                    </div>
                    <div class="col-action">
                        <button type="button" class="btn btn-orange btn-xs bfind-hotels" rel="<?php echo $aff_code;?>"><i class="fa fa-search"></i> <span class="hidden-md hidden-lg hidden-sm">Search</span> </button>
                    </div>

                </div>



			</div>
			<div class="extra hidden" id="hotel_form_rooms_infos_container">
				<table id="hotel_form_rooms_infos" class="table table-hover">
					<thead>
						<tr>
							<th class="text-center">Room</th>
							<th class="text-center">Adults</th>
							<th class="text-center">Children</th>
							<th class="childrenAge">Children Age</th>
						</tr>
					</thead>
				</table>
                <small>* Children Age: 0-17 years old</small>
			</div>
			
			<div style="clear: both;"></div>
		</form>
	</div>
</div>
<?php
// Use CURL to get results if we cannot directly access some ajax URLs
$request_url = $full_addon_uri;
if(isset($_REQUEST['debug_bs'])) {
	echo '<pre>', var_export($_SESSION, true), '</pre>';
}
if(isset($on_main_server) && $on_main_server == true) {
	$submit_search_url = rtrim($request_url, '/') . '/ajax_util.php?ax_action=' . rawurlencode('/amodules/booking_search/aff/aff_jump.php') . '&ax_method=POST';
}
else {
	if(!isset($_SESSION['user_id'])) {
		$submit_search_url = rtrim($request_url, '/') . '/amodules/booking_search/aff/aff_jump.php?';
	}
	else {
		$submit_search_url = rtrim($request_url, '/') . '/ajax_util.php?ax_action=' . rawurlencode('/amodules/booking_search/aff/aff_jump.php') . '&ax_method=POST';
	}
}

$submit_search_url .= '&domain_host=' . $domain_host;
?>
<script type="text/javascript">
	var roomStr = '#';
    var min_checkin_date = '<?php echo (isset($_SESSION['hotel_search']['checkin'])) ? $_SESSION['hotel_search']['checkin'] : date("d/m/Y", strtotime("+1 day"));?>';
    var min_checkout_date = '<?php echo (isset($_SESSION['hotel_search']['checkin'])) ? $min_checkout_date : date("d/m/Y", strtotime("+1 day"));?>';
    var max_checkout_date = '<?php echo (isset($_SESSION['hotel_search']['checkout'])) ? $max_checkout_date : date("d/m/Y", strtotime("+29 day"));?>';
    var submit_search_url = '<?=$submit_search_url;?>';

    console.log(min_checkin_date);
</script>