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

HTMLHelper::_('behavior.formvalidator');

if (version_compare(JVERSION, '4.0.0-alpha1', '<'))
{
	HTMLHelper::_('formbehavior.chosen', 'select');
}

Factory::getDocument()->addScriptDeclaration('
	Joomla.submitbutton = function(task) {
		if (task == "template.cancel" || document.formvalidator.isValid(document.getElementById("template-form"))) {
			Joomla.submitform(task, document.getElementById("template-form"));
		}
	};
');

Factory::getDocument()->addStyleSheet('components/com_cmgiftcard/assets/css/cmgiftcard.css');

$fieldsets = $this->form->getFieldsets();

$tags = array();

$tags['order_success'] = array(
		'{first_name}' => Text::_('COM_CMGIFTCARD_TEMPLATE_TAG_SENDER_FIRST_NAME'),
		'{last_name}' => Text::_('COM_CMGIFTCARD_TEMPLATE_TAG_SENDER_LAST_NAME'),
		'{completed_date}' => Text::_('COM_CMGIFTCARD_TEMPLATE_TAG_COMPLETED_DATE'),
		'{order_amount}' => Text::_('COM_CMGIFTCARD_TEMPLATE_TAG_ORDER_AMOUNT'),
		'{order_number}' => Text::_('COM_CMGIFTCARD_TEMPLATE_TAG_ORDER_NUMBER'),
		'{payment_method}' => Text::_('COM_CMGIFTCARD_TEMPLATE_TAG_PAYMENT_METHOD'),
		'{transaction_id}' => Text::_('COM_CMGIFTCARD_TEMPLATE_TAG_TRANSACTION_ID'),
	);

$tags['gift_card_sender'] = array(
		'{sender_first_name}' => Text::_('COM_CMGIFTCARD_TEMPLATE_TAG_SENDER_FIRST_NAME'),
		'{sender_last_name}' => Text::_('COM_CMGIFTCARD_TEMPLATE_TAG_SENDER_LAST_NAME'),
		'{recipient_name}' => Text::_('COM_CMGIFTCARD_TEMPLATE_TAG_RECIPIENT_NAME'),
		'{recipient_email}' => Text::_('COM_CMGIFTCARD_TEMPLATE_TAG_RECIPIENT_EMAIL'),
		'{order_date}' => Text::_('COM_CMGIFTCARD_TEMPLATE_TAG_ORDER_DATE'),
		'{completed_date}' => Text::_('COM_CMGIFTCARD_TEMPLATE_TAG_COMPLETED_DATE'),
		'{gift_card_amount}' => Text::_('COM_CMGIFTCARD_TEMPLATE_TAG_GIFT_CARD_AMOUNT'),
		'{order_amount}' => Text::_('COM_CMGIFTCARD_TEMPLATE_TAG_ORDER_AMOUNT'),
		'{order_number}' => Text::_('COM_CMGIFTCARD_TEMPLATE_TAG_ORDER_NUMBER'),
	);

$tags['gift_card_recipient'] = array(
		'{sender_name}' => Text::_('COM_CMGIFTCARD_TEMPLATE_TAG_SENDER_NAME'),
		'{recipient_name}' => Text::_('COM_CMGIFTCARD_TEMPLATE_TAG_RECIPIENT_NAME'),
		'{gift_card_image}' => Text::_('COM_CMGIFTCARD_TEMPLATE_TAG_GIFT_CARD_IMAGE'),
		'{gift_card_amount}' => Text::_('COM_CMGIFTCARD_TEMPLATE_TAG_GIFT_CARD_AMOUNT'),
		'{gift_card_code}' => Text::_('COM_CMGIFTCARD_TEMPLATE_TAG_GIFT_CARD_CODE'),
		'{gift_card_message}' => Text::_('COM_CMGIFTCARD_TEMPLATE_TAG_GIFT_CARD_MESSAGE'),
		'{order_date}' => Text::_('COM_CMGIFTCARD_TEMPLATE_TAG_ORDER_DATE'),
		'{completed_date}' => Text::_('COM_CMGIFTCARD_TEMPLATE_TAG_COMPLETED_DATE'),
	);
?>

<form action="<?php echo Route::_('index.php?option=com_cmgiftcard&layout=edit&id=' . (int) $this->item->id); ?>" method="post" name="adminForm" id="template-form" class="form-validate form-horizontal">
	<?php echo HTMLHelper::_('bootstrap.startTabSet', 'myTab', array('active' => 'details')); ?>

	<?php foreach ($fieldsets as $fieldset) : ?>
		<?php echo HTMLHelper::_('bootstrap.addTab', 'myTab', $fieldset->name, Text::_($fieldset->label, true)); ?>
		<?php foreach ($this->form->getFieldset($fieldset->name) as $field) : ?>
			<?php if ($field->hidden) : ?>
				<div class="control-group">
					<div class="controls"><?php echo $field->input; ?></div>
				</div>
			<?php else: ?>
				<div class="control-group">
					<div class="control-label"><?php echo $field->label; ?></div>
					<div class="controls">
						<?php if ($field->fieldname != 'body') : ?>
						<?php echo $field->input; ?>
						<?php else : ?>
						<div class="row-fluid">
							<div class="span8"><?php echo $field->input; ?></div>
							<div class="span4">
								<?php if (isset($tags[$this->item->template_id])) : ?>
								<p><?php echo Text::_('COM_CMGIFTCARD_TEMPLATE_TAGS'); ?></p>
								<ul>
								<?php foreach ($tags[$this->item->template_id] as $tag => $description) : ?>
									<li><?php echo $tag; ?>: <?php echo $description; ?></li>
								<?php endforeach; ?>
								</ul>
								<?php endif; ?>
							</div>
						</div>
						<?php endif; ?>
					</div>
				</div>
			<?php endif; ?>
		<?php endforeach; ?>
		<?php echo HTMLHelper::_('bootstrap.endTab'); ?>
	<?php endforeach; ?>

	<?php echo HTMLHelper::_('bootstrap.endTabSet'); ?>

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