<?php
	/**
	 * @package     Joomla.Administrator
	 * @subpackage  com_formea
	 *
	 * @copyright   Copyright (C) 2010-2023 Feseur Sdn Bhd. All rights reserved.
	 * @license     GNU General Public License version 2 or later; see LICENSE.txt
	 */

  namespace Joomla\Component\Formea\Plugins\Element\CountryState\Field;

	defined('_JEXEC') or die('Restricted access');

	use Joomla\CMS\Factory;
	use Joomla\CMS\Form\Field\ListField;
	use Joomla\CMS\HTML\HTMLHelper;
  use Joomla\Database\DatabaseDriver;

  /**
	 * name Form Field class for the Genejunction component
	 */
	class FormeaElementCountryListField extends ListField
	{
		/**
		 * The name field type.
		 *
		 * @var    string
		 */
		protected $type = 'FormeaElementCountryList';

		/**
		 * Method to get a list of options for a list input.
		 *
		 * @return  array    An array of JHtml options.
		 */
		protected function getOptions()
		{
      $container = Factory::getContainer();
      /** @var DatabaseDriver $db */
      $db = $container->get('DatabaseDriver');
			$query = $db->getQuery(true);
			$query->select(['id', 'country_name AS title']);
			$query->from($db->quoteName('#__formea_countries'));
			$query->where($db->quoteName('published') . ' = 1');
			$query->order('country_name ASC');
			$db->setQuery($query);
			$items   = $db->loadObjectList();
			$options = array();
			if ($items)
			{
				foreach ($items as $item)
				{
					$options[] = HTMLHelper::_('select.option', $item->id, ucwords($item->title));
				}
			}
			$options = array_merge(parent::getOptions(), $options);

			return $options;
		}
	}
