<?php

namespace Application\Service\Extension\HelperV2;

use Application\Service\Extension\Helper\ArrayHelper;

class HtmlStruct
{
    private ?\Closure $_options = null;

    public function setOptions(array $options)
    {
        $this->_options = function () use ($options) {
            $val = $options;
            $this->_options = null;
            return $val;
        };
        return $this;
    }

    private function _render(array $html)
    {
        if ($this->_options) {
            return ArrayHelper::merge($html, ($this->_options)());
        }

        return $html;
    }

    public function input(string $label, string $prop, $isRequired = false)
    {
        return $this->_render([
            'type' => 'Input',
            'label' => $label,
            'placeholder' => "请输入{$label}",
            'prop' => $prop,
            'rules' => $isRequired ? [
                ['required' => true, 'message' => "{$label}不能为空"]
            ] : [],
        ]);
    }

    public function radio(string $label, string $prop, $isRequired = false)
    {
        return $this->_render([
            'type' => 'Radio',
            'label' => $label,
            'prop' => $prop,
            'rules' => $isRequired ? [
                ['required' => true, 'message' => "{$label}不能为空"]
            ] : [],
        ]);
    }

    public function select(string $label, string $prop, $isRequired = false)
    {
        return $this->_render([
            'type' => 'Select',
            'label' => $label,
            'prop' => $prop,
            'rules' => $isRequired ? [
                ['required' => true, 'message' => "请选择{$label}"]
            ] : [],
        ]);
    }
}