<?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;

HTMLHelper::_('behavior.keepalive');
HTMLHelper::_('behavior.formvalidator');
HTMLHelper::_('formbehavior.chosen', 'select');

$cart = $this->cart;
$formUrl = 'index.php?option=com_cmgiftcard&view=form&type=';
$form = $this->form;
$fieldsets = $form->getFieldsets();
$params = $this->params;

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

	<div class="cart">
		<?php if (!isset($cart['items']) || !count($cart['items'])) : ?>
		<div class="alert alert-warning">
			<?php echo Text::_('COM_CMGIFTCARD_CART_EMPTY_MESSAGE'); ?>
		</div>
		<?php else : ?>
		<table class="table table-striped">
			<?php foreach ($cart['items'] as $i => $item) : ?>
			<tr>
				<td class="thumbnail-container hidden-phone">
				<?php if (isset($item['design_image']) && $item['design_image'] != '') : ?>
					<img class="thumbnail" src="<?php echo CMGiftCardHelper::getImageUrl($item['design_image']); ?>" title="<?php echo htmlspecialchars($item['design_title']); ?>" alt="<?php echo htmlspecialchars($item['design_title']); ?>">
				<?php endif; ?>
				</td>
				<td>
					<p class="type">
					<?php
					if ($item['card_type'] == 'physical') :
						echo Text::_('COM_CMGIFTCARD_PHYSICAL_DEFAULT_TITLE');
					else :
						echo Text::_('COM_CMGIFTCARD_ELECTRONIC_DEFAULT_TITLE');
					endif;
					?>
					</p>
					<p><?php echo Text::_('COM_CMGIFTCARD_CART_TO') . $item['to_name']; ?></p>
				</td>
				<td><?php echo $item['amount_formatted']; ?></td>
				<td class="actions">
					<a href="<?php echo CMGiftCardHelper::prepareRoute($formUrl . $item['card_type'] . '&item_id=' . ($i + 1)); ?>" class="btn btn-primary btn-sm"><?php echo Text::_('COM_CMGIFTCARD_CART_EDIT'); ?></a>
					<form method="post" action="<?php echo CMGiftCardHelper::prepareRoute('index.php?option=com_cmgiftcard&task=cart.removeFromCart'); ?>">
						<input type="hidden" name="index" value="<?php echo $i; ?>">
						<?php echo HTMLHelper::_('form.token'); ?>
						<button type="submit" class="btn btn-danger btn-sm"><?php echo Text::_('COM_CMGIFTCARD_CART_REMOVE'); ?></button>
					</form>
				</td>
			</tr>
			<?php endforeach; ?>
		</table>

		<form method="post" action="<?php echo CMGiftCardHelper::prepareRoute('index.php?option=com_cmgiftcard&task=cart.emptyCart'); ?>" class="empty-button-container">
			<?php echo HTMLHelper::_('form.token'); ?>
			<button type="submit" class="btn btn-danger">
				<?php echo Text::_('COM_CMGIFTCARD_EMPTY_CART_BUTTON'); ?>
			</button>
		</form>
		<?php endif; ?>

		<?php if (isset($cart['items']) && count($cart['items'])) : ?>
		<form method="post" action="<?php echo CMGiftCardHelper::prepareRoute('index.php?option=com_cmgiftcard&task=cart.checkout'); ?>" class="form-horizontal form-validate">
			<?php foreach ($fieldsets as $fieldset) : ?>
				<?php
				$fieldQuantity = 0;

				foreach ($form->getFieldset($fieldset->name) as $field)
				{
					$fieldQuantity++;
				}
				?>
				<?php if ($fieldQuantity) : ?>
					<?php if ($fieldset->name != 'payment_method' || ($fieldset->name == 'payment_method' && count($this->payments) > 1) || ($fieldset->name == 'payment_method' && count($this->payments) == 1 && $params->get('hide_lone_payment_method', 0) == 0)) : ?>
					<fieldset>
						<legend><?php echo Text::_($fieldset->label); ?></legend>

						<?php foreach ($form->getFieldset($fieldset->name) as $field) : ?>
							<?php echo $form->renderField($field->fieldname); ?>
						<?php endforeach; ?>
					</fieldset>
					<?php endif; ?>
				<?php endif; ?>
			<?php endforeach; ?>

			<div class="checkout-button-container">
				<button type="submit" class="btn btn-primary btn-large validate"><?php echo Text::_('COM_CMGIFTCARD_CHECKOUT_BUTTON'); ?></button>
			</div>
			<?php echo HTMLHelper::_('form.token'); ?>
		</form>
		<?php endif; ?>
	</div>
</div>
