<?
	$this->title = 'Drivers';
	$this->titleicon = 'road';
	$this->titleLink = '/drivers';
	$this->title2 = 'Shift';
	$this->title2icon = 'time';
	$communities = $this->communities;
	$id_admin = c::admin()->id_admin;

	$totalShifts = 0;
	$current = date( 'Ymd' );
	$first_day = $this->days[0]->format( 'Ymd' );
	$last_day = $this->days[6]->format( 'Ymd' );
	$editable = ( $last_day >= $current );
?>
<div class="container-fluid padded">
	<div class="row-fluid">
		<div class="box">
			<div class="box-header"><span class="title">Sort your shift preferences</span></div>
			<div class="padded">
				<div class="row-fluid">

					<div class="row-fluid">
						<div class="span2 padded">
							<br/><br/>
							<?php if( $first_day > $current ) { ?>
							<a class="shift-previous" href="/drivers/shift/schedule/driver/<?php echo $this->link_prev; ?>"><strong><i class="icon icon-angle-left"></i> previous</strong></a>
							<?php } ?>
						</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'); ?>
								</h5>
							</div>
						<div class="span2 padded" style="text-align:right;">
							<br/><br/>
							<a class="shift-next" href="/drivers/shift/schedule/driver/<?php echo $this->link_next; ?>"><strong>next <i class="icon icon-angle-right"></i></strong></a>
						</div>
					</div>

					<hr/>
					<div class="row-fluid sort-area">
						<div class="span4">
							<strong>Available shifts:</strong>
							<br/><br/>
							<ul class="available connected">
								<?php
								$addShiftAsAssigned = [];
								foreach( $communities as $community ) {
									foreach( $this->days as $day ){
										$segments = Crunchbutton_Community_Shift::shiftByCommunityDay( $community->id_community, $day->format( 'Y-m-d' ) );
										foreach ( $segments as $segment ) {
											$totalShifts++;
											if( Crunchbutton_Admin_Shift_Preference::adminHasShift( $id_admin, $segment->id_community_shift ) ){
												continue;
											}
											if( Crunchbutton_Admin_Shift_Assign::adminHasShift( $id_admin, $segment->id_community_shift ) ){
												$addShiftAsAssigned[] = $segment;
												continue;
											}

											?>
											<li id="<?php echo $segment->id_community_shift; ?>">
												<span class="position"></span>

												<?php echo $community->name; ?> - <?php echo $segment->dateStart()->format( 'M jS Y' ); ?>
												<br/>
												<strong><?php echo $segment->dateStart()->format( 'l' ); ?>:&nbsp;&nbsp;<?php echo $segment->startEndToString(); ?></strong>
											</li>
											<?php
										}
									}
								}
								?>
							</ul>
						</div>
						<div class="span4">
							<strong>Shifts you want to work, ranked from most to least preferred:</strong>
							<br/><br/>
							<ul class="wantwork connected">
							<?php
								$segments = Crunchbutton_Admin_Shift_Preference::shiftsByPeriod( $id_admin, $this->from->format( 'Y-m-d' ), $this->to->format( 'Y-m-d' ) );
								$count = 1;
								if( count( $addShiftAsAssigned ) > 0 ){
									foreach( $addShiftAsAssigned as $segment ){
										$locked = ( Crunchbutton_Admin_Shift_Assign::adminHasShift( $id_admin, $segment->id_community_shift ) ? 'locked' : '' );
										?>
										<li id="<?php echo $segment->id_community_shift; ?>" class="<?php echo $locked; ?>">
											<span class="position"><?php echo $count; ?>)&nbsp;</span>
											<?php echo $segment->community()->name; ?> - <?php echo $segment->dateStart()->format( 'M jS Y' ); ?>
											<br/>
											<strong><?php echo $segment->dateStart()->format( 'l' ); ?>:&nbsp;&nbsp;<?php echo $segment->startEndToString(); ?></strong>
											<?php if( $locked != '' ) { ?>
												<div><i class="icon-lock"></i> You are assigned to this shift.</div>
											<?php } ?>
										</li>
										<?php
										$count++;
									}
								}

								foreach ( $segments as $segment ) {
									$locked = ( Crunchbutton_Admin_Shift_Assign::adminHasShift( $id_admin, $segment->id_community_shift ) ? 'locked' : '' );
									?>
									<li id="<?php echo $segment->id_community_shift; ?>" class="<?php echo $locked; ?>">
										<span class="position"><?php echo $count; ?>)&nbsp;</span>
										<?php echo $segment->community()->name; ?> - <?php echo $segment->dateStart()->format( 'M jS Y' ); ?>
										<br/>
										<strong><?php echo $segment->dateStart()->format( 'l' ); ?>:&nbsp;&nbsp;<?php echo $segment->startEndToString(); ?></strong>
										<?php if( $locked != '' ) { ?>
											<div><i class="icon-lock"></i> You are assigned to this shift.</div>
										<?php } ?>
									</li>
									<?php
									$count++;
								}
							?>
							</ul>
						</div>
						<div class="span4">
							<strong>Shifts you CANNOT work:</strong>
							<br/><br/>
							<ul class="dontwantwork connected">
							<?php
								$segments = Crunchbutton_Admin_Shift_Preference::shiftsByPeriod( $id_admin, $this->from->format( 'Y-m-d' ), $this->to->format( 'Y-m-d' ), true );
								foreach ( $segments as $segment ) {
									?>
									<li id="<?php echo $segment->id_community_shift; ?>">

										<span class="position"></span>
										<?php echo $segment->community()->name; ?> - <?php echo $segment->dateStart()->format( 'M jS Y' ); ?>
										<br/>
										<strong><?php echo $segment->dateStart()->format( 'l' ); ?>:&nbsp;&nbsp;<?php echo $segment->startEndToString(); ?></strong>
									</li>
									<?php
								}
							?>
							</ul>
						</div>
					</div>
				</div>
				<hr/>
				<?php if( $totalShifts > 0 && $editable ) { ?>
				<div class="row-fluid">
					<div class="span4">
						<strong>How many shifts would you like to work this week?</strong>
					</div>
					<div class="span4">
						<select id="shifts" class="chosen-select">
							<option value=""></option>
							<?php for( $i = 12; $i >= 1; $i-- ) {
								$selected = ( $this->status->shifts == $i ) ? 'selected="selected"' : '';
								?>
								<option <?php echo $selected; ?> value="<?php echo $i; ?>"><?php echo $i; ?></option>
							<?php } ?>
						</select>
					</div>
				</div>
				<?php } ?>
				<div class="row-fluid">
				<br/><br/>
				* If you have any questions, please text us at (646) 783-1444.
				</div>
			</div>

			<?php if( $totalShifts > 0 ) { ?>
				<?php
				if( $editable ){
				?>
				<div class="form-actions">
					<!-- <button type="button" class="update-shift-driver btn btn-blue"><i class="icon-save"></i> Update</button>&nbsp;&nbsp; -->
					<button type="button" class="complete-shift-driver btn btn-green">
						<?php if( $this->status->completed == '1' ) { ?>
							<i class="icon-thumbs-up"></i> Already completed
						<?php } else { ?>
							<i class="icon-ok"></i> Save
						<?php } ?>
					</button>
				</div>
				<?php } ?>
			<?php } else { ?>
				<div class="row-fluid padded">
					<i>Sorry, there are no shift for you this week.</i>
				</div>
			<?php } ?>
			<div class="row-fluid padded">
				<a href="https://cockpit.la/drivers/shifts"><i class="icon icon-angle-right"> View your next shifts at <b>https://cockpit.la/</b></i></a>
			</div>
			<div class="divider"></div>
			</div>
		</div>
	</div>

</div>
<style type="text/css">
.position{
	font-weight: bold;
}
<?php if( $totalShifts == 0 ) { ?>
	hr, .sort-area{ display: none; }

<?php } ?>
ul.available, ul.wantwork, ul.dontwantwork {
	min-height: 50px;
	margin: 0px 25px 10px 0px;
	padding: 2px;
	border-width: 1px;
	border-style: solid;
	-webkit-border-radius: 3px;
	-moz-border-radius: 3px;
	border-radius: 3px;
	list-style-type: none;
	list-style-position: inside;
}
ul.available {
	border-color: #f8e0b1;
}
ul.wantwork {
	border-color: #add38d;
}
ul.dontwantwork{
	border-color: #999;
}
.available li, .wantwork li, .dontwantwork li {
	margin: 5px;
	padding: 5px;
	-webkit-border-radius: 4px;
	-moz-border-radius: 4px;
	border-radius: 4px;
	<?php if( $editable ) { ?> cursor: move; <?php } ?>
}
.available li {
	background-color: #fcf8e3;
	border: 1px solid #fbeed5;
	color: #c09853;
}
.wantwork li {
	background-color: #ebf5e6;
	border: 1px solid #d6e9c6;
	color: #468847;
}
.dontwantwork li{
	background-color: #F5F5F5;
	border: 1px solid #CCC;
	color: #CCC;
	text-decoration:line-through;
}
.locked{
	cursor: default !important;
}
.sortable-dragging {
	cursor: move;
	border-color: #ccc !important;
	background-color: #fafafa !important;
	color: #bbb !important;
}
.sortable-placeholder {
	height: 40px;
}
.available .sortable-placeholder {
	border: 2px dashed #f8e0b1 !important;
	background-color: #fefcf5 !important;
}
.wantwork .sortable-placeholder {
	border: 2px dashed #add38d !important;
	background-color: #f6fbf4 !important;
}
</style>
<script type="text/javascript" src="/assets/js/jquery-sortable.js"></script>
<script type="text/javascript" src="/assets/js/admin.shift.js"></script>
<script type="text/javascript">
$( document ).ready( function(){
	<?php if( $editable ) { ?>
		shift.drivers.year = '<?php echo $this->year; ?>';
		shift.drivers.week = '<?php echo $this->week; ?>';
		shift.drivers.init();
	<?php } ?>
} );
</script>