<?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']);
	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');

	//reset the conflict warning flag:
	if (isset($_SESSION['conflict_warning'])) unset($_SESSION['conflict_warning']);

	$js = '<script>'.PHP_EOL;
	$js .= 'readonly = false;'.PHP_EOL;
	
	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));
	if (!$allowEdit) 
	{
		include(SABLE_WORKING_PATH.'booking/view.php');
	}
	else
	{
?>
<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-edit" aria-hidden="true"></i>&nbsp;Modify 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..." onKeyUp="doClientRefinement(this)" style="border-bottom: none;"  value="<?php echo($row['client_full_name']); ?>" readonly/>
				</div>
				<div class="col-md-8">
					<div class="fieldHint" id="hint_edtClientName">Search by name</div>
				</div>   
			</div>
			<!-- edtClientName //-->
			<!--// selClientId -->
			<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 class="col-md-8">
					<h3 id="clientAddress"></h3>
				</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 class="col-md-8">
					
				</div>
			</div>			
			<p></p>
			<!-- selClientId //-->
			<p></p>
			<!--// edtBookingDate -->
			<div class="row forms-grid">
				<div class="col-md-4 forms-grid">
					<input name="edtBookingDate" type="date" id="edtBookingDate" onfocus="showHint('edtBookingDate'); validate_edtBookingDate();" onblur="hideHint('edtBookingDate');" placeholder="Date" onkeypress="handleEnter(this, event);" value="<?php echo($startDate); ?>">	
					<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" onfocus="showHint('cbRecurring');" disabled class="disabled"
							onblur="hideHint('cbRecurring')" onClick="showRecurringOptions();" <?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>
			<p></p>
			<!-- edtBookingDate //-->
			<p></p>
			<!--// txtBookingNote  -->
			<div class="row forms-grid">
				<div class="col-md-4 forms-grid">
					<textarea name="txtBookingNote" id="txtBookingNote" onfocus="showHint('txtBookingNote');" 
							  onblur="hideHint('txtBookingNote'); validate_txtBookingNote()" placeholder="Notes"><?= $row['booking_note']; ?></textarea>
					<label for="txtBookingNote" class="fieldHint hintUnder" id="hint_txtBookingNote">Notes</label>
				</div>
				<div class="col-md-4 forms-grid" style="display:none">
					<!-- dummy address field -->
					<textarea name="txtAddress" id="txtAddress"></textarea>
				</div>					
			</div>
			<p></p>
			<!-- txtBookingNote  //-->			
		</div>
		
		<div class="container forms-grids">
			<div class="row forms-grid">
				<!--// selBuildingId -->
					<div class="col-md-4 forms-grid">
						<div class="dropdown">
							<select name="selBuildingId" type="select" id="selBuildingId" onfocus="showHint('selBuildingId');" 
									onblur="hideHint('selBuildingId');" onkeypress="handleEnter(this, event)" placeholder="Building" onChange="setBuildingServiceList()">
								<option>Select Client First</option>
							</select>
						</div>
					</div>
					<div class="col-md-4 forms-grid">
						<div class="dropdown">
							<select name="selServiceId" type="select" id="selServiceId" onfocus="showHint('selServiceId');" 
									onblur="hideHint('selServiceId');" onkeypress="handleEnter(this, event)" placeholder="Service" onChange="setServiceItem();">
								<option>Select Building First</option>
							</select>
						</div>
					</div>
				<!-- selBuildingId //-->	
			</div>
	
			<div class="row forms-grid">
				<!--// 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="row forms-grid" id="recurring-options" style="display:none;">
			<div class="col-sm-12">
				<div class="panel-group">
					<div class="panel panel-default">
						<div class="panel-heading">
							<h4 class="panel-title">Recurring Appointment</h4>
						</div>
					
						<div id="repeat" class="panel-collapse">
							<div class="panel-body">

								<div class="container">
									<div class="row forms-grid">
										<div class="col-sm-2">
											<label class="holder" for="cbActive" style="width: 100%;">
												<span style="margin:6px;">Enabled</span>
												<input name="cbActive" type="checkbox" id="cbActive" <?php if($sa['sa_active']) echo('checked'); ?>/>
												<span class="checkmark"></span>					
											</label>
										</div>
										<div class="col-sm-10"></div>
									</div>									
									<div class="row forms-grid">
										<div class="col-sm-2">
											<label for="edtRepeatEvery">Recurs every </label>
										</div>
										<div class="col-sm-10"></div>
									</div>
									<p></p>
									<div class="row forms-grid">
										<div class="col-sm-2">
											<input type="number" class="mod" min="1" max="14" value="<?php echo($sa['sa_occurrence']); ?>" id="edtDayNumber" onChange="setSchedulingFields()"/>
										</div>
										<div class="col-sm-2">
											<div class="dropdown">
												<select id="selRepeatCycle" onChange="setSchedulingFields()">
													<option value="1" <?php if ($sa['sa_occ_type'] == 1) echo('selected'); ?>>Day</option>
													<option value="2" <?php if ($sa['sa_occ_type'] == 2) echo('selected'); ?>>Week</option>
													<option value="3" <?php if ($sa['sa_occ_type'] == 3) echo('selected'); ?>>Month</option>
												</select>
											</div>
										</div>
										<div class="col-sm-2">
											<div class="dropdown" id="divIntervalType" style="display:none;">
												<select id="selMonthType" onChange="setSchedulingFields()">
													<option value="1" <?php if ($sa['sa_month_type'] == 1) echo('selected'); ?>>By Day Numbers</option>
													<option value="2" <?php if ($sa['sa_month_type'] == 2) echo('selected'); ?>>By Week Days</option>
												</select>
											</div>
										</div>
									</div>
									<p></p>
									<div class="row forms-grid">
										<div class="col-sm-2" id="divMonthDayInterval" style="display:none;">
											<div class="dropdown">
												<select id="selMonthWeekCycle" onChange="setSchedulingFields()">
													<option value="1" <?php if ($sa['sa_month_dow_occ'] == 1) echo('selected'); ?>>1<sup>st</sup></option>
													<option value="2" <?php if ($sa['sa_month_dow_occ'] == 2) echo('selected'); ?>>2<sup>nd</sup></option>
													<option value="3" <?php if ($sa['sa_month_dow_occ'] == 3) echo('selected'); ?>>3<sup>rd</sup></option>
													<option value="4" <?php if ($sa['sa_month_dow_occ'] == 4) echo('selected'); ?>>4<sup>th</sup></option>
													<option value="5" <?php if ($sa['sa_month_dow_occ'] == 5) echo('selected'); ?>>Last</option>
												</select>
											</div>
										</div>
									</div>
									<p></p>
									<div class="row forms-grid">									
										<div class="col-sm-12" id="divDayOfWeek" style="display:none;">
											<label for="chkbxMonday" class="holder">
												<span style="margin-left: 6px;">Monday</span>
												<input type="checkbox" id="chkbxMonday" value="Monday" name="dayOfWeek" onChange="setSchedulingFields()" <?php getCheckedDOW($sadow, 1); ?>/>
												<span class="checkmark"></span>
											</label>
											<label for="chkbxTuesday" class="holder">
												<span style="margin-left: 6px;">Tuesday</span>
												<input type="checkbox" id="chkbxTuesday" value="Tuesday" name="dayOfWeek" onChange="setSchedulingFields()" <?php getCheckedDOW($sadow, 2); ?>/>
												<span class="checkmark"></span>
											</label>
											<label for="chkbxWednesday" class="holder">
												<span style="margin-left: 6px;">Wednesday</span>
												<input type="checkbox" id="chkbxWednesday" value="Wednesday" name="dayOfWeek" onChange="setSchedulingFields()"  <?php getCheckedDOW($sadow, 3); ?>/>
												<span class="checkmark"></span>
											</label>
											<label for="chkbxThursday" class="holder">
												<span style="margin-left: 6px;">Thursday</span>
												<input type="checkbox" id="chkbxThursday" value="Thursday" name="dayOfWeek" onChange="setSchedulingFields()"  <?php getCheckedDOW($sadow, 4); ?>/>
												<span class="checkmark"></span>
											</label>
											<label for="chkbxFriday" class="holder">
												<span style="margin-left: 6px;">Friday</span>
												<input type="checkbox" id="chkbxFriday" value="Friday" name="dayOfWeek" onChange="setSchedulingFields()"  <?php getCheckedDOW($sadow, 5); ?>/>
												<span class="checkmark"></span>
											</label>
											<label for="chkbxSaturday" class="holder">
												<span style="margin-left: 6px;">Saturday</span>
												<input type="checkbox" id="chkbxSaturday" value="Saturday" name="dayOfWeek" onChange="setSchedulingFields()"  <?php getCheckedDOW($sadow, 6); ?>/>
												<span class="checkmark"></span>
											</label>
											<label for="chkbxSunday" class="holder">
												<span style="margin-left: 6px;">Sunday</span>
												<input type="checkbox" id="chkbxSunday" value="Sunday" name="dayOfWeek" onChange="setSchedulingFields()"  <?php getCheckedDOW($sadow, 0); ?>/>
												<span class="checkmark"></span>
											</label>
										</div>										
									</div>
									<p></p>
									<div class="row forms-grid">
										<div class="col-sm-12" id="divDayOfMonth" style="display:none;">
											<?php printDaysOfMonth($sadom); ?>											
										</div>
									</div>
									
									<div class="row forms-grid">
										<div class="col-md-12">
											<div class="fieldHint hintUnder" id="hint_recurring"></div>
										</div>			
									</div>
									
								</div>
								<div class="panel-footer" id="pnlFooter">Repeats every day.</div>
								
							</div>
						</div>
						
						
					</div>
				</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>
				<div class="col-md-2 forms-grid">
					<input id="btnOk" type="button" onclick="modifyBooking();" value="Modify Booking" class="submit"/>
				</div>
			</div>
		<!-- buttons //-->
		</div>
	</div>
</form>
<!-- Booking //-->
<?php 
		echo($js); 
	}
?>
