<?php

namespace Plainwork\Ui\Component\Form;

use Plainwork\Http\Request;
use Plainwork\Ui\Component\Form\AbstractInput;
use Plainwork\Ui\Html;

class ButtonInput extends AbstractInput
{
    private $label;
    private bool $value = false;

    public function grab(Request $request): bool
    {
        $postedValue = $request->getPostParam($this->getName());
        $ret = (null === $postedValue) ? false : true;

        return $ret;
    }

    public function setValue(bool $v): self
    {
        $this->value = $v;

        return $this;
    }

    public function getValue(): bool
    {
        return $this->value;
    }

    public function getLabel(): array
    {
        return $this->label;
    }

    public function setLabel($label): self
    {
        $this->label = $label;

        return $this;
    }

    public function render(self $el, Request $request)
    {
        $html = new Html();

        $name = $el->getName();
        $label = (isset($el->label)) ? $el->label : $name;

        $ret = $html->el('button', $label);

        $ret->withAttr('name', $name);
        $ret->withAttr('value', 1);

        $attr = $el->getAttr();
        foreach ($attr as $k => $v) {
            $ret->withAttr($k, $v);
        }

        return $ret;
    }
}
