<?php
/**
 * @package     CMGiftCard
 * @subpackage  com_cmgiftcard
 * @copyright   Copyright (C) 2016-2024 CMExension
 * @license     GNU General Public License version 2 or later
 */

defined('JPATH_BASE') or die();

require_once JPATH_ROOT . '/administrator/components/com_cmgiftcard/models/base_fields/text.php';

use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Form\Field\TextField;

/**
 * Form field for gift card amount.
 *
 * @package     CMGiftCard
 * @subpackage  com_cmgiftcard
 * @since       1.0.0
 */
class JFormFieldCMGCAmount extends CMGiftCardTextField
{
	/**
	 * The form field type.
	 *
	 * @var    string
	 * @since  1.0.0
	 */
	protected $type = 'CMGCAmount';

	/**
	 * Method to get the field input markup for amount input.
	 *
	 * @return  string  The field input markup.
	 *
	 * @since   1.0.0
	 */
	protected function getInput()
	{
		HTMLHelper::_('jquery.framework');

		$html = array();
		$params = ComponentHelper::getParams('com_cmgiftcard');
		$cardType = Factory::getApplication()->input->get('type', 'electronic', 'word');
		$symbolPosition = $params->get('currency_symbol_position', 'before');
		$symbol = $params->get('currency_symbol');

		Factory::getDocument()->addScript('components/com_cmgiftcard/assets/js/cmgiftcard.js');

		if ($cardType == 'physical')
			$amountType = $params->get('physical_amount_type', 'custom');
		else
			$amountType = $params->get('electronic_amount_type', 'custom');

		if ($amountType == 'defined' || $amountType == 'defined_custom')
		{
			if ($cardType == 'physical')
				$amountString = $params->get('physical_defined_amounts');
			else
				$amountString = $params->get('electronic_defined_amounts');

			$amounts = explode(';', $amountString);

			if (count($amounts))
			{
				$html[] = '<ul class="amount-selector">';

				foreach ($amounts as $amount)
				{
					$amount = trim($amount);

					$html[] = ' <li><span class="btn" data-amount="' . $amount . '">' . HTMLHelper::_('cmgiftcard.formatPrice', $amount, $params) . '</span></li>';
				}

				$html[] = '</ul>';

				$html[] = '<input type="hidden" name="' . $this->name . '" id="' . $this->id . '" value="'
					. htmlspecialchars($this->value, ENT_COMPAT, 'UTF-8') . '" />';
			}
		}

		if ($amountType == 'custom' || $amountType == 'defined_custom')
		{
			// Translate placeholder text
			$hint = $this->translateHint ? Text::_($this->hint) : $this->hint;

			// Initialize some field attributes.
			$size         = !empty($this->size) ? ' size="' . $this->size . '"' : '';
			$maxLength    = !empty($this->maxLength) ? ' maxlength="' . $this->maxLength . '"' : '';
			$class        = !empty($this->class) ? ' class="' . $this->class . '"' : '';
			$readonly     = $this->readonly ? ' readonly' : '';
			$disabled     = $this->disabled ? ' disabled' : '';
			$required     = $this->required ? ' required aria-required="true"' : '';
			$hint         = $hint ? ' placeholder="' . $hint . '"' : '';
			$autocomplete = !$this->autocomplete ? ' autocomplete="off"' : ' autocomplete="' . $this->autocomplete . '"';
			$autocomplete = $autocomplete == ' autocomplete="on"' ? '' : $autocomplete;
			$autofocus    = $this->autofocus ? ' autofocus' : '';
			$multiple     = $this->multiple ? ' multiple' : '';
			$spellcheck   = $this->spellcheck ? '' : ' spellcheck="false"';

			// Including fallback code for HTML5 non supported browsers.
			HTMLHelper::_('script', 'system/html5fallback.js');
		}

		if ($amountType == 'custom')
		{
			$onchange = $this->onchange ? ' onchange="' . $this->onchange . '"' : '';

			if ($symbolPosition == 'before')
			{
				$html[] = '<div class="input-prepend">';
				$html[] = '<span class="add-on">' . $symbol . '</span>';
			}
			else
			{
				$html[] = '<div class="input-append">';
			}

			$html[] = '<input type="text" name="' . $this->name . '"' . $class . ' id="' . $this->id . '" value="'
				. htmlspecialchars($this->value, ENT_COMPAT, 'UTF-8') . '"' . $spellcheck . $size . $disabled . $readonly
				. $onchange . $autocomplete . $multiple . $maxLength . $hint . $required . $autofocus . ' />';

			if ($symbolPosition == 'before')
			{
				$html[] = '</div>';
			}
			else
			{
				$html[] = '<span class="add-on">' . $symbol . '</span>';
				$html[] = '</div>';
			}
		}
		elseif ($amountType == 'defined_custom')
		{
			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 = HTMLHelper::_('cmgiftcard.formatPrice', $minAmount, $params);
			$maxAmount = HTMLHelper::_('cmgiftcard.formatPrice', $maxAmount, $params);

			$html[] = '<div class="form-instruction">' . Text::sprintf('COM_CMGIFTCARD_FORM_CUSTOM_AMOUNT_INSTRUCTION', $minAmount, $maxAmount) . '</div>';

			$onchange = ' onchange="checkCustomAmount();"';

			if ($symbolPosition == 'before')
			{
				$html[] = '<div class="input-prepend">';
				$html[] = '<span class="add-on">' . $symbol . '</span>';
			}
			else
			{
				$html[] = '<div class="input-append">';
			}

			$html[] = '<input type="text" ' . $class . ' id="customAmount" value="'
				. htmlspecialchars($this->value, ENT_COMPAT, 'UTF-8') . '"' . $spellcheck . $size . $disabled . $readonly
				. $onchange . $autocomplete . $multiple . $maxLength . $hint . $required . $autofocus . ' />';

			if ($symbolPosition == 'before')
			{
				$html[] = '</div>';
			}
			else
			{
				$html[] = '<span class="add-on">' . $symbol . '</span>';
				$html[] = '</div>';
			}
		}

		return implode("\n", $html);
	}
}
