<?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\Uri\Uri;
use Joomla\CMS\Date\Date;

HTMLHelper::_('bootstrap.tooltip');
HTMLHelper::_('formbehavior.chosen', 'select');

$params = $this->params;
$items = $this->items;
$countries = CMGiftCardHelper::getCountries();
$listOrder = $this->escape($this->state->get('list.ordering'));
$listDirn = $this->escape($this->state->get('list.direction'));

Factory::getDocument()->addStyleSheet('components/com_cmgiftcard/assets/css/styles.css');
?>
<div class="cmgiftcard<?php echo $this->pageclass_sfx?>">
	<?php if ($params->get('show_page_heading')) : ?>
	<div class="page-header">
		<h1> <?php echo $this->escape($params->get('page_heading')); ?> </h1>
	</div>
	<?php endif; ?>

	<form action="<?php echo Uri::getInstance()->toString(); ?>" method="get">
		<div class="row mb-2">
			<div class="col-md-12">
				<?php echo $this->form->getInput('keyword'); ?>
			</div>
		</div>

		<div class="row">
			<div class="col-md-3">
				<?php echo $this->form->getInput('card_type'); ?>
			</div>

			<div class="col-md-3">
				<?php echo $this->form->getInput('order_status'); ?>
			</div>

			<div class="col-md-3">
				<?php echo $this->form->getInput('card_delivered'); ?>
			</div>

			<div class="col-md-3">
				<input type="submit" class="btn btn-primary" value="<?php echo Text::_('COM_CMGIFTCARD_SEARCH'); ?>">
			</div>
		</div>
	</form>

	<?php if (count($items)) : ?>
	<form action="<?php echo htmlspecialchars(Uri::getInstance()->toString()); ?>" method="post" name="adminForm" id="adminForm">
		<table class="table table-striped">
			<thead>
				<tr>
					<th>
						<?php echo HTMLHelper::_('grid.sort', 'COM_CMGIFTCARD_CARD_CODE', 'a.card_code', $listDirn, $listOrder); ?>
					</th>
					<th>
						<?php echo HTMLHelper::_('grid.sort', 'COM_CMGIFTCARD_ORDER_NUMBER', 'order_number', $listDirn, $listOrder); ?>
					</th>
					<th class="hidden-phone">
						<?php echo Text::_('COM_CMGIFTCARD_CARD_INFO'); ?>
					</th>
					<th class="hidden-phone">
						<?php echo Text::_('COM_CMGIFTCARD_SENDER'); ?>
					</th>
					<th class="hidden-phone">
						<?php echo Text::_('COM_CMGIFTCARD_CARD_RECIPIENT_INFO'); ?>
					</th>
					<th>
						<?php echo HTMLHelper::_('grid.sort', 'COM_CMGIFTCARD_AMOUNT', 'amount', $listDirn, $listOrder); ?>
					</th>
					<th>
						<?php echo Text::_('JSTATUS'); ?>
					</th>
				</tr>
			</thead>
			<tfoot>
				<tr>
					<td colspan="7">
						<?php echo $this->pagination->getPagesLinks(); ?>
					</td>
				</tr>
			</tfoot>
			<tbody>
				<?php foreach ($this->items as $i => $item) : ?>
					<tr>
						<td>
							<div>
								<a href="<?php echo CMGiftCardHelper::prepareRoute('index.php?option=com_cmgiftcard&view=card&code=' . $item->card_code); ?>">
										<?php echo $this->escape($item->card_code); ?></a>
							</div>
						</td>
						<td>
							<?php echo $item->order_number; ?>
						</td>
						<td class="hidden-phone">
							<?php if ($item->design_image != '') : ?>
							<p><img src="<?php echo CMGiftCardHelper::getImageUrl($item->design_image); ?>" class="staff-design-thumb"></p>
							<?php endif; ?>
							<p><span class="badge bg-success badge-success"><?php echo Text::_('COM_CMGIFTCARD_CARD_TYPE_SHORT_' . $item->card_type); ?></span></p>
							<?php if ($item->card_type == 'electronic') : ?>
								<?php if ($item->deliver_date) : ?>
									<?php $deliverDate = new Date($item->deliver_date) ;?>
									<p><span class="badge bg-info badge-info"><?php echo $deliverDate->format(Text::_('DATE_FORMAT_LC1')); ?></span></p>
								<?php endif; ?>
							<?php endif; ?>
						</td>
						<td class="hidden-phone">
							<?php
							if ($item->user_id > 0)
							{
								echo Factory::getUser($item->user_id)->username;
							}
							else
							{
								echo Text::_('COM_CMGIFTCARD_GUEST');
							}
							?>
							<br>(<?php echo $item->from_name; ?>)
						</td>
						<td class="hidden-phone">
							<?php echo $item->sender_first_name . ' ' . $item->sender_last_name; ?>
						</td>
						<td>
							<?php echo HTMLHelper::_('cmgiftcard.formatPrice', $item->amount); ?>
						</td>
						<td>
							<p>
							<?php if ($item->order_status == 'unpaid') : ?>
								<span class="badge bg-info badge-info"><?php echo Text::_('COM_CMGIFTCARD_ORDER_STATUS_UNPAID'); ?></span>
							<?php elseif ($item->order_status == 'paid') : ?>
								<span class="badge bg-success badge-success"><?php echo Text::_('COM_CMGIFTCARD_ORDER_STATUS_PAID'); ?></span>
							<?php elseif ($item->order_status == 'refunded') : ?>
								<span class="badge bg-danger badge-danger"><?php echo Text::_('COM_CMGIFTCARD_ORDER_STATUS_REFUNDED'); ?></span>
							<?php endif; ?>
							</p>

							<?php if ($item->card_type == 'electronic') : ?>
								<?php if (!$item->delivered) : ?>
								<p><span class="badge bg-danger badge-danger"><?php echo Text::_('COM_CMGIFTCARD_CARD_STATUS_NOT_DELIVERED'); ?></span></p>
								<?php elseif (!$item->delivered) :?>
								<p><span class="badge bg-success badge-success">
									<?php echo Text::_('COM_CMGIFTCARD_CARD_STATUS_DELIVERED'); ?>
								</span></p>
									<?php if ($item->delivered) : ?>
									<p><span class="badge bg-info badge-info">
										<?php echo HTMLHelper::_('date', $item->delivered, Text::_('DATE_FORMAT_LC1')); ?>
									</span></p>
									<?php endif; ?>
								<?php endif; ?>

							<?php endif; ?>
						</td>
					</tr>
				<?php endforeach; ?>
			</tbody>
		</table>

		<input type="hidden" name="filter_order" value="<?php echo $listOrder; ?>" />
		<input type="hidden" name="filter_order_Dir" value="<?php echo $listDirn; ?>" />
		<?php echo HTMLHelper::_('form.token'); ?>
	</form>
	<?php else : ?>
	<div class="alert alert-warning">
		<?php echo Text::_('COM_CMGIFTCARD_ORDER_NO_GIFT_CARDS_FOUND'); ?>
	</div>
	<?php endif; ?>
</div>
