<?php
/**
 * @package         FireBox
 * @version         2.1.12 Free
 * 
 * @author          FirePlugins <info@fireplugins.com>
 * @link            https://www.fireplugins.com
 * @copyright       Copyright © 2024 FirePlugins All Rights Reserved
 * @license         GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later
*/

namespace FireBox\Core\Form\Fields\Fields;

if (!defined('ABSPATH'))
{
	exit; // Exit if accessed directly.
}

use FPFramework\Base\Filter;

class Dropdown extends \FireBox\Core\Form\Fields\FieldChoice
{
	protected $type = 'dropdown';
	
	/**
	 * Validate the field.
	 * 
	 * @param   mixed  $value
	 * 
	 * @return  void
	 */
	public function validate(&$value = '')
	{
		$value = Filter::getInstance()->clean($value);

		return parent::validate($value);
	}

	/**
	 * Returns the field input.
	 * 
	 * @return  void
	 */
	public function getInput()
	{
		if (!$choices = $this->getOptionValue('choices'))
		{
			return;
		}

		$selectedValue = $this->getOptionValue('value') ? $this->getOptionValue('value') : ($this->getOptionValue('placeholder') ? '' : '');
		?>
		<div class="fb-form-select-wrapper">
			<select
				type="text"
				id="fb-form-input-<?php esc_attr_e($this->getOptionValue('id')); ?>"
				name="fb_form[<?php esc_attr_e($this->getOptionValue('name')); ?>]"
				class="fb-form-input<?php echo $this->getOptionValue('input_css_class') ? ' ' . esc_attr(implode(' ', $this->getOptionValue('input_css_class'))) : ''; ?>"
				<?php if ($this->getOptionValue('required')): ?>
					required
				<?php endif; ?>
			>
				<?php
				foreach ($choices as $choice)
				{
					$value = !empty($choice['value']) ? $choice['value'] : '';
					$isDisabled = isset($choice['disabled']) ? $choice['disabled'] : false;
					$isSelected = isset($choice['selected']) && $choice['selected'] ? true : false;
					if ($selectedValue)
					{
						$isSelected = (string) $selectedValue === (string) $value;
					}
					?>
					<option
						value="<?php esc_attr_e($value); ?>"
						<?php echo $isSelected ? ' selected' : ''; ?>
						<?php echo $isDisabled ? ' disabled' : ''; ?>
					>
						<?php esc_html_e($choice['label']); ?>
					</option>
					<?php
				}
				?>
			</select>
		</div>
		<?php
	}
}