<?
	$this->title = 'Drivers';
	$this->titleicon = 'road';
	$this->titleLink = '/drivers';
	$this->title2 = 'Summary';
	$this->title2icon = 'time';
	$this->titleLink = '/drivers/shift/summary/';

	$communities = [];

	foreach( $this->days as $day ) {
		$segments = Crunchbutton_Community_Shift::shiftsByDay( $day->format( 'Y-m-d' ) );
		foreach( $segments as $segment ){
			if( !$communities[ $segment->id_community ] ){
				$communities[ $segment->id_community ] = [];
			}
			if( !$communities[ $segment->id_community ][ $day->format( 'Y-m-d' ) ] ){
				$communities[ $segment->id_community ][ $day->format( 'Y-m-d' ) ] = [];
			}
			$communities[ $segment->id_community ][ $day->format( 'Y-m-d' ) ][] = $segment;
		}
	}
?>
<div class="container-fluid padded">
	<div class="row-fluid">
		<div class="box">
			<div class="box-header">
				<div class="row-fluid">
					<div class="row-fluid">
						<div class="span2 padded">
							<br/>
							<a class="shift-previous" href="/drivers/shift/summary/<?php echo $this->link_prev; ?>"><strong><i class="icon icon-angle-left"></i> previous</strong></a>
						</div>
							<div class="span8 padded" style="text-align:center;">
								<h5>
									From <?php echo $this->from->format('M jS Y') ?> to <?php echo $this->to->format('M jS Y'); ?>
									<?php if( $this->current ) { ?>
										<div style="color:green;">Current week</div>
									<?php } ?>
								</h5>
							</div>
						<div class="span2 padded" style="text-align:right;">
							<br/>
							<a class="shift-next" href="/drivers/shift/summary/<?php echo $this->link_next; ?>"><strong>next <i class="icon icon-angle-right"></i></strong></a>
						</div>
				</div>
				</div>
			</div>
			<div class="box-content">

			<div class="padded">
			<table width="100%" id="header_float" style="position:fixed;top:0;display:none;background:#FFF;">
				<tr>
					<?php foreach( $this->days as $day ) {
						$dark = ( $dark == '#F5F5F5' ) ? '' : '#F5F5F5';
					?>
						<th width="14%" style="background:<?php echo $dark; ?>" id="float-<?php echo $day->format( 'l' ); ?>">
							<div class="row-fluid padded">
								<div class="span12">
									<strong style="font-size:16px;color:#666;">
										<?php echo $day->format( 'l' ); ?>
										<div style="font-size:10px;font-weight:normal;color:#CCC;"><?php echo $day->format( 'M' ); ?> <?php echo $day->format( 'd' ); ?></div>
									</strong>
								</div>
							</div>
						</th>
					<?php } ?>
				</tr>
				</table>
				</div>

				<div class="padded" id="header_columns">
					<table width="100%">
						<tr>
							<?php
							$dark = false;
							foreach( $this->days as $day ) {
								$dark = ( $dark == '#F5F5F5' ) ? '' : '#F5F5F5';
							?>
								<th id="<?php echo $day->format( 'l' ); ?>" width="14%" style="background:<?php echo $dark; ?>">
									<div class="row-fluid padded">
										<div class="span12">
											<strong style="font-size:16px;color:#666;">
												<?php echo $day->format( 'l' ); ?>
												<div style="font-size:10px;font-weight:normal;color:#CCC;"><?php echo $day->format( 'M' ); ?> <?php echo $day->format( 'd' ); ?></div>
											</strong>
										</div>
									</div>
								</th>
							<?php } ?>
						</tr>
						<?php $dark = ''; ?>
						<?php
							foreach( $communities as $community ) {

								echo '<tr>';

									foreach( $this->days as $day ) {

										$_day = $day->format( 'Y-m-d' );
										$weekday = $day->format( 'N' );

										$dark = ( $weekday % 2 ) ? '' : '#F5F5F5';

										echo '<td valign="top" width="14%" style="background:'. $dark . '">';

										$segments = $community[ $_day ];

										if( count( $segments ) > 0 ){

											foreach( $segments as $segment ){

												$isHidden = $segment->isHidden();

												$_drivers = $segment->getDrivers();
												$firstDayOfWeek = $segment->firstDayOfWeek()->format( 'Y-m-d' );
												$lastDayOfWeek = $segment->lastDayOfWeek()->format( 'Y-m-d' );
												$hasDriver = false;
												$drivers = [];
												foreach( $_drivers as $driver ){
													$hasDriver = true;
													$ranking = 0;
													$highestRanking = 0;
													$preference = $segment->getAdminPreferencesByDriver( $driver->id_admin );
													$highestRanking = Crunchbutton_Admin_Shift_Preference::highestRankingByPeriod( $driver->id_admin, $firstDayOfWeek, $lastDayOfWeek );
													if( $preference->id_admin_shift_preference ){
														$ranking = $preference->ranking;
													}

													$isPermanent = ( Crunchbutton_Admin_Shift_Assign_Permanently::adminIsPermanently( $driver->id_admin, $segment->id_community_shift ) ) ? 'checked="checked"' : '';

													$isFirstWeek = Crunchbutton_Admin_Shift_Assign::isFirstWeek( $driver->id_admin, $segment->dateStart()->format( 'Y-m-d H:i' )  );

													$font_color = ( $isPermanent ? ';color:blue;' : '' );
													$background_color = ( $isFirstWeek ? ';background-color:#45ef7f;' : '' );

													$ordersPerHour = $driver->ordersPerHour();

													$_driver = '<div style="letter-spacing:-.09em;font-size:11px;padding:2px 0;white-space: nowrap;' . $font_color . $background_color . '">- ';
													$_driver .= $driver->name;
													if( $ordersPerHour ){
														$_driver .= ": $ordersPerHour o/h";
													}

													$_driver .= '</div>';

													$drivers[] = $_driver;
												}
												if( $hasDriver ){
													$drivers = join( '', $drivers );
												}

												?><div class="row-fluid">

													<div id="container_shift_<?php echo $segment->id_community_shift; ?>" class="shift span11 <?php if( $isHidden ){ echo 'isHidden'; } ?> <?php if( $hasDriver ){ echo 'hasDrivers'; } else { echo 'hasntDrivers'; } ?>">
														<a class="modal-shift-assign" href="/drivers/shift/summary/shift/<?php echo $segment->id_community_shift; ?>" title="Assign drivers">
															<div>
																<?php if( $segment->id_community_shift_father || $segment->recurring > 0 ){ ?>
																	<i class="icon-retweet" title="It is a recurring event"></i>&nbsp;
																<?php } ?>
															<strong><?php echo $segment->community()->name; ?></strong></div>
															<span class="community-timezone" title="<?php echo $segment->fullDate(); ?>">
																<strong><?php echo $segment->startEndToString(); ?></strong> (<?php echo $segment->timezoneAbbr(); ?>)
															</span>
															<span class="pst-timezone" title="<?php echo $segment->fullDate( c::config()->timezone ); ?>">
																<strong><?php echo $segment->startEndToString( c::config()->timezone ); ?></strong> (PST)
															</span>
															<?php if( $hasDriver ) { ?>
																<span><?php echo $drivers; ?></span>
															<?php } ?>
														</a>
														<label>
															<input type="checkbox" <?php if( $isHidden ){ echo 'checked="checked"'; } ?> class="hide-shift" style="float:left;" value="<?php echo $segment->id_community_shift; ?>" name="hide_<?php echo $segment->id_community_shift; ?>">
															<div style="font-size: 11px;font-weight: bold;">&nbsp;Hide shift</div>
															<div style="clear:both;"></div>
														</label>
													</div>
												</div><?php
											}
										}
										echo '<br/>';
										echo '<br/>';
										echo '</td>';
									}
								echo '</tr>';
							}
						?>
					</table>
					<div class="divider"></div>
				</div>
			</div>
			<div class="padded">
				<button style="margin-left:20px;" class="btn btn-default" onclick="shift.community.toggleTimezone();"><i class="icon-time"></i> Show hours in&nbsp;
				<span class="pst-timezone">community timezone</span>
				<span class="community-timezone">PST timezone</span>
				</button>
			</div>
		</div>
	</div>
</div>

<div class="modal hide fade" id="modal-shift" style="display: none;">
	<div class="modal-header">
		<button class="close" data-dismiss="modal" type="button">×</button>
		<h6 id="modal-shift-title"></h6>
	</div>
	<div class="modal-body" id="modal-shift-body"></div>
	<div class="modal-footer">
		<button class="btn btn-default" data-dismiss="modal">Close</button>
	</div>
</div>

<style type="text/css">
	.shift{
		min-height: 50px;
		margin: 0px 25px 10px 8px !important;
		padding: 5px;
		border-width: 1px;
		border-style: solid;
		-webkit-border-radius: 3px;
		-moz-border-radius: 3px;
		border-radius: 3px;
	}
	.shift.hasDrivers{
		background-color: #ebf5e6;
		border: 1px solid #d6e9c6;
	}
		.shift.hasDrivers a, .shift.hasDrivers div{
			color: #468847;
		}
	.hasntDrivers{
		background-color: #ceb2b4;
		border: 1px solid #eca49f;
	}
		.shift.hasntDrivers a, .shift.hasntDrivers div{
			color: #8c3533;
		}
		.shift.hasntDrivers a, .shift.hasntDrivers div{
			color: #8c3533;
		}
	.shift span{
		display: block;
		margin: 5px 0;
	}
	#modal-shift{
		width: 900px !important;
        margin: 0 auto;
	}
	.isHidden{
		background-color: #7ca3b7 !important;
		border: 1px solid #618193 !important;
	}
		.shift.isHidden a, .shift.isHidden div{
			color: #536f7f !important;
		}
</style>
<script type="text/javascript" src="/assets/js/admin.shift.js"></script>
<script type="text/javascript">
$( document ).ready( function(){
	shift.summary.init();
} );
</script>
<script type="text/javascript" src="/assets/js/jquery.autosize-min.js?v=<?=getenv('HEROKU_SLUG_COMMIT')?>"></script>
<script type="text/javascript">
var getHeaderSize = function(){
	var header_columns = $( '#header_columns' );
	header_columns.find( 'th' ).each( function(){
		var th = $( this );
		if( th.attr( 'id' ) ){
			var th_float = $( '#float-' + th.attr( 'id' ) );
			if( th_float ){
				th_float.width( th.width() );
			}
		}
	} );
	var header_float = $( '#header_float' );
	header_float.width( header_columns.width() );
	showHideHeader();
};
var showHideHeader = function(){
	var scroll_top = $( window ).scrollTop();
	var header_float = $( '#header_float' );
	if( scroll_top >= 265 ){
		$( '#header_float' ).fadeIn( 200 );
	} else {
		$( '#header_float' ).fadeOut( 200 );
	}
};
$( function() {

	setTimeout( function(){
		getHeaderSize();
	}, 100 );

	$( window ).scroll( function(){
		showHideHeader();
	} );

	$( window ).resize( function(){
		getHeaderSize();
	} );

});
</script>