<?php

namespace GriffinForms\Admin\Html\FormControls;

class SelectField extends \GriffinForms\Admin\Html\FormControls\Format
{
    protected $options;

    protected function setCssClass()
    {
        if (!empty($this->data['app_context'])) {
            $this->css_class = 'class="form-select gf-app-control-input griffinforms-' . $this->purgeString($this->name) . ' griffinforms-' . strtolower($this->meta) . ' griffinforms-formcontrol"';
        }
    }
    
    public function selectOptions($options)
    {
        if (is_array($options)) {
            $this->options = $options;
        } else {
            $this->options = array();
        }
    }
    
    public function html()
    {
        echo '<select ' . wp_kses_post($this->attr_name . ' ' . $this->css_class . ' ' . $this->css_id) . '>';
        foreach($this->options as $value => $text) {
            if ($value == $this->value) {
                echo '<option value="' . esc_attr($value) . '" selected>' . esc_html($text) . '</option>';
            } else {
                echo '<option value="' . esc_attr($value) . '">' . esc_html($text) . '</option>';
            }
        }
        echo '</select>';
        
    }
}
