<?php
/*
 * Software Name: Eorisis Pixels
 * Software URI: https://eorisis.com/shop/software/joomla/pixels
 * Author: Eorisis
 * Author URI: https://eorisis.com
 * Copyright: Copyright (C) 2011-2025 EORISIS. All Rights Reserved.
 * License: GNU General Public License version 2; See https://eorisis.com/gpl
 * Terms of Service: https://eorisis.com/tos
 */
defined('_JEXEC') or die;

use com_eorisis_pixels\framework\lib\core\cms\language as cms_language;
use com_eorisis_pixels\framework\lib\core\model\form as form_model;
use com_eorisis_pixels\framework\lib\core\model\params;
use com_eorisis_pixels\framework\lib\core\model\clean;
use com_eorisis_pixels\framework\lib\core\view\help;

if (!class_exists('EorisisFormField', false))
{
	if (version_compare(JVERSION, '3.8', '>='))
	{
		JLoader::registerAlias('EorisisFormField', '\\Joomla\\CMS\\Form\\FormField');
	}
	else
	{
		JLoader::registerAlias('EorisisFormField', 'JFormField');
	}
}

class JFormFieldAgent extends EorisisFormField
{
	protected $type = 'agent';
	protected $form;

	// --------------------------------------------------

	protected function getLabel() : string
	{
		if (!params::field_value('label', $this->element))
		{
			return '';
		}

		return parent::getLabel().help::icon($this->element);
	}

	// --------------------------------------------------

	protected function getInput() : string
	{
		form_model::set($this->form);
		$id = (string)$this->element['name'];
		$default_original = $this->default;
		$is_pro = defined('EORISIS_PIXELS_PRO');

		if ($is_pro)
		{
			$value = form_model::get($id, $this->default);
			if ($value !== '')
			{
				// In case a constant is saved..
				if ($value == $default_original)
				{
					$value = $this->default;
				}

				if ($value)
				{
					$value = clean::string($value);
				}
			}
		}

		$args = [];

		if ($this->class)
		{
			$args['class'] = $this->class;
		}

		$html = '';

		if (!$is_pro)
		{
			$html .= '<div class="flex-row">';

			$value = 'Eorisis';
			$args['disabled'] = true;
			$args['title'] = cms_language::text('EO_UPGRADE_TO_PRO_TO_USE_FEATURE');
		}

		$html .= form_model::text($id, $value, $args);

		if (!$is_pro)
		{
			$html .=	'<div class="field-notice">'.cms_language::text('EO_UPGRADE_TO_PRO').'</div>';
			$html .= '</div>';
		}

		return $html;
	}
}
