<?php 
spl_autoload_register('splAutoLoader');

function splAutoLoader($className){
	$path="panel/assets/classes/"; //path to classes folder
	$extensation = ".classes.php"; //file extensation for the files in classes folder
	$fullpath = $path.$className.$extensation;

	if (!file_exists($fullpath)) {
		return false;
	}

	include_once $fullpath;

}
//================this fuction returns the free rooms todayonly
//================it checks if the room is free today for booking
function GetFreeRooms($roomID,$totalRooms){

	$database = new Database();
    $db = $database->getConnection();
     $conn= $db;
     $checkinn =date('Y-m-d'); // fetch today date as checkin date 
     $checkout =date('Y-m-d', strtotime("+1 day", strtotime($checkinn)));
///////==============get all rooms====
		$totalRooms=$totalRooms;
//////==roombookingStatus 
		//=0 to be checked in
		//=1 to becked out
		//=2= checked out
//////=============check if room has ever been booked===
		$sqlbooking="SELECT * FROM roombooking WHERE roomID='$roomID'";
		$stmt = $conn->query($sqlbooking);
        $result=$stmt->fetchAll(PDO::FETCH_ASSOC);
        if (sizeof($result)>0) {
   // 	//================if yes then get all booked rooms that ====
        	//===total number of rooms still booked inisialised to 0
        	$totalBookedRooms=0;
        	//====select all rooms that are still booked
        	$sqlbookedRooms="SELECT roombooking.numberRooms FROM roombooking WHERE roomID='$roomID' and roombookingStatus!='2'";

        	$room_stmt = $conn->query($sqlbookedRooms);
        	//$room_result=$room_stmt->fetchAll(PDO::FETCH_BOTH);
        	while($room_result=$room_stmt->fetch(PDO::FETCH_ASSOC)){
                 $totalBookedRooms=$totalBookedRooms+$room_result['numberRooms'];
            }

             //==select rooms that are booked but free===========
            //loop around to see if the dates booked are free or not======
            //===total number of rooms  booked but free inisialised to 0
            $totalBookedfreeRooms=0;

        	 $sqlbookedfree="SELECT roombooking.numberRooms FROM roombooking WHERE roomID='$roomID' and roombookingStatus!='2' and ('$checkinn' NOT BETWEEN checkinn and checkout) and ('$checkout' NOT BETWEEN checkinn and checkout) and ('$checkinn'>checkout and '$checkout'> checkout) or ('$checkinn'< checkinn and '$checkout'< checkinn)";

        	 	$free_stmt = $conn->query($sqlbookedfree);        	 	
        	 	
        	 		while($free_result=$free_stmt->fetch(PDO::FETCH_ASSOC)){
                 		
                        $totalBookedfreeRooms=$totalBookedfreeRooms+$free_result['numberRooms'];
            		}

        	 	
        	 	//==================calculate free rooms
            		//formulae= 
            		//free rooms= Totalroom-totalbookedroomsAndNotAvailable+totalBookedButAvailable;
            		return sprintf($toatlFree=($totalRooms-$totalBookedRooms)+$totalBookedfreeRooms);
        	 	
        	
        }else{
        	return sprintf($totalRooms); 
        }

        



// 	//=====================return the room numbers=======
    
     //return var_dump($result);
}


function GetDriverExpirations($driverID){
    $database = new Database();
    $db = $database->getConnection();
    $conn= $db;

    //========todays Data
    $todayDate =strtotime(date("Y-m-d"));


    //==========get the driver details
    $sqlDriver="SELECT * FROM drivers WHERE driverID='$driverID'";
    $stmt = $conn->query($sqlDriver);
    $result=$stmt->fetch(PDO::FETCH_ASSOC);

    if (sizeof($result)>0) {
        //====check for any document that has expired

        if (strtotime(date($result['LicenceExpiration']))<$todayDate) {
            
            ?>
            <b style="color: red;">Licence</b>
            <?php
        }

        if (strtotime(date($result['permitExpiration']))<$todayDate) {
            ?>
            <b style="color: red;">Permit</b>
            <?php
        }


        if (strtotime(date($result['permitExpiration']))>$todayDate && strtotime(date($result['LicenceExpiration']))> $todayDate) {
            ?>
            <b class="success">- - -</b>
            <?php
        }

        }




}

echo GetDriverExpirations(15);
 ?>
 

