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

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();
$pdf		= $this->params->get('electronic_pdf', 0);

$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=cards'); ?>" 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 : ?>
		<div class="card mark-delivered mb-4">
			<div class="card-body">
				<p><?php echo Text::_('COM_CMGIFTCARD_CARD_MARK_DELIVERED_INSTRUCTION'); ?></p>
				<div class="row row-cols-lg-auto g-3 align-items-center">
  					<div class="col-12">
						<?php echo HTMLHelper::_('calendar', Factory::getDate('now', Factory::getApplication()->get('offset')), 'date', 'date', '%Y-%m-%d %H:%M:%S'); ?>
					</div>
					<div class="col-12">
						<span onclick="if (document.adminForm.boxchecked.value==0){alert('<?php echo Text::_('JLIB_HTML_PLEASE_MAKE_A_SELECTION_FROM_THE_LIST', true); ?>');} else if (document.adminForm.date.value==''){alert('<?php echo Text::_('COM_CMGIFTCARD_ERROR_EMPTY_DELIVERY_DATE', true); ?>');} else {Joomla.submitbutton('cards.markDelivered');}" class="btn btn-primary">
							<?php echo Text::_('COM_CMGIFTCARD_CARD_MARK_DELIVERED_BUTTON'); ?>
						</span>
					</div>
					<div class="col-12">
						<span onclick="if (document.adminForm.boxchecked.value==0){alert('<?php echo Text::_('JLIB_HTML_PLEASE_MAKE_A_SELECTION_FROM_THE_LIST', true); ?>');} else if (document.adminForm.date.value==''){alert('<?php echo Text::_('COM_CMGIFTCARD_ERROR_EMPTY_DELIVERY_DATE', true); ?>');} else {Joomla.submitbutton('cards.markRedeemed');}" class="btn btn-primary">
							<?php echo Text::_('COM_CMGIFTCARD_CARD_MARK_REDEEMED_BUTTON'); ?>
						</span>
					</div>
				</div>
			</div>
		</div>
		<table class="table table-striped" id="cardList">
			<thead>
				<tr>
					<th width="1%" class="center">
						<?php echo HTMLHelper::_('grid.checkall'); ?>
					</th>
					<th>
						<?php echo HTMLHelper::_('searchtools.sort', 'COM_CMGIFTCARD_CARD_FIELD_CARD_CODE_LABEL', 'a.card_code', $listDirn, $listOrder); ?>
					</th>
					<th>
						<?php echo HTMLHelper::_('searchtools.sort', 'COM_CMGIFTCARD_ORDER_FIELD_ORDER_NUMBER_LABEL', '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_CARD_INFO'); ?>
					</th>
					<th class="hidden-phone">
						<?php echo Text::_('COM_CMGIFTCARD_CARD_RECIPIENT_INFO'); ?>
					</th>
					<th>
						<?php echo Text::_('COM_CMGIFTCARD_AMOUNT'); ?>
					</th>
					<th>
						<?php echo Text::_('COM_CMGIFTCARD_PAYMENT'); ?>
					</th>
					<th>
						<?php echo Text::_('COM_CMGIFTCARD_DELIVERY'); ?>
					</th>
					<th>
						<?php echo Text::_('COM_CMGIFTCARD_REDEMPTION'); ?>
					</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="11">
						<?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;

					$recipientInfo = array();
					$recipientInfo[] = $item->to_name;
					$recipientInfo[] = $item->to_email;

					if ($item->to_address_1 != '')
						$recipientInfo[] = $item->to_address_1;

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

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

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

					if ($item->to_city != '')
						$recipientInfo[] = $item->to_city;

					if ($item->to_state != '')
						$recipientInfo[] = $item->to_state;

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

					if ($this->params->get('country') && $item->to_country != '' && isset($countries[$item->to_country]))
						$recipientInfo[] = $countries[$item->to_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, 'cards.', $canCheckin); ?>
								<?php endif; ?>
								<?php if ($canEdit) : ?>
									<a href="<?php echo Route::_('index.php?option=com_cmgiftcard&task=card.edit&id=' . (int) $item->id); ?>">
										<?php echo $this->escape($item->card_code); ?></a>
								<?php else : ?>
									<?php echo $this->escape($item->card_code); ?>
								<?php endif; ?>
							</div>
						</td>
						<td>
							<?php echo $item->order_number; ?>
						</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 if ($item->design_image != '') : ?>
							<p><img src="../<?php echo $item->design_image; ?>" style="max-width: 100px;"></p>
							<?php endif; ?>
							<p><span class="badge badge-success bg-success"><?php echo Text::_('COM_CMGIFTCARD_CARD_TYPE_SHORT_' . $item->card_type); ?></span></p>
							<?php if ($item->card_type == 'electronic' && $item->deliver_date) : ?>
							<?php $deliverDate = new Date($item->deliver_date); ?>
							<p><span class="badge badge-info bg-info"><?php echo $deliverDate->format(Text::_('DATE_FORMAT_LC1')); ?></span></p>
							<?php endif; ?>
						</td>
						<td class="hidden-phone">
							<?php echo implode('<br>', $recipientInfo); ?>
						</td>
						<td>
							<?php echo HTMLHelper::_('cmgiftcard.formatPrice', $item->amount); ?>
						</td>
						<td>
							<p>
							<?php if ($item->order_status == 'unpaid') : ?>
								<span class="badge badge-info bg-info"><?php echo Text::_('COM_CMGIFTCARD_ORDER_STATUS_UNPAID'); ?></span>
							<?php elseif ($item->order_status == 'paid') : ?>
								<span class="badge badge-success bg-success"><?php echo Text::_('COM_CMGIFTCARD_ORDER_STATUS_PAID'); ?></span>
							<?php elseif ($item->order_status == 'refunded') : ?>
								<span class="badge badge-danger bg-danger"><?php echo Text::_('COM_CMGIFTCARD_ORDER_STATUS_REFUNDED'); ?></span>
							<?php endif; ?>
							</p>
						</td>
						<td>
							<p>
								<?php if ($item->delivered === null) : ?>
								<span class="badge badge-danger bg-danger"><?php echo Text::_('COM_CMGIFTCARD_CARD_STATUS_NOT_DELIVERED'); ?></span>
								<?php else :?>
								<span class="badge badge-success bg-success">
										<?php echo Text::_('COM_CMGIFTCARD_CARD_STATUS_DELIVERED'); ?>
									</span><br><span class="badge badge-info bg-info">
										<?php echo HTMLHelper::_('date', $item->delivered, Text::_('DATE_FORMAT_LC1')); ?>
									</span>
								<?php endif; ?>
							</p>

							<?php if ($item->card_type == 'electronic') : ?>
							<p><span class="btn btn-primary btn-sm send-card-email" data-id="<?php echo $item->id; ?>" title="<?php echo Text::_('COM_CMGIFTCARD_SEND_EGIFT_CARD_EMAIL'); ?>">
								<i class="fas fa-spinner fa-spin spinner"></i> <i class="fas fa-envelope"></i></span>
								<?php if ($pdf) : ?>
									<a class="btn btn-primary btn-sm" href="index.php?option=com_cmgiftcard&task=card.download&<?php echo $token; ?>=1&id=<?php echo $item->id; ?>" title="<?php echo Text::_('COM_CMGIFTCARD_DOWNLOAD_PDF_CARD'); ?>">
										<i class="fas fa-download"></i></a>
								<?php endif; ?>
							</p>
							<?php endif; ?>
						</td>
						<td>
							<p>
							<?php if ($item->redeemed !== null) : ?>
							<span class="badge badge-success bg-success">
									<?php echo Text::_('COM_CMGIFTCARD_CARD_STATUS_REDEEMED'); ?>
								</span><br><span class="badge badge-info bg-info">
								<?php echo HTMLHelper::_('date', $item->redeemed, Text::_('DATE_FORMAT_LC1')); ?>
							</span>
							<?php else: ?>
							<span class="badge badge-danger bg-danger"><?php echo Text::_('COM_CMGIFTCARD_CARD_STATUS_NOT_REDEEMED'); ?></span>
							<?php endif; ?>
							</p>
						</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>
