<style>
.comments .ui-state-default {
	border: 0;
}
.list-orders.table-normal tbody td {
	vertical-align: top;
}
.list-orders a {
	text-decoration: underline;
}
</style>
<?php
$show_details = ( $this->orders->count() < 800 );
$cache = [];
?>
<? if (!$this->orders->count()) : ?>
	<div class="padded">
		No results found
	</div>
<? else : ?>

	<table class="list-orders table table-normal">
		<thead>
			<tr>
				<td style="width:120px;">Info</td>
				<td style="width:95px;">Date</td>
				<td style="width:120px;">Restaurant</td>
				<td>Order</td>
				<td>Price</td>
				<td>Pay&nbsp;/&nbsp;Type</td>
				<td></td>
			</tr>
		</thead>

<?php
$permissionRefund = c::admin()->permission()->check(['global','orders-all','orders-refund']);
$permissionNotification = c::admin()->permission()->check(['global','orders-all','orders-notification']);
$permissionGiftCard = c::admin()->permission()->check(['global','orders-all','gift-card-all','gift-card-crud']);
$permissionSupportCreate = c::admin()->permission()->check(['global','support-all','support-crud']);
$permissionSupportView = c::admin()->permission()->check(['global','support-all','support-crud', 'support-view']);
?>

		<? foreach ($this->orders as $order) :  /* @var $order Crunchbutton_Order */?>
			<? $date = $order->date(); ?>
			<tr class="list-order-item <?php if( $order->restaurant()->deliveryItSelf() ){ echo 'deliverThemSelves"'; } ?>" >
				<td>
					<a href="/vieworder/<?=$order->uuid?>">#<?=$order->id?></a>
					<br/>
					<a title="Drivers view" target="blank" href="/vieworder/<?=$order->uuid?>"><img style="opacity:.7" src="/assets/images/mprinter/ico-driver.png"></a>&nbsp;&nbsp;&nbsp;
					<a target="blank" title="Restaurant view" href="/vieworder/<?=$order->uuid?>/restaurant"><img style="opacity:.7" src="/assets/images/mprinter/ico-restaurant.png"></a>&nbsp;&nbsp;&nbsp;
					<a target="blank" title="Customer view" href="/vieworder/<?=$order->uuid?>/customer"><img style="opacity:.7" src="/assets/images/mprinter/ico-customer.png"></a>
					<br/>
					<br/>
					<?=$order->env?>
					<br/>
					<br/>

						<?php if( $permissionRefund ) { ?>
							<? if ($order->refunded) : ?>
								REFUNDED
							<? else : ?>
								<a href="javascript:;" class="refund was-refunded-<?=$order->uuid?>" data-charged="<?php echo $order->charged(); ?>" data-gift="<?php if( $order->chargedByCredit() > 0 ){ echo $order->chargedByCredit(); } else { echo '0'; } ?>" data-uuid="<?=$order->uuid?>">REFUND
								</a>

								<br>
								<br>
								<a href="javascript:;" class="check-refunded check-was-refunded-<?=$order->uuid?>" data-uuid="<?=$order->uuid?>">Check if it was refunded</a>

							<? endif ; ?>
						<?php } ?>

							<?php
							$credit = $order->credit;
							if( $credit > 0 ){
								echo '<div style="color:red;margin-top:8px;">Received <strong style="font-weight:bold;">$' . number_format( $credit, 2 ) . '</strong> credit.</div>';
							}
							?>
							<?php
							$giftcard = $order->gift_card;
							if( $giftcard > 0 ){
								echo '<br/><br/><i class="icon-gift"></i> Received <b>$' . number_format( $giftcard, 2 ) . '</b> gift card.';
							}
							?>
							<br />
							<br />

								<?php if( $permissionRefund ) { ?>
									<div style="<?php if( !$order->refunded ) { echo 'display:none;';} ?>" class="pointer refunded-<?php echo $order->uuid; ?> pay_if_refunded" data-value="<?php if( $order->pay_if_refunded ){ echo 1; } else { echo 0; } ?>" data-uuid="<?=$order->uuid?>">
										<span><?php if( $order->pay_if_refunded ) { echo '<i class="icon-check"></i>'; } else { echo '<i class="icon-check-empty"></i>'; } ?></span> Pay restaurant despite refund (e.g. this refund was our fault)
									</div>

<?php if( $order->delivery_service ) { ?>

							<div style="<?php if( !$order->refunded ) { echo 'display:none;';} ?>" class="pointer do_not_reimburse_driver-<?php echo $order->uuid; ?> refunded-<?php echo $order->uuid; ?> do_not_reimburse_driver" data-value="<?php if( $order->do_not_reimburse_driver ){ echo 1; } else { echo 0; } ?>" data-uuid="<?=$order->uuid?>">
								<span><?php if( $order->do_not_reimburse_driver ) { echo '<i class="icon-check"></i>'; } else { echo '<i class="icon-check-empty"></i>'; } ?></span>
								Do Not Pay Driver Anything
							</div>
<?php } ?>

						<?php } ?>

					<br/>

					<?
						if( $order->id_support ) {

							if( $permissionSupportCreate ){
								echo '<i class="icon-comments"></i> <a href="/support/' . $order->id_support . '">View support ticket</a>';
							}

						}
						else {
							if( $permissionSupportView ){
								echo '<i class="icon-comments"></i> <a href="javascript:create_support_from_order(' . $order->id . ');">Create support ticket</a>';
							}
						}
					?>
					<br/>
					<?php if( $order->txn && $order->env == 'live' ){ ?>
						<br/>
						<a target="_blank"  href="https://dashboard.balancedpayments.com/#/marketplaces/MP2BldrjHz0NLH3st95JbeR0/debits/<?php echo $order->txn ?>">Balanced transaction</a>
					<?php } ?>
				</td>
				<td nowrap="">
					<?php $date->setTimeZone( new DateTimeZone( c::config()->timezone ) ); ?>
					<?=$date->format('M jS Y')?><br /><br /><?=$date->format('g:i:s A')?> <strong>(<?php echo $date->format( 'T' ) ?>)</strong>
					<br/>
					<br/>
					<br/>

			<?php
			if( $show_details ){
				$_log = '';
				$timeToDelivery = '';
				$logs = $order->actions();
				foreach( $logs as $log ){
					$_date = $log->date();
					switch ( $log->type ) {
						case Crunchbutton_Order_Action::DELIVERY_REJECTED:
							$color = '#fb3927';
							break;
						case Crunchbutton_Order_Action::DELIVERY_ACCEPTED:
						case Crunchbutton_Order_Action::DELIVERY_PICKEDUP:
						case Crunchbutton_Order_Action::DELIVERY_DELIVERED:
							$color = '#009c48';
							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 . ' [' . $log->phone . ']</span> : ';

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

				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>
				<td nowrap="nowrap">
				<?php
				if( !$cache[ 'restaurant_' . $order->id_restaurant ] ){
					$cache[ 'restaurant_' . $order->id_restaurant ] = $order->restaurant();
					$cache[ 'restaurant_communities' . $order->id_restaurant ] = $order->restaurant()->communityNames();
					$cache[ 'restaurant_phone_notification' . $order->id_restaurant ] = $order->restaurant()->hasPhoneNotification();
				}
				$restaurant = $cache[ 'restaurant_' . $order->id_restaurant ];
				?>
					<a href="/restaurants/<?=$restaurant->id_restaurant?>">
						<?=$restaurant->name?>
					</a>
					<br/>
						<?php echo $restaurant->phone() ?>
					<br/>
					<?=$cache[ 'restaurant_communities' . $order->id_restaurant ];?>
					<hr/>
					<?php if( $restaurant->delivery_service && ( $restaurant->formal_relationship || $restaurant->order_notifications_sent ) ) { ?>
						<strong>Notification Sent to Restaurant</strong><br/><br/>
					<?php } ?>
					<?php if( $restaurant->confirmation || $cache[ 'restaurant_phone_notification' . $order->id_restaurant ] ){
						if( $order->confirmed ) {
							echo '<i class="icon-thumbs-up"></i> Confirmed!';
						} else {
							echo '<i class="icon-thumbs-down"></i> NOT confirmed.';
						}
						echo '<br/><br/>';
					} ?>
					<?php if( $permissionNotification ) { ?>
						<i class="icon-bullhorn"></i> <a href="javascript:;" class="resend_notification" data-confirmed="<?php echo $order->confirmed; ?>" data-uuid="<?=$order->uuid?>">Resend <strong>restaurant</strong> <br/>notification(s)</a> <br/><br/>

						<?php if( $order->delivery_service ) { ?>

							<i class="icon-bullhorn"></i> <a href="javascript:;" class="resend_notification_drivers" data-confirmed="<?php echo $order->confirmed; ?>" data-uuid="<?=$order->uuid?>">Resend <strong>driver</strong>  <br/>notification(s)</a>
						<?php } ?>
					<?php } ?>
					<hr/>
					<span title="Timezone">
						<?php $date->setTimeZone( new DateTimeZone( $restaurant->timezone ) ); ?>
						<?=$date->format('M jS Y')?><br /><?=$date->format('g:i:s A')?> <strong>(<?php echo $date->format( 'T' ) ?>)</strong>
						<br/>
						<i class="icon-globe"></i> <?=$restaurant->timezone?>
					</span>
				</td>
				<td style="width: 230px; white-space: normal;">
					<?php if( $show_details ) { ?>
						<ul>
						<? foreach ($order->dishes() as $dish) : ?>
							<li>
								<?=$dish->dish()->name?>
								<? if ($dish->options()->count()) : ?>
									<ul>
									<? foreach ($dish->options() as $option) : ?>
										<li><?=$option->option()->name?></li>
									<? endforeach ; ?>
									<? if( $dish->id_order_dish && $dish->id_dish ){
											foreach ( $dish->optionsDefaultNotChoosen() as $option ) {
											?><li><i style="font-style:italic;">No <?=$option->option()->name?></i></li><?
										}
									}?>
									</ul>
								<? endif ; ?>
							</li>
						<? endforeach ; ?>
						</ul>
					<?php	} ?>
				</td>
				<td style="padding:0;">

					<table style="margin:0;">
						<tr>
							<td style="border-top:0;">Subtotal</td>
							<td style="border-top:0;">$
							<?php
							if( $order->price_plus_delivery_markup && $order->price_plus_delivery_markup > 0 ) {
								echo number_format( $order->price_plus_delivery_markup, 2 );
							} else {
								echo number_format( $order->price, 2 );
							}
							?>
							</td>
						</tr>
						<?php if( $order->delivery_service_markup_value > 0 ) { ?>
						<tr>
							<td nowrap="nowrap">Delivery service percent <br/>(included in subtotal) <br/>
								(+<?=$order->delivery_service_markup ? $order->delivery_service_markup : '0'?>% of $
								<?php
								if( $order->price_plus_delivery_markup ) {
									echo number_format( $order->price, 2 );
								} else {
									number_format( $order->price - $order->delivery_service_markup_value, 2 );
								} ?>)
							</td>
							<td>
								$<?=number_format( $order->delivery_service_markup_value, 2 )?>
								<br/>
								(+<?=$order->delivery_service_markup ? $order->delivery_service_markup : '0'?>%)
							</td>
						</tr>
						<?php } ?>
						<tr>
							<td>Service Fee</td>
							<td>$<?=$order->serviceFee()?> (%<?=$order->service_fee ? $order->service_fee : '0'?>) </td>
						</tr>
						<tr>
							<td nowrap="">Delivery Fee</td>
							<td>$<?=$order->deliveryFee()?></td>
						</tr>
						<tr>
							<td nowrap="">
							Tax <br/>
								<?php
								if( $order->tax ){
									if( $order->price_plus_delivery_markup && $order->delivery_service_markup_value ) {
										if( $order->delivery_service ){
											echo $order->tax . '% of ' . number_format( ( $order->price ), 2 ) . '<br/>(subtotal without markup)';
										} else {
											echo $order->tax . '% of ' . number_format( ( $order->price + $order->deliveryFee() ), 2 ) . '<br/>(subtotal without markup)';
										}
									} else {
										echo $order->tax . '% of ' . number_format( $order->price, 2 ) ;
									}
								}
								?>
							</td>
							<td>$<?=$order->tax()?> (<?=$order->tax ? $order->tax : '0'?>%)</td>
						</tr>
						<tr>
							<td>Tip</td>
							<td>$<?=$order->tip()?> <?php if( $order->tip_type != Crunchbutton_Order::TIP_NUMBER ) { echo '(' . $order->tip . '%)'; } ?></td>
						</tr>
						<tr>
							<td>Total</td>
							<td>$
							<?php
							if( $order->price_plus_delivery_markup && $order->final_price_plus_delivery_markup > 0 ) {
								echo number_format( $order->final_price_plus_delivery_markup, 2 );
							} else {
								echo number_format( $order->final_price, 2 );
							}
							?></td>
						</tr>
						<?php if( $order->chargedByCredit() > 0 ) { ?>
						<tr style="background:#F5F5F5;">
							<td>Gift card</td>
							<td>$<?=$order->chargedByCredit();?></td>
						</tr>
						<tr style="background:#F5F5F5;">
							<td>Charged</td>
							<td>$<?=$order->charged();?></td>
						</tr>
						<?php } ?>
					</table>
				</td>
				<td>
					<?=$order->pay_type?>
					<br />
					<br />
					<?=$order->delivery_type?>
				</td>
				<td style="padding:0;">
					<table style="margin:0;border-top:0;width:100%;">
						<tr title="Name">
							<td style="border-top:0;"><i class="icon-user"></i> <?=$order->name?> </td>
						</tr>
						<tr title="Phone">
							<td><i class="icon-phone"> </i><?=$order->phone();?> </td>
						</tr>
						<?php if ($order->delivery_type == 'delivery') { ?>
						<tr title="Address">
							<td><i class="icon-home"></i> <?=$order->address?> </td>
						</tr>
						<?php } ?>
						<? if ($order->notes) :?>
							<tr>
								<td>
									<span>
										<i class="icon-file-alt"></i> <?php echo $order->notes;	?>
									</span>
								</td>
							</tr>
						<? endif ?>
						<?php if( $permissionGiftCard ){ ?>
						<tr>
							<td>
								<br/>
								<a href="/giftcards/new?id_user=<?php echo $order->user()->id_user; ?>&id_restaurant=<?php echo $restaurant->id_restaurant; ?>&id_order_reference=<?php echo $order->id_order; ?>"><i class="icon-gift"></i> Add gift card</a>
							</td>
						</tr>
						<?php } ?>
						<?php if( $order->id_agent ) { ?>
							<tr>
								<td><i class="icon-desktop"></i> <?php echo $order->os . ' ' . $order->browser ?></td>
							</tr>
								<?php
							} ?>
					</table>
				</td>
			</tr>
		<? endforeach ; ?>
	</table>
<? endif ; ?>