<?php
/**
 * @package     CMGiftCard
 * @subpackage  com_cmgiftcard
 * @copyright   Copyright (C) 2016-2024 CMExension
 * @license     GNU General Public License version 2 or later
 */

defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Layout\LayoutHelper;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Session\Session;

HTMLHelper::_('bootstrap.tooltip');
HTMLHelper::_('behavior.multiselect');
HTMLHelper::_('jquery.framework');

HTMLHelper::addIncludePath(JPATH_COMPONENT . '/helpers/html');

$user		= Factory::getUser();
$userId		= $user->get('id');
$listOrder	= $this->escape($this->state->get('list.ordering'));
$listDirn	= $this->escape($this->state->get('list.direction'));
$canEdit	= $user->authorise('core.edit', 'com_cmgiftcard');
$countries	= CMGiftCardHelper::getCountries();
$token		= Session::getFormToken();

$js = <<<JS
var token = '{$token}';
JS;

$doc = Factory::getDocument();
$doc->addScriptDeclaration($js);
$doc->addStyleSheet('components/com_cmgiftcard/assets/css/cmgiftcard.css');
$doc->addScript('components/com_cmgiftcard/assets/js/cmgiftcard.js');
?>

<form action="<?php echo Route::_('index.php?option=com_cmgiftcard&view=orders'); ?>" method="post" name="adminForm" id="adminForm">
	<?php
	echo LayoutHelper::render('joomla.searchtools.default', array('view' => $this));
	?>
	<?php if (empty($this->items)) : ?>
		<div class="alert alert-info">
			<?php echo Text::_('JGLOBAL_NO_MATCHING_RESULTS'); ?>
		</div>
	<?php else : ?>
		<table class="table table-striped" id="orderList">
			<thead>
				<tr>
					<th width="1%" class="center">
						<?php echo HTMLHelper::_('grid.checkall'); ?>
					</th>
					<th>
						<?php echo HTMLHelper::_('searchtools.sort', 'COM_CMGIFTCARD_ORDER_FIELD_ORDER_NUMBER_LABEL', 'a.order_number', $listDirn, $listOrder); ?>
					</th>
					<th class="hidden-phone">
						<?php echo Text::_('COM_CMGIFTCARD_ORDER_FIELD_USER_ID_LABEL'); ?>
					</th>
					<th class="hidden-phone">
						<?php echo Text::_('COM_CMGIFTCARD_FIELDSET_BILLING_INFO'); ?>
					</th>
					<th>
						<?php echo Text::_('COM_CMGIFTCARD_ORDER_CARDS'); ?>
					</th>
					<th>
						<?php echo Text::_('COM_CMGIFTCARD_AMOUNT'); ?>
					</th>
					<th>
						<?php echo Text::_('JSTATUS'); ?>
					</th>
					<th>
						<?php echo HTMLHelper::_('searchtools.sort', 'JGLOBAL_FIELD_CREATED_LABEL', 'a.created', $listDirn, $listOrder); ?>
					</th>
					<th width="1%" class="nowrap hidden-phone">
						<?php echo HTMLHelper::_('searchtools.sort', 'JGRID_HEADING_ID', 'a.id', $listDirn, $listOrder); ?>
					</th>
				</tr>
			</thead>
			<tfoot>
				<tr>
					<td colspan="9">
						<?php echo $this->pagination->getListFooter(); ?>
					</td>
				</tr>
			</tfoot>
			<tbody>
				<?php foreach ($this->items as $i => $item) :
					$canCheckin = $user->authorise('core.manage', 'com_checkin') || $item->checked_out == $userId || $item->checked_out == 0;

					$billingInfo = array();
					$billingInfo[] = $item->first_name . ' ' . $item->last_name;

					if ($item->email != '')
						$billingInfo[] = $item->email;

					if ($item->address_1 != '')
						$billingInfo[] = $item->address_1;

					if ($this->params->get('street_address_2') && $item->address_2 != '')
						$billingInfo[] = $item->address_2;

					if ($this->params->get('street_address_3') && $item->address_3 != '')
						$billingInfo[] = $item->address_3;

					if ($this->params->get('postal_box') && $item->postal_box != '')
						$billingInfo[] = $item->postal_box;

					if ($item->city != '')
						$billingInfo[] = $item->city;

					if ($item->state != '')
						$billingInfo[] = $item->state;

					if ($this->params->get('postal_code') && $item->postal_code != '')
						$billingInfo[] = $item->postal_code;

					if ($this->params->get('country') && $item->country != '' && isset($countries[$item->country]))
						$billingInfo[] = $countries[$item->country];
					?>
					<tr class="row<?php echo $i % 2; ?>" sortable-group-id="">
						<td class="center">
							<?php echo HTMLHelper::_('grid.id', $i, $item->id); ?>
						</td>
						<td>
							<div class="pull-left">
								<?php if ($item->checked_out) : ?>
									<?php echo HTMLHelper::_('jgrid.checkedout', $i, $item->editor, $item->checked_out_time, 'orders.', $canCheckin); ?>
								<?php endif; ?>
								<?php if ($canEdit) : ?>
									<a href="<?php echo Route::_('index.php?option=com_cmgiftcard&task=order.edit&id=' . (int) $item->id); ?>">
										<?php echo $this->escape($item->order_number); ?></a>
								<?php else : ?>
									<?php echo $this->escape($item->order_number); ?>
								<?php endif; ?>
							</div>
						</td>
						<td class="hidden-phone">
							<?php
							if ($item->user_id > 0)
							{
								echo Factory::getUser($item->user_id)->username;
							}
							else
							{
								echo Text::_('COM_CMGIFTCARD_GUEST');
							}
							?>
						</td>
						<td class="hidden-phone">
							<?php echo implode('<br>', $billingInfo); ?>
						</td>
						<td >
							<?php if (count($item->cards)) : ?>
								<ul>
								<?php foreach ($item->cards as $card) : ?>
									<li><a href="index.php?option=com_cmgiftcard&task=card.edit&id=<?php echo $card->id; ?>" target="_blank"><?php echo $card->card_code; ?></a></li>
								<?php endforeach; ?>
								</ul>
							<?php endif; ?>
						</td>
						<td>
							<?php echo HTMLHelper::_('cmgiftcard.formatPrice', $item->amount); ?>
						</td>
						<td>
							<?php if ($item->order_status == 'unpaid') : ?>
								<p><span class="badge badge-info bg-info"><?php echo Text::_('COM_CMGIFTCARD_ORDER_STATUS_UNPAID'); ?></span></p>
							<?php elseif ($item->order_status == 'paid') : ?>
								<p><span class="badge badge-success bg-success"><?php echo Text::_('COM_CMGIFTCARD_ORDER_STATUS_PAID'); ?></span></p>

								<span class="btn btn-primary btn-sm send-order-email" data-id="<?php echo $item->id; ?>">
									<i class="fas fa-spinner fa-spin spinner"></i> <?php echo Text::_('COM_CMGIFTCARD_SEND_ORDER_EMAIL'); ?></span>
							<?php elseif ($item->order_status == 'refunded') : ?>
								<p><span class="badge badge-danger bg-danger"><?php echo Text::_('COM_CMGIFTCARD_ORDER_STATUS_REFUNDED'); ?></span></p>
							<?php endif; ?>
						</td>
						<td>
							<?php
							if ($item->created)
							{
								echo HTMLHelper::_('date', $item->created, Text::_('DATE_FORMAT_LC1'));
							}
							?>
						</td>
						<td class="hidden-phone">
							<?php echo $item->id; ?>
						</td>
					</tr>
				<?php endforeach; ?>
			</tbody>
		</table>
	<?php endif; ?>

	<input type="hidden" name="task" value="" />
	<input type="hidden" name="boxchecked" value="0" />
	<?php echo HTMLHelper::_('form.token'); ?>
</form>
