<?php
/**
 * Created by PhpStorm.
 * User: slava
 * Date: 18.04.17
 * Time: 11:07
 */

namespace app\widgets;

use yii\helpers\Html;
use yii\widgets\InputWidget;


class ButtonGroupInput extends InputWidget
{
    /**
     * @inheritdoc
     */
    public $items = [];

    /**
     * @inheritdoc
     */
    public $type = 'default';

    /**
     * @inheritdoc
     */
    public $size = 'default';

    /**
     * @var bool
     */
    public $radio = true;

    /**
     * @inheritdoc
     */
    public function run()
    {
        $options = [
            'class' => 'btn-group',
            'data-toggle' => 'buttons',
            'item' => [$this, 'renderItem'],
            'style' => 'width: 100%;'
        ];

        if ($this->name) {
            $type = $this->radio ? 'radioList' : 'checkboxList';

            $result = Html::$type(
                $this->name,
                $this->value,
                $this->items,
                $options
            );

        } else {
            $type = $this->radio ? 'activeRadioList' : 'activeCheckboxList';

            $result = Html::$type(
                $this->model,
                $this->attribute,
                $this->items,
                $options
            );
        }

        return $result;
    }

    /**
     * @param int $index
     * @param string $label
     * @param string $name
     * @param bool $checked
     * @param int|string $value
     * @return string
     */
    public function renderItem($index, $label, $name, $checked, $value)
    {
        $input = $this->radio ? 'radio' : 'checkbox';

        return Html::$input(
            $name,
            $checked,
            [
                'value' => $value,
                'label' => $label,
                'container' => false,
                'labelOptions' => [
                    'class' => 'btn btn-' . $this->type . ' btn-' . $this->size . ($checked ? ' active' : ''),
                    'style' => 'width:' . (100 / count($this->items)) . '%',
                ],
            ]
        );
    }
}