<?php if ( !$this->orders->count() ) { ?>
	No results found
<?php } else { ?>

	<table class="list-orders table table-normal">
		<thead>
			<tr>
				<td>Order</td>
				<td>Driver</td>
				<td>Notifications</td>
				<td>Actions</td>
			</tr>
		</thead>

		<?php
			$tz_default = c::view()->tz_default;
			foreach( $this->orders as $order ) {
				$timeToDelivery = '';
				$date = $order->date();
				$date_admin_tz = $order->dateAtTz( $this->admin_tz );
				?>
			<tr>
				<td style="vertical-align:top;">
					<a href="/vieworder/<?php echo $order->uuid; ?>" target="blank">Order #<?php echo $order->id_order; ?></a>
					<br/>
					<a href="https://cockpit.la/<?php echo $order->id_order; ?>" target="blank">https://cockpit.la/<?php echo $order->id_order; ?></a>
					<br />
					<?php echo $order->restaurant()->name; ?>
					<br />
					<?php echo $order->restaurant()->communityNames(); ?>
					<br />
					<div class="admin-tz" <?php if( $tz_default == 'restaurant-tz' ){ echo 'style="display:none;"'; } ?>>
						<?php echo $date_admin_tz->format('M jS Y')?> <?php echo $date_admin_tz->format('g:i:s A T')?>
					</div>
					<div class="restaurant-tz" <?php if( $tz_default == 'admin-tz' ){ echo 'style="display:none;"'; } ?>>
						<?php echo $date->format('M jS Y')?> <?php echo $date->format('g:i:s A T')?>
					</div>

				</td>
				<td style="vertical-align:top;">
					<?php
						if( $order->driver()->name ){
							?>
							<a href="/permissions/users/<?php echo $order->driver()->id_admin ?>" target="_blank">
								<strong><?php echo $order->driver()->name; ?></strong>
							</a>
							<?php
							if( $order->driver()->phone ){
								echo '<br/><i class="icon icon-phone"></i> ' . $order->driver()->phone;
							}
							if( $order->driver()->txt ){
								echo '<br/><i class="icon icon-mobile-phone"></i> ' . $order->driver()->txt;
							}
							if( $order->driver()->email ){
								echo '<br/><i class="icon icon-envelope"></i> ' . $order->driver()->email;
							}
							$activeNotifications = Crunchbutton_Admin_Notification::q( "SELECT * FROM admin_notification WHERE id_admin = {$order->driver()->id_admin} AND active = true" );
							if ( count( $activeNotifications ) > 0 ) {
								?>
								<hr/>
								<strong>Active notifications:</strong>
								<?php

								foreach ( $activeNotifications as $activeNotification ) {
									?>
									<br/><strong><?php echo $activeNotification->type ?>: </strong>
									<?php echo $activeNotification->value ?>
									<?php
								}
							}

						} else {
							echo '<i style="color:#CCC;font-size:11px;">None</i>';
						}
					?>
					<?php

					?>
				</td>
				<td style="vertical-align:top;">
				<?php
					$hasNotification = false;
					$notifications = Crunchbutton_Admin_Notification_Log::byOrder( $order->id_order );
					foreach( $notifications as $notification ){
						$hasNotification = true;
						$_date = $notification->date();
						$_date_admin_tz = $notification->dateAtTz( $this->admin_tz );
						echo $notification->description
						?>
						: <span class="admin-tz" <?php if( $tz_default == 'restaurant-tz' ){ echo 'style="display:none;"'; } ?>>
							<?php echo $_date_admin_tz->format( 'g:i:s A T' ); ?>
						</span>

						: <span class="restaurant-tz" <?php if( $tz_default == 'admin-tz' ){ echo 'style="display:none;"'; } ?>>
							<?php echo $_date->format( 'g:i:s A T' ); ?>
						</span>
						<br/>
						<?php
					}
					if( !$hasNotification ){
						echo '<i style="color:#CCC;font-size:11px;">-</i>';
					}
				?>
				</td>
				<td style="vertical-align:top;">
				<?php
				$editActions = true;
				$_log = '';
				$logs = $order->actions();
				foreach( $logs as $log ){
					$_date = $log->date();
					$_date_admin_tz = $log->dateAtTz( $this->admin_tz );
					switch ( $log->type ) {
						case Crunchbutton_Order_Action::DELIVERY_REJECTED:
							$editActions = false;
							$color = '#fb3927';
							break;
						case Crunchbutton_Order_Action::DELIVERY_ACCEPTED:
						case Crunchbutton_Order_Action::DELIVERY_PICKEDUP:
						case Crunchbutton_Order_Action::DELIVERY_DELIVERED:
							$color = '#009c48';
							$editActions = false;
							break;
						default:
							$color = '#000';
							break;
					}
					if( $log->type == Crunchbutton_Order_Action::DELIVERY_DELIVERED ){
					 	$timeToDelivery = $date->diff( $_date );
					 	$timeToDelivery = ( $timeToDelivery->days * 24 * 60 ) + ( $timeToDelivery->h * 60 ) + ( $timeToDelivery->i );
						$timeToDelivery .= ' minutes';
					}
						$_log .= '<span style="color:' . $color . '">' . $log->type . ' by ' . $log->name . '</span> : ';

						$_log .= '<span class="admin-tz"' . ( ( $tz_default == 'restaurant-tz' ) ? 'style="display:none;"' : '' ) . '>';
						$_log .= $_date_admin_tz->format( 'g:i:s A T' );
						$_log .= '</span>';

						$_log .= '<span class="restaurant-tz"' . ( ( $tz_default == 'admin-tz' ) ? 'style="display:none;"' : '' ) . '>';
						$_log .= $_date->format( 'g:i:s A T' );
						$_log .= '</span><br/>';
				}

				if( $editActions ){
					?>
						<div>
							<button onclick="deliveries.action( <?php echo $order->id_order ?> );" class="btn btn-blue"> Assign driver</button>
						</div>
					<?php
				}

				if( $timeToDelivery != '' ){
					echo '<strong style="color:#666;font-size:12px;">Delivered in ' . $timeToDelivery . '</strong><br/>';
				}
				if( $_log ){
					echo $_log;
				} else {
					echo '<i style="color:#CCC;font-size:11px;">-</i>';
				}
				?>
				</td>
			</tr>
		<?php } ?>
	</table>
<?php } ?>