<?php
	require_once(SABLE_WORKING_PATH.'attendance/attendance_func.php');
	$checkedout = checkAttendance();
	/**
	If this user is checked in, but not checked out, then continue with the schedule, but give them the option to check out. Ideally we also show a timer indicating how long they have been checked in for.
	**/

	include_once(SABLE_WORKING_PATH.'booking/booking_func.php');

	function getClientsOverview()
	{
		
		/**
		Custom change requested here by Champooches to only show the next customer so that they drivers don't print the list and work according to their own schedules:
		**/
		$showOnlyNext = true;
		
		$bgcol	= '#eee';
		$pdng	= '8px';
		
		$rows = getDriverScheduleData($showOnlyNext);
		
		if (empty($rows))
		{
			$html = '
			<div class="col-sm-12 text-center" style="width:100%; background:'.$bgcol.'; padding:'.$pdng.';">
				<p><i class="fa fa-2x fa-ban error" style="color:red;"></i></p>
				<h3>No bookings scheduled for today.</h3>
            	<div class="row forms-grid">
					<div class="col-sm-4">
					</div>
					<div class="col-sm-4">
            			<button onClick="window.location.reload();">Retry</button>				
					</div>
					<div class="col-sm-4">
					</div>
				</div>
			</div>';
		}
		else
		{
			$html = '';
			foreach($rows as $row)
			{
				$link	= SableSystem::getParameterLink('themes/sable/mobi', 'index.php', $row['bs_id']);
				$sched	= new DateTime($row['booking_time']);
				$html  .= '
					<div class="col-sm-12 mob-usr-block">
						<a href="'.$link.'">
							<div>
								<div class="mob-usr-block-title";>
									<h3 class="sub">'.$sched->format('H:i').'&nbsp;<i class="fa fa-arrow-right success"></i>&nbsp;'.$row['client_full_name'].'</h3>
								</div>
								<div style="width:90%;"><h4>'.$row['address'].'</h4></div>
							</div>	
							<div class="mob-usr-block-addr">
								<i class="fa fa-chevron-right fa-2x"></i>
							</div>
						</a>
					</div>';
				unset($sched);
			}
		}
		return $html;
	}
	
	function printClientsOverview()
	{
		$html = getClientsOverview();
		echo($html);
	}

if ($_SESSION['user']['access_level'] < SABLE_ACCESS_MANAGER)
{
?>
<script src="<?php SableGeneralFunctions::printModuleJS('attendance'); ?>"></script>
<input type="hidden" id="hddnLat" />
<input type="hidden" id="hddnLng" />
<div class="container no-padding" style="margin-top:-25px;">
	<div class="row">
		<div class="col-sm-12">
			<div class="navbar section no-padding" role="navigation" style="background-color:#3cced6;">
				<div class="heading" style="margin:10px;">
					<div class="container no-padding">
						<div class="row">
							<div class="col-xs-2">
								<a class="navbar-minimalize" title="Menu" href="<?php SableSystem::printSableLinkPath('login/logout.php'); ?>">
									<span class="fa fa-2x fa-sign-out-alt">&nbsp;</span>
								</a>
							</div>
							<div class="col-xs-10">
								<h3 class="sub">
									<?= $_SESSION['user']['user_name'].' '.$_SESSION['user']['user_surname'].' - <span style="color:#474d4b">'.date('j F Y').'</span>' ?></h3>
							</div>							
						</div>					
					</div>
				</div>
			</div>
		</div>
	</div>	
</div>
<?php
}

if (!$checkedout){ ?>
<div class="container no-padding">
	<div class="row-fluid">
		<?php printClientsOverview(); ?>
	</div>
</div>
<div class="container no-padding">
	<div class="row-fluid">
		<button type="button" onClick="checkOut()" style="font-size:16px;"><i class="fa fa-user-clock"></i>&nbsp;Check Out</button>
	</div>
</div>
<?php } else { ?>
<div class="container no-padding">
	<div class="row-fluid">
		<button type="button" onClick="checkIn()" style="font-size:16px;"><i class="fa fa-user-clock"></i>&nbsp;Check In</button>
	</div>
</div>
<?php } ?>
<script src="/themes/sable/dashboard/dashboard.js"></script>
<script>
	$(document).ready(function(){getCurrentPosition("hddnLat", "hddnLng")});
</script>