<?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 custom gift card amount.
 *
 * @package     CMGiftCard
 * @subpackage  com_cmgiftcard
 * @since       1.0.0
 */
class JFormFieldCMGCCustomAmount extends CMGiftCardTextField
{
	/**
	 * The form field type.
	 *
	 * @var    string
	 * @since  1.0.0
	 */
	protected $type = 'CMGCCustomAmount';

	/**
	 * Method to get the field input markup for amount input.
	 *
	 * @return  string  The field input markup.
	 *
	 * @since   1.0.0
	 */
	protected function getInput()
	{
		$html = array();
		$params = ComponentHelper::getParams('com_cmgiftcard');
		$app = Factory::getApplication();
		$cardType = $app->input->get('type', 'electronic', 'word');
		$symbolPosition = $params->get('currency_symbol_position', 'before');
		$symbol = $params->get('currency_symbol');

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

		// 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"';
		$onchange     = $this->onchange ? ' onchange="' . $this->onchange . '"' : '';

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

		$html[] = '<p>' . Text::sprintf('COM_CMGIFTCARD_AMOUNT_FROM', $minAmount, $maxAmount) . '</p>';

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

		$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="input-group-append">';
			$html[] = '<span class="input-group-text">';
			$html[] = $symbol;
			$html[] = '</span>';
			$html[] = '</span>';

			$html[] = '</div>';
		}

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