<?php

namespace app\components;

use Yii;

use app\components\BaseField;

class RadioGroupField extends BaseField{
    public $form_id;
    public $form_version;
    public $items;
    public $selected;
    public $separator;
    public $document;

    public function __construct($structure, $document, $form_id = null){
        parent::__construct($structure, $document, $form_id);
        $this->separator = isset($structure['inline']) && $structure['inline'] ? ' ' : '<br>';
        foreach($this->structure['values'] as $item){
            if($item['status'] == 'Active'){
                $this->items[] = [
                    'value' => $item['value'],
                    'label' => $item['label'],
                ];
                if(isset($item['selected'])){
                    $this->selected = $item['value'];
                }
            }
        }
        $this->handleRules();
        if($this->js_script){
            Yii::$app->controller->getView()->registerJs($this->js_script);
        }
    }

    public function handleRules(){
        if(isset($this->structure['isReadOnly']) && $this->structure['isReadOnly']){
            self::$model->{$this->name} = $this->selected;
            $this->is_disabled = true;
        }
    }
}