<style>
	.btn-green{
		width: 20em;
		height: 6em;
		top: 90%;
		left: 80%;
		position:fixed;
	}
</style>
<?php
	$shift = $this->shift;
?>
<div class="row-fluid">
	<div class="container-fluid">
		<div class="row-fluid">
			<div class="span12">
						<form>
							<input type="hidden" value="<?php echo $shift->id_community_shift; ?>" id="id_community_shift" name="id_community_shift" />
							<div class="row-fluid">
								<div class="span12">
									<div class="span2">
										Community:
									</div>
									<div class="span7">
										<strong><?php echo $shift->community()->name; ?></strong>
									</div>
								</div>
							</div>
							<div class="row-fluid">
								<div class="span12">
									<div class="span2">
										Start at:
									</div>
									<div class="span7">
										<?php echo $shift->dateStart()->format( 'M jS Y ' ); ?>
										<strong><?php echo $shift->dateStart()->format( 'g:i A' ); ?></strong>
									</div>
								</div>
							</div>
							<div class="row-fluid">
								<div class="span12">
									<div class="span2">
										End at:
									</div>
									<div class="span7">
										<?php echo $shift->dateEnd()->format( 'M jS Y ' ); ?>
										<strong><?php echo $shift->dateEnd()->format( 'g:i A' ); ?></strong>
									</div>
								</div>
							</div>
							<div class="row-fluid">
								<div class="span12">
									<div class="span2">
										Timezone:
									</div>
									<div class="span7">
										<strong><?php echo $shift->timezone(); ?></strong>
									</div>
								</div>
							</div>
							<?php if( $shift->isHidden() ){ ?>
								<div class="row-fluid">
									<div class="span12">
										<strong style="color:red;">This shift is hidden from drivers!</strong>
									</div>
								</div>
							<?php } ?>
							<div class="box-content" style="border:1px solid #cdcdcd;">
								<table width="100%" class="table table-normal">
									<thead>
										<tr>
											<td style="width:25px;"></td>
											<td style="width:100px;">Driver</td>
											<td style="width:60px;">Ranking</td>
											<td>Shifts wants to work</td>
											<td>Total assigned shifts</td>
											<td>
												<?php if( $shift->isRecurring() ) { ?>
													Make it permanent?
												<?php } ?>
											</td>
											<td style="width:200px;">Notes</td>
										</tr>
									</thead>
									<tbody>
									<?php
									$alreadyShown = [];
									$driversDontWantWorkHere = [];
									$preferences = $shift->getAdminPreferences();
									$hasDrivers = false;
									$week = $shift->week();
									$year = $shift->year();
									foreach( $preferences as $preference ){
										if( intval( $preference->ranking ) > 0 ){
											$hasDrivers = true;
										} else if( intval( $preference->ranking ) == 0 ){
											$driversDontWantWorkHere[] = $preference->admin();
										}
									}
									if( $hasDrivers ){
									?>
									<tr>
										<td colspan="7">
											<strong><center>Drivers that want to work at this shift</center></strong>
										</td>
									</tr>
									<?php

									$shift_date = new DateTime( $shift->dateStart()->format( 'Y-m-d' ), new DateTimeZone( c::config()->timezone  ) );

									if( $shift_date->format( 'l' ) == 'Thursday' ){
										$thursday = $shift_date;
									} else {
										$shift_date->modify( 'last thursday' );
										$thursday = $shift_date;
									}

									$firstDayOfWeek = $thursday->format( 'Y-m-d' );
									$thursday->modify( '+ 6 days' );
									$lastDayOfWeek = $thursday->format( 'Y-m-d' );

									foreach( $preferences as $preference ){
										$admin = $preference->admin();

										$highestRanking = $preference->highestRankingByPeriod( $admin->id_admin, $firstDayOfWeek, $lastDayOfWeek );

										$alreadyShown[] = $admin->id_admin;
										if( $preference->ranking == 0 ){
											continue;
										}
										$status = Crunchbutton_Admin_Shift_Status::getByAdminWeekYear( $admin->id_admin, $week, $year );
										$checked = ( Crunchbutton_Admin_Shift_Assign::adminHasShift( $admin->id_admin, $shift->id_community_shift ) ) ? 'checked="checked"' : '';
										$checkedPermanently = ( Crunchbutton_Admin_Shift_Assign_Permanently::adminIsPermanently( $admin->id_admin, $shift->id_community_shift ) ) ? 'checked="checked"' : '';
										$adminShifts = Crunchbutton_Admin_Shift_Assign::shiftsByAdminPeriod( $admin->id_admin, $firstDayOfWeek, $lastDayOfWeek );
										$totalShifts = $adminShifts->count();
										?>
										<tr>
											<td>
												<input type="checkbox" class="icheck" <?php echo $checked; ?> value="<?php echo $admin->id_admin; ?>" name="form-id_admin" id="form-id_admin-<?php echo $admin->id_admin; ?>">
											</td>
											<td>
												<?php echo $admin->name; ?>
												<br/>
												<?php echo $admin->phone(); ?>
												<br/>
												<?php
													$payment_type = $admin->payment_type();
													if( $payment_type->payment_type == Crunchbutton_Admin_Payment_Type::PAYMENT_TYPE_HOURS ){
														echo 'Hourly';
													} else if( $payment_type->payment_type == Crunchbutton_Admin_Payment_Type::PAYMENT_TYPE_HOURS ){
														echo 'Commission';
													} else {
														echo '-';
													}
													echo '<br>';
													if( $payment_type->using_pex == 1 ) {
														echo 'Using pex card';
													} else {
														echo 'Not using pex card';
													}
													$ordersPerHour = $admin->ordersPerHour();
													if( $ordersPerHour ){
														echo "<br>$ordersPerHour orders/hour";
													}
												?>
											</td>
											<td style="background:#009c48;color:#FFF;text-align:center;"><strong><?php echo $preference->ranking; ?></strong> of <?php echo $highestRanking; ?></td>
											<td>
												<?php if( intval( $status->shifts ) > 0 ){ echo $status->shifts; } else { echo '-'; } ; ?>
											</td>
											<td><?php echo $totalShifts; ?></td>
											<td>
												<?php if( $shift->isRecurring() ) { ?>
													<input type="checkbox" class="icheck" <?php echo $checkedPermanently; ?> value="<?php echo $admin->id_admin; ?>" name="form-id_admin_permanently" id="form-id_admin_permanently-<?php echo $admin->id_admin; ?>">
												<?php } ?>
											</td>
											<td>
									<?php
										$driver = $admin;
										$note = $driver->note();
										$note_data = [];
										if( $note->id_admin_note ){
											$note_data = $note->exports();
										}
									?>
									<textarea style="width:100px; height:80px;" name="form-note" id_admin="<?php echo $driver->id_admin; ?>" id="admin-note-<?php echo $driver->id_admin; ?>" placeholder="loading"></textarea>
									<div class="admin-note-updating" id="admin-note-updating-<?php echo $driver->id_admin; ?>">
										<i class="icon-spinner icon-spin"></i>
									</div>
									<div style="display:none;" id_admin="<?php echo $driver->id_admin; ?>" class="admin-note-text" id="admin-note-text-<?php echo $driver->id_admin; ?>"><?php echo nl2br( $note_data[ 'text' ] ); ?></div>
											</td>
										</tr>
										<?php }
										}
									?>
									<?php


									if( count( $driversDontWantWorkHere ) ){
										foreach( $driversDontWantWorkHere as $driver ){
											$alreadyShown[] = $driver->id_admin;
										}
									}


									$otherDrivers = $shift->community()->getDriversOfCommunity();
									$hasDrivers = false;
									foreach( $otherDrivers as $admin ){
										if( in_array( $admin->id_admin, $alreadyShown ) ){
											continue;
										}
										$hasDrivers = true;
									}
									if ( $hasDrivers ){
									?>
									<tr>
										<td colspan="7">
											<strong><center>Other drivers</center></strong>
										</td>
									</tr>
									<?php

									foreach( $otherDrivers as $admin ){
										if( in_array( $admin->id_admin, $alreadyShown ) ){
											continue;
										}
										$status = Crunchbutton_Admin_Shift_Status::getByAdminWeekYear( $admin->id_admin, $week, $year );
										$checked = ( Crunchbutton_Admin_Shift_Assign::adminHasShift( $admin->id_admin, $shift->id_community_shift ) ) ? 'checked="checked"' : '';
										$checkedPermanently = ( Crunchbutton_Admin_Shift_Assign_Permanently::adminIsPermanently( $admin->id_admin, $shift->id_community_shift ) ) ? 'checked="checked"' : '';
										$adminShifts = Crunchbutton_Admin_Shift_Assign::shiftsByAdminPeriod( $admin->id_admin, $firstDayOfWeek, $lastDayOfWeek );
										$totalShifts = $adminShifts->count();
										?>
										<tr>
											<td>
												<input type="checkbox" class="icheck" <?php echo $checked; ?> value="<?php echo $admin->id_admin; ?>" name="form-id_admin" id="form-id_admin-<?php echo $admin->id_admin; ?>">
											</td>
											<td title="id_admin: <?php echo $admin->id_admin; ?>">
											<?php echo $admin->name; ?>
												<br/>
												<?php echo $admin->phone(); ?>
												<br/>
												<?php
													$payment_type = $admin->payment_type();
													if( $payment_type->payment_type == Crunchbutton_Admin_Payment_Type::PAYMENT_TYPE_HOURS ){
														echo 'Hourly';
													} else if( $payment_type->payment_type == Crunchbutton_Admin_Payment_Type::PAYMENT_TYPE_HOURS ){
														echo 'Commission';
													} else {
														echo '-';
													}
													echo '<br>';
													if( $payment_type->using_pex == 1 ) {
														echo 'Using pex card';
													} else {
														echo 'Not using pex card';
													}
													$ordersPerHour = $admin->ordersPerHour();
													if( $ordersPerHour ){
														echo "<br>$ordersPerHour orders/hour";
													}
												?>
											</td>
											<td>-</td>
											<td><?php if( intval( $status->shifts ) > 0 ){ echo $status->shifts; } else { echo '-'; } ; ?></td>
											<td><?php echo $totalShifts; ?></td>
											<td>
												<?php if( $shift->isRecurring() ) { ?>
													<input type="checkbox" class="icheck" <?php echo $checkedPermanently; ?> value="<?php echo $admin->id_admin; ?>" name="form-id_admin_permanently" id="form-id_admin_permanently-<?php echo $admin->id_admin; ?>">
												<?php } ?>
											</td>
											<td>
<?php
										$driver = $admin;
										$note = $driver->note();
										$note_data = [];
										if( $note->id_admin_note ){
											$note_data = $note->exports();
										}
									?>
									<textarea style="width:100%;height:80px;" name="form-note" id_admin="<?php echo $driver->id_admin; ?>" id="admin-note-<?php echo $driver->id_admin; ?>" placeholder="loading"></textarea>
												<div <?php if( !$note_data[ 'date' ] ){ echo 'style="display:none;"'; } ?> class="admin-note-updated" id="admin-note-updated-<?php echo $driver->id_admin; ?>">
													Updated at <span id="note-updated-<?php echo $driver->id_admin; ?>"><?php echo $note_data[ 'date' ]; ?></span>
													<br/>
													by <span id="note-added_by-<?php echo $driver->id_admin; ?>"><?php echo $note_data[ 'added_by' ]; ?></span>
												</div>
									<div class="admin-note-updating" id="admin-note-updating-<?php echo $driver->id_admin; ?>">
										<i class="icon-spinner icon-spin"></i>
									</div>
									<div style="display:none;" id_admin="<?php echo $driver->id_admin; ?>" class="admin-note-text" id="admin-note-text-<?php echo $driver->id_admin; ?>"><?php echo nl2br( $note_data[ 'text' ] ); ?></div>
											</td>
										</tr>
										<?php
										}
									}
									?>
									<?php if( count( $driversDontWantWorkHere ) > 0 ) { ?>
										<tr>
											<td colspan="7">
												<strong><center>Drivers that don't want to work at this shift</center></strong>
											</td>
										</tr>
										<?php
										foreach( $driversDontWantWorkHere as $admin ){
											$status = Crunchbutton_Admin_Shift_Status::getByAdminWeekYear( $admin->id_admin, $week, $year );
											$checked = ( Crunchbutton_Admin_Shift_Assign::adminHasShift( $admin->id_admin, $shift->id_community_shift ) ) ? 'checked="checked"' : '';
											$checkedPermanently = ( Crunchbutton_Admin_Shift_Assign_Permanently::adminIsPermanently( $admin->id_admin, $shift->id_community_shift ) ) ? 'checked="checked"' : '';
											$adminShifts = Crunchbutton_Admin_Shift_Assign::shiftsByAdminPeriod( $admin->id_admin, $firstDayOfWeek, $lastDayOfWeek );
											$totalShifts = $adminShifts->count();
											?>
											<tr>
												<td>
													<input type="checkbox" class="icheck" <?php echo $checked; ?> value="<?php echo $admin->id_admin; ?>" name="form-id_admin" id="form-id_admin-<?php echo $admin->id_admin; ?>">
												</td>
												<td title="id_admin: <?php echo $admin->id_admin; ?>">
												<?php echo $admin->name; ?>
												<br/>
												<?php echo $admin->phone(); ?>
												<br/>
												<?php
													$payment_type = $admin->payment_type();
													if( $payment_type->payment_type == Crunchbutton_Admin_Payment_Type::PAYMENT_TYPE_HOURS ){
														echo 'Hourly';
													} else if( $payment_type->payment_type == Crunchbutton_Admin_Payment_Type::PAYMENT_TYPE_HOURS ){
														echo 'Commission';
													} else {
														echo '-';
													}
													echo '<br>';
													if( $payment_type->using_pex == 1 ) {
														echo 'Using pex card';
													} else {
														echo 'Not using pex card';
													}
													$ordersPerHour = $admin->ordersPerHour();
													if( $ordersPerHour ){
														echo "<br>$ordersPerHour orders/hour";
													}
												?>
												</td>
												<td>-</td>
												<td><?php if( intval( $status->shifts ) > 0 ){ echo $status->shifts; } else { echo '-'; } ; ?></td>
												<td><?php echo $totalShifts; ?></td>
												<td>
													<?php if( $shift->isRecurring() ) { ?>
														<input type="checkbox" class="icheck" <?php echo $checkedPermanently; ?> value="<?php echo $admin->id_admin; ?>" name="form-id_admin_permanently" id="form-id_admin_permanently-<?php echo $admin->id_admin; ?>">
													<?php } ?>
											</td>
											<td>
									<?php
										$driver = $admin;
										$note = $driver->note();
										$note_data = [];
										if( $note->id_admin_note ){
											$note_data = $note->exports();
										}
									?>
									<textarea style="width:100px;height:80px;" name="form-note" id_admin="<?php echo $driver->id_admin; ?>" id="admin-note-<?php echo $driver->id_admin; ?>" placeholder="loading"></textarea>
									<div class="admin-note-updating" id="admin-note-updating-<?php echo $driver->id_admin; ?>">
										<i class="icon-spinner icon-spin"></i>
									</div>
									<div style="display:none;" id_admin="<?php echo $driver->id_admin; ?>" class="admin-note-text" id="admin-note-text-<?php echo $driver->id_admin; ?>"><?php echo nl2br( $note_data[ 'text' ] ); ?></div>
											</td>
											</tr>
										<?php
										}
									 } ?>
									</tbody>
								</table>
							</div>

							<div class="row-fluid">
								<div class="row-fluid">
									<div class="span12">
										<br/>
									</div>
								</div>
								<div class="row-fluid">
									<div class="span6">
										<button type="button" onclick="shift.summary.assign.save();" class="btn btn-green"><i class="icon-save"></i> Save </button>
									</div>
								</div>
							</div>
						</form>
		</div>
	</div>
</div>
<script type="text/javascript" src="/assets/js/admin.shift.js"></script>
<script type="text/javascript">
$( document ).ready( function(){
	shift.summary.assign.init();
	shift.status.init();
} );
</script>