<?php  
$hours = $this->hours;
$restaurant = $this->restaurant;

$isOpen = $restaurant->open();

$timezone = new DateTimeZone( $restaurant->timezone );

$date_start = new DateTime( 'now ', $timezone );
$multipleOf = 5;
$time = new DateTime( 'now', $timezone );
$minutes = round( ( ( $time->format( 'i' ) + 10 ) + $multipleOf / 2 ) / $multipleOf ) * $multipleOf;
$minutes -= $time->format( 'i' );
$minutes -= 20;
$date_start->modify( $minutes . ' minutes' );
$hour_override_date_start = $date_start->format( 'm/d/Y' );
$hour_override_date_start_hour = $date_start->format( 'h:i' );
$hour_override_date_start_ampm = $date_start->format( 'A' );

$date_end = $restaurant->next_open_time();
if( $date_end ){
	$hour_override_date_end = $date_end->format( 'm/d/Y' );
	$hour_override_date_end_hour = $date_end->format( 'h:i' );
	$hour_override_date_end_ampm = $date_end->format( 'A' );
}
?>
<div class="row-fluid">
	<div class="container-fluid">
		<div class="row-fluid">
			<div class="span12">
				<div class="box">
					<div class="box-header"><span class="title"><?php echo $restaurant->name; ?>:: Add date</span></div>
					<div class="box-content padded">
						<form id="restaurant-bank-info" onsubmit="return false;">
							<div class="row-fluid">
								<div class="span12">
									<div class="span3">
										<strong>Start at:</strong>
									</div>
									<div class="span3">
										<input type="text" value="<?php echo $hour_override_date_start; ?>" class=" span12" id="hour_override_date_start" name="hour_override_date_start">
									</div>
									<div class="span1">
										Hour:
									</div>
									<div class="span2">
										<input type="text" value="<?php echo $hour_override_date_start_hour; ?>" name="hour_override_date_start_hour" id="hour_override_date_start_hour" class="span10"> 
									</div>
									<div class="span2">
										<select class="span10" name="hour_override_date_start_ampm" id="hour_override_date_start_ampm">
											<option <?php if( $hour_override_date_start_ampm == 'AM' ){ echo 'selected="selected"'; } ?> value="AM">AM</option>
											<option <?php if( $hour_override_date_start_ampm == 'PM' ){ echo 'selected="selected"'; } ?> value="PM">PM</option>
										</select>
									</div>
								</div>
							</div>
							<div class="row-fluid">
								<div class="span12">
									<div class="span3">
										<strong>Finish at:</strong>
									</div>
									<div class="span3">
										<input type="text" value="<?php echo $hour_override_date_end; ?>" class=" span12" id="hour_override_date_end" name="hour_override_date_end">
									</div>
									<div class="span1">
										Hour:
									</div>
									<div class="span2">
										<input type="text" value="<?php echo $hour_override_date_end_hour; ?>" name="hour_override_date_end_hour" id="hour_override_date_end_hour" class="span10">
									</div>
									<div class="span2">
										<select class="span10" name="hour_override_date_end_ampm" id="hour_override_date_end_ampm">
											<option <?php if( $hour_override_date_end_ampm == 'AM' ){ echo 'selected="selected"'; } ?> value="AM">AM</option>
											<option <?php if( $hour_override_date_end_ampm == 'PM' ){ echo 'selected="selected"'; } ?> value="PM">PM</option>
										</select>
									</div>
								</div>
							</div>
							<div class="row-fluid">
								<div class="span12">
									<div class="span4">
										<strong>Timezone</strong>
									</div>
									<div class="span8">
										 <strong><?php echo $timezone->getName(); ?></strong>
									</div>
								</div>
							</div>
							<div class="row-fluid">
								<div class="span12">
									<div class="span4">
										<strong>Notes</strong>
									</div>
									<div class="span8">
										<input type="text" name="hour_override_notes" id="hour_override_notes" maxlength="250" />
									</div>
								</div>
							</div>
							<div class="row-fluid">
								<div class="span12">
									<div class="span4">
										<strong>The restaurant will be</strong>
									</div>
									<div class="span3">
										<select name="hour_override_type" id="hour_override_type">
												<option value="<?php echo Crunchbutton_Restaurant_Hour_Override::TYPE_CLOSED; ?>" <?php if( $isOpen ){ echo 'selected="selected"'; } ?>>Closed</option>
												<option value="<?php echo Crunchbutton_Restaurant_Hour_Override::TYPE_OPENED ?>" <?php if( !$isOpen ){ echo 'selected="selected"'; }  ?>>Opened</option>
										</select>
									</div>
								</div>
							</div>
							<div class="row-fluid">
								<div class="span12">
									<div class="span4">
									</div>
									<div class="span3">
										<button type="submit" onclick="hour_override.save();" class="btn btn-blue restaurant-bank-info-button"><i class="icon-save"></i> Save </button> 
										<i class="restaurant-bank-info-button-loading icon-spinner icon-spin" style="display:none;"></i>
									</div>
								</div>
							</div>
						</form>
					</div>
				</div>
			</div>
		</div>
	</div>
	<?php if( $hours->count() > 0 ) { ?>
	<div class="box">
		<div class="box-header"><span class="title"><?php echo $restaurant->name; ?>:: Hours: <?php echo $restaurant->timezone; ?></span></div>
		<div class="box-content">
			<table class="table table-normal">
				<thead>
					<tr>
						<td>Start at</td>
						<td>Finish at</td>
						<td>Status</td>
						<td>Notes</td>
						<td></td>
					</tr>
				</thead>
					<?php foreach ( $hours as $hour ) {
						?>
						<tr>
							<td nowrap="nowrap">
								<?php echo $hour->date_start()->format('M jS Y - D'); ?>
								<br/>
								<?php echo $hour->date_start()->format('g:i:s A'); ?>
							</td>
							<td nowrap="nowrap">
								<?php echo $hour->date_end()->format('M jS Y - D'); ?>
								<br/>
								<?php echo $hour->date_end()->format('g:i:s A'); ?>
							</td>
							<td><?php echo $hour->status(); ?></td>
							<td><?php echo $hour->notes; ?> - <?php echo $hour->admin()->name; ?></td>
							<td><button type="button" onclick="hour_override.remove( '<?php echo $hour->id_restaurant_hour_override; ?>' );" class="btn btn-small btn-red"><i title="Remove" class="icon-trash"/></button> </td>
						</tr>
						<?php
					} ?>
			</table>
		</div>
	</div>
	<?php } ?>
</div>