<?php
$delivery_service_markup = ( $order->delivery_service_markup ) ? $order->delivery_service_markup : 0;

$isRestaurant = !$user;
if( $isDriverCockpit ){
	$isRestaurant = false;
	if( $delivery_service_markup == 0 ){
		$isDriverCockpit = false;
	}
}

// force version at cockpit
if( $version == 'restaurant' ){
	$isRestaurant = true;
	$isDriverCockpit = false;
}

if( $version == 'customer' ){
	$user = true;
	$isRestaurant = false;
	$isDriverCockpit = false;
}

?>
<table width="100%" celpadding="0" cellspacing="0" class="detail">
	<thead style="font-weight:bold">
		<td class="topitem">Qty</td>
		<td class="topitem">Item</td>
		<?php if( $isDriverCockpit ) { ?>
		<td class="topitem" nowrap="nowrap">Customer Price</td>
		<td class="topitem" style="width:80px;font-size:12px;color:#666;">Store Price</td>
		<?php } else { ?>
			<td class="topitem">Price</td>
		<?php } ?>
	</thead>
	<?php

	$dishes = Crunchbutton_Order_Data::dishes( $order->id_order );
	if( $dishes ){

		echo $this->display('order/details.dishes.data',['set' => ['dishes' => $dishes, 'isRestaurant' => $isRestaurant, 'isDriverCockpit' => $isDriverCockpit ]]);
	} else {
		// LEGACY: old orders doesn't have data
		echo $this->display('order/details.dishes.legacy',['set' => ['order' => $order, 'isRestaurant' => $isRestaurant, 'isDriverCockpit' => $isDriverCockpit ]]);
	}
	?>
	<tr class="class">
		<td></td>
		<td style="text-align:right">Subtotal</td>
		<td>
		$<?php
		if( $isRestaurant ){
			// legacy
			if( intval( $delivery_service_markup ) > 0 && !$order->price_plus_delivery_markup ){
				echo number_format( $order->price - $order->delivery_service_markup_value, 2);
			} else {
				echo number_format( $order->price, 2 );
			}
		} else {
			if( $order->price_plus_delivery_markup ){
				echo number_format( $order->price_plus_delivery_markup, 2 );
			} else {
				echo number_format( $order->price, 2 );
			}
		}
		?>
		</td>
		<?php if( $isDriverCockpit ) { ?>
		<td style="font-size:12px;color:#666;">
		$<?php
		// legacy
		if( intval( $delivery_service_markup ) > 0 && !$order->price_plus_delivery_markup ){
			echo number_format( $order->price - $order->delivery_service_markup_value, 2);
		} else {
			echo number_format( $order->price, 2 ) ;
		}
		?>
		</td>
		<?php } ?>
	</tr>
	<?php
	// change fax for 3rd party restaurants #2962
	if( $viewer != 'driver' ){
		$showTipAndDeliveryFee = true;
		$show_credit_card_tips = true;
	} else {
		$showTipAndDeliveryFee = !$order->restaurant()->delivery_service;
	}

	if( $showTipAndDeliveryFee && $show_credit_card_tips && $viewer != 'driver' ){
		if ( $order->pay_type == 'card' && $order->tip ) { ?>
			<tr>
				<td></td>
				<td style="text-align:right">Tip <?php
				 if( $order->tip_type != Crunchbutton_Order::TIP_NUMBER ) { echo $order->tip . '%'; }
				 ?></td>
				<td>$<?php echo $order->tip()?></td>
				<?php if( $isDriverCockpit ) { ?>
				<td style="font-size:12px;color:#666;">
					$0.00
				</td>
				<?php } ?>
			</tr>
		<?php }
	}

	if( $viewer != 'driver' || $show_delivery_fees ){
		 if ( $order->restaurant()->delivery_fee || $order->serviceFee() > 0 ) { ?>
			<tr>
				<td></td>
				<td style="text-align:right">Delivery Fee</td>
				<td>
				$<?php
					echo number_format( $order->deliveryFee() + $order->serviceFee(), 2 );
				?></td>
				<?php if( $isDriverCockpit ) {  ?>
				<td style="font-size:12px;color:#666;">
					&mdash;
				</td>
				<?php }  ?>
			</tr>
		<?php  }

	}

	?>

	<?php /* #5597 ?>
	<?php if ( $order->restaurant()->fee_customer ) { ?>
		<tr>
			<td></td>
			<td style="text-align:right">Service Fee</td>
			<td>$<?php echo $order->serviceFee() ;?></td>
			<?php if( $isDriverCockpit ) { ?>
			<td style="font-size:12px;color:#666;">
				$<?php echo $order->serviceFee(); ?>
			</td>
			<?php } ?>
		</tr>
	<?php } ?>
	<?php */ ?>

	<tr>
		<td></td>
		<td style="text-align:right">
			Tax<?php if( !$isRestaurant && $order->restaurant()->delivery_service ) {  echo '/Service'; }?>
		</td>
		<td>
		$<?php
		$tax = $order->tax();
		if( $isRestaurant ){
			echo number_format( $tax, 2 );
		} else {
			echo number_format( ( $tax + $order->cb_service_fee ), 2 );
		}
		?>
		</td>
		<?php if( $isDriverCockpit ) { ?>
		<td style="font-size:12px;color:#666;">
			$<?php echo number_format( $tax, 2 ); ?>
		</td>
		<?php } ?>
	</tr>

	<tr style="font-weight:bold;">
		<td></td>
		<td style="text-align:right">Total</td>
		<td>
		$<?php
		if( $isRestaurant ){
			echo $order->restaurantPrice();
		} else {
			if( $order->final_price_plus_delivery_markup ){
				echo number_format( $order->final_price_plus_delivery_markup, 2 );
			} else {
				echo number_format( $order->final_price, 2 );
			}
		}
		?>
		</td>
		<?php if( $isDriverCockpit ) { ?>
		<td style="font-size:14px;color:#666;">
		$<?php
		// legacy
		if( intval( $delivery_service_markup ) > 0 && !$order->final_price_plus_delivery_markup ){
			echo number_format( $order->final_price - $order->delivery_service_markup_value - $order->restaurant()->delivery_fee, 2);
		} else {
			echo number_format( $order->final_price - $order->restaurant()->delivery_fee, 2 );
		}
		?>
		</td>
		<?php } ?>
	</tr>

	<?php /* Just the user and CB will see this info about gift card. See #1006. */ ?>
	<?php if( $user && $order->chargedByCredit() > 0 ) { ?>
	<tr style="font-weight:bold;">
		<td></td>
		<td style="text-align:right">
			<?php
			if( $order->pay_type == 'card' ) { ?>
				<?php if( $order->reward_delivery_free ) { ?>
					Delivery paid by Crunchbutton
				<?php } else { ?>
					Gift Card
				<?php } ?>
			<?php } else { ?>
				Already Paid by Gift Card
			<?php } ?>
		</td>
		<td>
		<?php
			if( $order->pay_type == 'card' ) { ?>
				<?php if( !$order->reward_delivery_free ) { ?>
					$<?php echo number_format($order->chargedByCredit(),2); ?>
				<?php } ?>
			<?php } else { ?>
				$<?php echo number_format($order->chargedByCredit(),2); ?>
			<?php } ?>
		</td>
	</tr>

	<tr style="font-weight:bold;">
		<td></td>
		<td style="text-align:right"><?php if( $order->pay_type == 'card' ) { ?>Total Charged <?php } else { ?> Total Owed <?php } ?></td>
		<td>$<?php echo number_format($order->charged(),2); ?></td>
	</tr>
	<?php } ?>

	<?php if ($order->pay_type == 'card' && $order->tip == 0) { ?>
		<tr>
			<td colspan="3" style="text-align:left">Customer is tipping with cash</td>
		</tr>
	<?php } ?>
</table>