<?php
/**
 * @version     1.4.7
 * @package     sellacious
 *
 * @copyright   Copyright (C) 2012-2017 Bhartiy Web Technologies. All rights reserved.
 * @license     SPL Sellacious Private License; see http://www.sellacious.com/spl.html
 * @author      Izhar Aazmi <info@bhartiy.com> - http://www.bhartiy.com
 */
// no direct access
defined('_JEXEC') or die;

/** @var  SellaciousViewOrders $this */
JHtml::_('script', 'media/com_sellacious/js/plugin/serialize-object/jquery.serialize-object.min.js', false, false);
JHtml::_('script', 'com_sellacious/util.modal.js', true, true);
JHtml::_('script', 'com_sellacious/view.orders.js', true, true);

JHtml::_('stylesheet', 'com_sellacious/component.css', null, true);
JHtml::_('stylesheet', 'com_sellacious/view.orders.css', null, true);
JHtml::_('stylesheet', 'com_sellacious/font-awesome.min.css', null, true);

$listOrder = $this->escape($this->state->get('list.ordering'));
$listDirn  = $this->escape($this->state->get('list.direction'));
$ordering  = ($listOrder == 'a.ordering');
$saveOrder = ($listOrder == 'a.ordering' && strtolower($listDirn) == 'asc');

$c_state    = $this->state->get('list.currency', 'current');
$c_currency = $c_state == 'current' ? '' : ($c_state == 'original' ? null : $this->helper->currency->getGlobal('code_3'));

foreach ($this->items as $i => $order)
{
	$invoice_link = JRoute::_('index.php?option=com_sellacious&view=order&layout=invoice&id=' . (int) $order->id);
	$receipt_link = JRoute::_('index.php?option=com_sellacious&view=order&layout=receipt&id=' . (int) $order->id);
	$txn_link     = JRoute::_('index.php?option=com_sellacious&view=transactions&layout=order&tmpl=component&filter[order_id]=' . (int) $order->id);
	?>
	<tr id="order-<?php echo $i ?>" class="order-row">
		<td class="center hidden-phone" style="width:40px">
			<input type="checkbox" name="cid[]" id="cb<?php echo $i ?>" class="hidden" value="<?php echo $order->id ?>" title=""/>
			<span><?php echo (int) $order->id; ?></span>
		</td>
		<td style="width:100px" class="nowrap">
			<?php $time = JHtml::_('date', $order->created, 'h:i A'); ?>
			<span class="hasTooltip" title="<?php echo $time ?>"><?php
				echo JHtml::_('date', $order->created, 'M d, Y'); ?></span>
		</td>
		<td style="max-width: 120px; text-overflow: ellipsis; overflow: hidden; padding-right: 5px;"><?php
			echo $this->escape($order->customer_name) ?></td>
		<td class="text-left" data-row="#order-items-<?php echo $i ?>">
			<span class="monospace"><?php echo $order->order_number; ?></span><?php

			if (!empty($order->items))
			{
				?>
				<button type="button" class="btn-toggle button-mini pull-right">Expand</button>
				<button type="button" class="btn-toggle button-mini pull-right hidden">Collapse</button>
				<?php
			}
			?>
		</td>
		<td class="nowrap text-center">
			<span><?php echo $order->order_status; ?></span>
		</td>
		<td class="">
			<?php echo $order->payment_method; ?>
		</td>
		<td class="">
			<?php echo $order->shipping_rule; ?><br>
			<?php echo $order->shipping_service; ?>
		</td>
		<td class="amount-cell">
			<?php
			if ($order->coupon_code)
			{
				echo $this->helper->currency->display($order->coupon_amount, $order->currency, $c_currency);

				echo '<br/><small class="red">(' . $this->escape($order->coupon_code) . ')</small>';
			}
			else
			{
				echo 'NA';
			}
			?>
		</td>
		<td class="amount-cell">
			<?php echo $this->helper->currency->display($order->product_taxes + $order->cart_taxes, $order->currency, $c_currency); ?>
		</td>
		<td class="amount-cell">
			<?php echo $this->helper->currency->display($order->product_discounts + $order->cart_discounts, $order->currency, $c_currency); ?>
		</td>
		<td class="amount-cell strong">
			<?php echo $this->helper->currency->display($order->grand_total, $order->currency, $c_currency); ?>
		</td>
		<td class="nowrap center">
			<?php $link = JRoute::_('index.php?option=com_sellacious&view=order&layout=history&tmpl=component&id=' . $order->id); ?>
			<a class="btn btn-info btn-xs btn-modal" href="<?php echo $link ?>"><span data-modal='{"h": "500", "w": "1100"}' >
				<i class="fa fa-clock-o"></i> <?php echo JText::_('History') ?></span></a>

			<a target="_blank" class="btn btn-danger btn-xs" href="<?php echo $invoice_link ?>"><i class="fa fa-file-text"></i> Invoice</a>
			<a target="_blank" class="btn btn-success btn-xs" href="<?php echo $txn_link ?>"><i class="fa fa-table"></i> Transactions</a>
			<a target="_blank" class="btn btn-primary btn-xs" href="<?php echo $receipt_link ?>"><i class="fa fa-file-o"></i> Receipt</a>
		</td>
	</tr>

	<tr id="order-items-<?php echo $i ?>" class="hidden order-items-row">
		<td colspan="12">
			<?php
			if (!empty($order->items))
			{
				?>
				<table class="w100p order-items table table-bordered">
					<thead>
					<tr>
						<th>Title</th>
						<th class="nowrap text-center" style="width:155px">Seller</th>
						<th class="nowrap text-center" style="width: 55px">Quantity</th>
						<th class="nowrap text-center" style="width:200px">Status</th>
						<th class="nowrap text-center" style="width: 90px">List Price</th>
						<th class="nowrap text-center" style="width: 90px">Sales Price</th>
						<th class="nowrap text-center" style="width: 90px">Shipping</th>
						<th class="nowrap text-center" style="width: 90px">Taxes</th>
						<th class="nowrap text-center" style="width: 90px">Discounts</th>
						<th class="nowrap text-center" style="width: 88px">Total</th>
					</tr>
					</thead>
					<tbody>
					<?php
					foreach ($order->items as $oii => $oi)
					{
						$p_code = $this->helper->product->getCode($oi->product_id, $oi->variant_id, $oi->seller_uid);
						$p_url  = JRoute::_('../index.php?option=com_sellacious&view=product&p=' . $p_code);

						$oi->title    = sprintf('%s - %s', $oi->product_title, $oi->variant_title);
						$oi->status   = $this->helper->order->getStatus($oi->order_id, $oi->item_uid);
						$oi->statuses = $this->helper->order->getStatuses('order.' . $oi->product_type, $oi->status->s_id);
						?>
						<tr id="oi-row-<?php echo $i ?>-<?php echo $oii ?>">
							<td data-row="#oi-info-row-<?php echo $i ?>-<?php echo $oii ?>">
								<?php echo $oi->package_items ? JText::_('COM_SELLACIOUS_CART_PACKAGE_ITEM_LABEL') : ''; ?>
								<a target="_blank" href="<?php echo $p_url ?>"><?php echo $this->escape($oi->title) ?></a>

								<?php if ($oi->package_items): ?>
									<hr class="thin-line">
									<ol class="package-items">
										<?php
										foreach ($oi->package_items as $pkg_item):
											$url = JRoute::_('../index.php?option=com_sellacious&view=product&p=' . $pkg_item->code);
											?><li><a class="dark-link-off" href="<?php echo $url ?>"><?php
												echo $pkg_item->product_title ?> <?php echo $pkg_item->variant_title ?>
												(<?php echo $pkg_item->product_sku ?>-<?php echo $pkg_item->variant_sku ?>)</a></li><?php
										endforeach;
										?>
									</ol>
								<?php endif; ?>

							</td>
							<td class="text-center">
								<?php echo $oi->seller_company ? $oi->seller_company : "[$oi->seller_name]" ?>
							</td>
							<td class="text-center">
								<?php echo $oi->quantity ?>
							</td>
							<td class="text-center" style="padding: 1px;">
								<span class="oi-status"><?php echo $oi->status->s_title ? $oi->status->s_title : 'NA'; ?> </span>
								<?php if ($this->helper->access->checkAny(array('status', 'status.own'), 'order.item.edit.')): ?>
									<a href="#" class="txt-color-red btn-oi-status-edit" data-id="<?php $oi->id ?>"><i class="fa fa-edit"></i> </a>
								<?php endif; ?>
							</td>
							<td class="amount-cell text-italic">
								<?php echo $this->helper->currency->display($oi->list_price + $oi->variant_price, $order->currency, $c_currency); ?>
							</td>
							<td class="amount-cell text-italic">
								<?php echo $this->helper->currency->display($oi->basic_price, $order->currency, $c_currency); ?>
							</td>
							<td class="amount-cell">
								<?php echo $this->helper->currency->display($oi->shipping_amount, $order->currency, $c_currency); ?>
							</td>
							<td class="amount-cell text-italic">
								<?php echo $this->helper->currency->display($oi->tax_amount, $order->currency, $c_currency); ?>
							</td>
							<td class="amount-cell text-italic">
								<?php echo $this->helper->currency->display($oi->discount_amount, $order->currency, $c_currency); ?>
							</td>
							<td class="amount-cell">
								<?php echo $this->helper->currency->display($oi->sub_total, $order->currency, $c_currency); ?>
							</td>
						</tr>
						<?php
						if ($this->helper->access->checkAny(array('status', 'status.own'), 'order.item.edit.'))
						{
							?>
							<tr id="oi-info-row-<?php echo $i ?>-<?php echo $oii ?>" class="hidden">
								<td colspan="10">
									<table class="w100p">
										<tbody>
										<tr>
											<td class="status-form-container bg-color-white v-top hidden">
												<?php echo $this->loadTemplate('item_statusform', $oi); ?>
											</td>
											<td class="status-log-container bg-color-white v-top"></td>
										</tr>
										</tbody>
									</table>
								</td>
							</tr>
							<?php
						}
					}
					?>
					</tbody>
					<tfoot>
					<tr>
						<td class="text-right" colspan="6">Cart Taxes:</td>
						<td class="text-center">-</td>
						<td class="amount-cell"><?php echo $this->helper->currency->display($order->cart_taxes, $order->currency, $c_currency); ?></td>
						<td class="text-center">-</td>
						<td class="text-center">-</td>
					</tr>
					<tr>
						<td class="text-right" colspan="6">Cart Discounts:</td>
						<td class="text-center">-</td>
						<td class="text-center">-</td>
						<td class="amount-cell"><?php echo $this->helper->currency->display($order->cart_discounts, $order->currency, $c_currency); ?></td>
						<td class="text-center">-</td>
					</tr>
					</tfoot>
				</table>
				<?php
			}
			?>
		</td>
	</tr>
	<?php
}
?>
<div class="item-status-detail"><?php
	// Fixme: Add note here for what this line does! Remove suppression!
	echo JLayoutHelper::render('com_sellacious.orders.status.form', @$oi); ?></div>
<input type="hidden" id="formToken" name="<?php echo JSession::getFormToken(); ?>" value="1"/>
