<?php

namespace GriffinForms\Admin\Html\Elements\Submission;

class InputRadio extends \GriffinForms\Admin\Html\Elements\Submission\Format
{
    protected $field_options;

    protected function setFieldOptions()
    {
        $this->field_options = $this->field->getProp('field_options');
    }

    public function getHtml()
    {
        if (!is_array($this->field_options)) {
            return;
        }

        echo '<div class="d-flex my-2">';
        foreach ($this->field_options as $option_group) {
            $this->getOptionGroupHtml($option_group);
        }

        echo '</div>';
    }

    protected function getOptionGroupHtml($option_group)
    {
        echo '<div class="flex-column me-3">';
        
        foreach ($option_group as $option => $value) {
            echo '<div class="d-flex mb-1">';
            $this->radioIcon($value);
            $this->radioLabel($option);
            echo '</div>';
        }

        echo '</div>';
    }

    protected function radioIcon($value)
    {
        if ($value === $this->field_value) {
            echo '<span class="me-1 text-primary">';
            $this->mIcon('radio_button_checked');
        } else {
            echo '<span class="me-1">';
            $this->mIcon('radio_button_unchecked');
        }
        
        echo '</span>';
    }

    protected function radioLabel($option) 
    {
        echo '<span class="">';
        echo esc_html($option);
        echo '</span>';
    }
}