<?php
	include_once("booking_func.php");
	$hddnBookingId = $_SESSION['caller']['args'][1];
	if ($_SESSION['user']['access_level'] < SABLE_ACCESS_MANAGER)
	{
		SableGeneralFunctions::divertFromPage();
	}
	$sql = "select booking.*, concat(client_name, ' ', client_surname) as client_full_name
			from booking 
			inner join client on client.client_id = booking.client_id 
			where booking_id = ".$hddnBookingId;
	$row	= $sableDB->getRow($sql);

	$cbRecurs = $row['booking_repeats'] ? 'checked="checked"' : '';

	$sql = "select booking__service.building_id, booking__service.service_id, building_name, service_name, service_price, service_active  
			from booking__service 
			inner join building on building.building_id = booking__service.building_id 
			inner join service on service.service_id = booking__service.service_id 
			where booking__service.booking_id = ".$hddnBookingId;

	$serv	= $sableDB->getRows($sql);

	$sad = new DateTime($row['booking_date']);
	$sa = '';
	if ($row['booking_repeats'])
	{
		$sa		= $sableDB->getRow('select *, DATE(sa_start_date) as sa_start_date from standing_appointment where sa_id = '.$row['sa_id']);
		$sadom	= $sableDB->getRows('select * from standing_appointment__dom where sa_id = '.$row['sa_id']);
		$sadow	= $sableDB->getRows('select * from standing_appointment__dow where sa_id = '.$row['sa_id']);
		$sad = new DateTime($sa['sa_start_date']);	
	}
	$startDate = $sad->format('Y-m-d');

	$js = '<script>'.PHP_EOL;
	$js .= 'readonly = true;'.PHP_EOL;
	
	/* do not show when viewing:
	if ($row['booking_repeats']) 
	{
		$js .= "showRecurringOptions();".PHP_EOL;
		$js .= "setSchedulingFields();".PHP_EOL;
	}
	*/

	foreach($serv as $itm)
	{
		$name = $itm['service_name'].' (R'.number_format($itm['service_price'], 2).')';
		$js .= 'buildServiceList('.$itm['building_id'].', '.$itm['service_id'].', "'.$itm['building_name'].'", "'.$name.'", '.$itm['service_active'].');'.PHP_EOL;
	}
	$js .= '$(document).ready(function(e){getClientBuildingList();})';
	$js .= '</script>'.PHP_EOL;

	$today = new DateTime();
	$today->setTime(0, 0);
	$allowEdit = ($cbRecurs) || (!$cbRecurs && ($sad >= $today));

?>
<script src="<?php SableGeneralFunctions::printModuleJS('booking'); ?>"></script>

<form id="frmBooking" name="frmBooking" autocomplete="on">
	<input type="hidden" id="hddnBookingId" name="hddnBookingId" value="<?php echo($hddnBookingId); ?>" />
	<div class="forms">
		<div class="container forms-grids">
			<div class="row forms-grid">
				<div class="col-md-12">
					<h3 class="title"><i class="fa fa-eye" aria-hidden="true"></i>&nbsp;View Booking</h3>
					<p></p>
				</div>
			</div>			
			<!--// edtClientName -->
			<div class="row forms-grids">
				<div class="col-md-4 forms-grid">
					<input type="text" id="edtClientName" name="edtClientName" placeholder="SEARCH..." 
						   value="<?php echo($row['client_full_name']); ?>" readonly/>
				</div>
				<div class="col-md-8">
					<h3 id="clientAddress"></h3>
				</div>
			</div>
			<div class="row forms-grids">
				<div class="col-md-4">
					<div class="fieldHint" id="hint_edtClientName">Client</div>
				</div> 
				<div class="col-md-8">
					<div class="fieldHint">Address</div>
				</div>   
			</div>			
			<!-- edtClientName //-->
			<!--// selClientId -->
			<div style="display:none;">
				<div class="row forms-grid">
					<div class="col-md-4 forms-grid">
						<div class="dropdown">
							<select name="selClientId" type="select" id="selClientId" onfocus="showHint('selClientId');" onblur="hideHint('selClientId');"
									onkeypress="handleEnter(this, event)" placeholder="Client" size="6" class="noArrow"  onClick="setClientName()" 
									onChange="getClientBuildingList();">
							<?php echo(SableDBFunctions::getOptions("client", "client_id", "concat(client_name, ' ', client_surname)", $row['client_id'])); ?>
							</select>
						</div>
					</div>
				</div>
				<div class="row forms-grid">
					<div class="col-md-4 forms-grid">
						<div class="fieldHint hintUnder" id="hint_selClientId">Client</div>
					</div>
				</div>			
				<p></p>
				<!-- selClientId //-->			
			</div>
			<p></p>
			<!--// edtBookingDate -->
			<div class="row forms-grid">
				<div class="col-md-4 forms-grid">
					<input name="edtBookingDate" type="date" id="edtBookingDate" value="<?php echo($startDate); ?>" readonly>
					<label for="edtBookingDate" class="fieldHint hintUnder" id="hint_edtBookingDate">Date</label>
				</div>
				<div class="col-md-8 forms-grid">
                  	<label class="holder" for="cbRecurring" style="width: 100%;">
						<span style="margin:6px; color: #bbb; cursor:not-allowed;">Recurring Appointment</span>
						<input name="cbRecurring" type="checkbox" id="cbRecurring" disabled class="disabled"
							<?php echo($cbRecurs); ?>/>
						<span class="checkmark" style="background-color: #bbb; cursor:not-allowed;"></span>					
                    </label>
					<label for="edtBookcbRecurringingDate" class="fieldHint hintUnder" id="hint_cbRecurring"></label>
				</div>
			</div>
			<!-- edtBookingDate //-->
			<p></p>
			<!--// txtBookingNote  -->
			<p></p>
			<div class="row forms-grid">
				<div class="col-md-4 forms-grid">
					<textarea name="txtBookingNote" id="txtBookingNote" readonly><?= $row['booking_note']; ?></textarea>
					<label for="txtBookingNote" class="fieldHint hintUnder" id="hint_txtBookingNote">Notes</label>
				</div>			
			</div>
			<p></p>
			<!-- txtBookingNote  //-->	
		</div>
		
		<div class="container forms-grids">
			<div class="row forms-grid" style="display:none;">
				<!--// selBuildingId -->
				<div class="col-md-4 forms-grid">
					<div class="dropdown">
						<select name="selBuildingId" type="select" id="selBuildingId">
							<option>Buildings</option>
						</select>
					</div>
				</div>
				<div class="col-md-4 forms-grid">
					<div class="dropdown">
						<select name="selServiceId" type="select" id="selServiceId">
							<option>Services</option>
						</select>
					</div>
				</div>
				<!-- selBuildingId //-->	
			</div>
	
			<div class="row forms-grid" style="display:none;">
				<!--// selServiceId -->
				<div class="col-md-4">
					<div class="fieldHint hintUnder" id="hint_selBuildingId">Building</div>
				</div>
				<div class="col-md-4">
					<div class="fieldHint hintUnder" id="hint_selServiceId">Service</div>
				</div>
				<!-- selServiceId //-->	
			</div>
			
			<div class="row forms-grid">
				<div id="ServiceElement">
					<!--// Selected elements will be placed here. -->
				</div>
			</div>
			<p></p>
		</div>
		
		<!--// Recurring Pattern -->
		<div class="container forms-grids">
			<div class="row forms-grid">
				<div class="col-sm-12">
					<h4><?php echo($sa['sa_description'] ?? ''); ?></h4>
				</div>
			</div>
		</div>
		<!-- Recurring Pattern //-->
		
		<div class="container forms-grids">
		<!--// buttons -->
			<div class="row forms-grid">
				<div class="col-md-2">
					<input type="button" id="btnCancel" value="Cancel"
					class="submit" onclick="window.location = '<?php SableSystem::printCallPageLink('/booking/list.php'); ?>'"/>
				</div>
				<?php if ($allowEdit) { ?>
				<div class="col-md-2 forms-grid">
					<input id="btnOk" type="button" onclick="editBooking('<?php echo(SableSystem::sableEncode($hddnBookingId)); ?>')" value="Modify Booking" class="submit"/>
				</div>
				<?php } ?>
			</div>
		<!-- buttons //-->
		</div>
	</div>
</form>
<!-- Booking //-->
<?php echo($js); ?>
