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

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

$form = $this->form;
$params = $this->state->get('params');
$dateSelection = $params->get('electronic_date_selection', 1);
$app = Factory::getApplication();
$itemId = $app->input->get('item_id', 0, 'uint');

$giftCardType = $params->get('gift_card_type', 'all');

if ($giftCardType == 'physical')
{
	$cardType = 'physical';
}
elseif ($giftCardType == 'electronic')
{
	$cardType = 'electronic';
}
else
{
	$cardType = $app->input->get('type', 'electronic', 'word');
}

$fieldsets = $form->getFieldsets();

if ($itemId <= 0)
{
	$itemExists = false;
}
else
{
	$session = Factory::getSession();
	$cart = $session->get('cart', array(), 'CMGiftCard');

	$itemExists = (isset($cart['items'][$itemId - 1])) ? true : false;
}

if ($cardType != 'physical' && $cardType != 'electronic')
	$cardType = 'electronic';

if ($cardType == 'physical')
{
	$minAmount = $params->get('physical_min_amount');
	$maxAmount = $params->get('physical_max_amount');
}
else
{
	$minAmount = $params->get('electronic_min_amount');
	$maxAmount = $params->get('electronic_max_amount');
}

$minAmount = floatval($minAmount);
$maxAmount = floatval($maxAmount);

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

$doc->addScriptDeclaration("
	var minAmount = " . $minAmount . ";
	var maxAmount = " . $maxAmount . ";

	Joomla.submitbutton = function(task) {
		if (document.formvalidator.isValid(document.getElementById('adminForm'))) {
			Joomla.submitform(task);
		} else {
			jQuery('html, body').animate({
				scrollTop: jQuery('#system-message-container').offset().top
			}, 1000);
		}
	}
");
?>
<div class="cmgiftcard<?php echo $this->pageclass_sfx?>">
	<form action="<?php echo Route::_('index.php?option=com_cmgiftcard'); ?>" method="post" name="adminForm" id="adminForm" class="form-validate gift-card-form">
	<?php foreach ($fieldsets as $fieldset) : ?>
		<?php
		$fieldQuantity = 0;

		foreach ($form->getFieldset($fieldset->name) as $field)
		{
			$fieldQuantity++;
		}
		?>
		<?php if ($fieldQuantity > 0) : ?>
			<fieldset>
				<?php if ($fieldset->label != '') : ?>
				<legend><?php echo Text::_($fieldset->label); ?></legend>
				<?php endif; ?>
				<?php if ($fieldset->description) : ?>
				<p><?php echo $fieldset->description; ?></p>
				<?php endif; ?>

				<?php foreach ($form->getFieldset($fieldset->name) as $field) : ?>
					<?php if ($field->hidden) : ?>
						<div class="form-group">
							<?php echo $field->input; ?>
						</div>
					<?php else: ?>
						<div class="form-group">
							<?php echo $field->label; ?>
							<div><?php echo $field->input; ?></div>
						</div>
					<?php endif; ?>
				<?php endforeach; ?>
			</fieldset>
		<?php endif; ?>
	<?php endforeach; ?>

		<div class="text-right text-end">
			<button type="button" class="btn btn-primary btn-large" onclick="Joomla.submitbutton('cart.addToCart')">
				<?php
				if (!$itemExists) :
					echo Text::_('COM_CMGIFTCARD_ADD_TO_CART');
				else :
					echo Text::_('COM_CMGIFTCARD_UPDATE_CART');
				endif;
				?>
			</button>
		</div>

		<input type="hidden" name="item_id" value="<?php echo $itemId; ?>" />
		<input type="hidden" name="task" value="" />
		<input type="hidden" name="return" value="<?php echo base64_encode(Uri::getInstance()->toString()); ?>" />
		<?php echo HTMLHelper::_('form.token'); ?>
	</form>
</div>
