<?php 
namespace Meramo\Mrmbepages\ViewHelper\Form;

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;



class FormSelectViewHelper extends AbstractViewHelper
{
    /**
     * Tag Name 
     * 
     * @var string
     */
    protected $tagName = 'select';
    /**
     * Initialize Arguments
     */
    

    public function initializeArguments(): void
    {
        $this->registerArgument('label', 'string', 'select field label');
        $this->registerArgument('name', 'string', 'select name attribute');
        $this->registerArgument('id', 'string', 'id attribute select field');
        $this->registerArgument('options', 'array', 'Associative array with keys as integers and values as string');
        $this->registerArgument('required', 'boolean');
    }

    /**
     * @return string
     */
    public function render()
    {
        $output = '';

        if(isset($this->argument['label']) && $this->argument['label']) {
            $label = $this->argument['label'];
            $output .= '<label for="Category">'.$label.'</label>';
        }

        if(isset($this->argument['name']) && $this->argument['name']) {
            $this->tag->addAttribute($this->argument['name']);
        }
        
        if(isset($this->argument['id']) && $this->argument['id']) {
            $this->tag->addAttribute($this->argument['id']);
        }

        if(isset($this->argument['required']) && $this->argument['required']) {
            $this->tag->addAttribute($this->argument['required']);
        }

        if(isset($this->argument['options']) && $this->argument['options']) {
            if(is_array($this->argument['options'])) {
                $options = $this->argument['options'];
                foreach($options as $k => $value) {
                    $output .= '<option value="'.$value.'" >"'.ucfirst($value).'"</option>';
                }
            }
            
        }
        $this->tag->forceClosingTag(true);
        return $output;


    }
}