<?php
include_once('../db.functions.php');

$START_DATE = $_GET['START_DATE'];
$END_DATE = $_GET['END_DATE'];
$PAX = $_GET['PAX'];

if(empty($_GET['ID']) || $_GET['ID'] != 'xGWs44WhqswJP37gVD7466U6Zr8lS4AA' ) {
    echo 'Direct access to the script is not allowed';
    exit();
}

if( empty($_GET['PAX']) || !is_numeric($_GET['PAX']) ) {
    echo 'Pax must be in number';
    exit();
}

function validateDate($date)
{
    $d = DateTime::createFromFormat('Y-m-d', $date);
    return $d && $d->format('Y-m-d') == $date;
}

if( validateDate($START_DATE) == FALSE || validateDate($END_DATE) == FALSE )
{
    echo 'Date does not exist in gregorian calendar';
    exit(); 
}

if ((strtotime($START_DATE)) > (strtotime($END_DATE)))
{
    echo 'End date cannot be earlier than start date';
    exit();
}

                    		$result_check = DB::query("SELECT * FROM bookings WHERE START_DATE <= %s AND END_DATE >= %s AND BOOKING_STATUS_REF NOT IN ('CN','BS','AS')",$END_DATE,$START_DATE);
                            foreach ($result_check as $row_check) 
                            {
                                    $row_double_check=DB::queryFirstRow("SELECT * FROM `config_hall` WHERE FAC_ID = %s",$row_check['FACILITY_REF']);
                                    $FACILITY_REF[] = $row_double_check['FAC_AVAILABILITY'];
                            }

                            $FAC_AVAILABILITY = join(',',$FACILITY_REF);  

                            $SELECTED_HALL = join(',',$FAC_SELECTED);

                            $NEW_ARRAY = $FAC_AVAILABILITY.','.$SELECTED_HALL;
                            
                            $result_display = DB::query("SELECT * FROM `config_hall` WHERE UPDATE_ILA = 'y' AND FAC_CAPACITY >= $PAX");

                            foreach ($result_display as $row_display)
                            {
                                $FAC_ID = $row_display['FAC_ID'];
                                $FAC_NAME = $row_display['FAC_NAME'];
                                $FAC_FILENAME = $row_display['FAC_FILENAME'];
                                $FAC_CAPACITY = $row_display['FAC_CAPACITY'];

                                $arr[] = array(
								        'FAC_ID' => $FAC_ID,
								        'FAC_NAME' => $FAC_NAME,
								        'FAC_FILENAME' => $FAC_FILENAME,
										'FAC_CAPACITY' => $FAC_CAPACITY);	
                            }

echo json_encode($arr);

?>