<?php
namespace App\Components;

use Core\Component;

class FormBuilder extends Component
{
    protected string $title = '';
    protected string $description = '';
    protected array $fields = [];
    protected array $data = []; // New property to store form data

    public static function make(string $id = 'form'): self
    {
        $instance = new self();
        $instance->id = $id;
        return $instance;
    }

    public function title(string $title): self
    {
        $this->title = $title;
        return $this;
    }

    public function description(string $text): self
    {
        $this->description = $text;
        return $this;
    }

    public function addField(Field $field): self
    {
        $this->fields[] = $field->toArray();
        return $this;
    }

    // New method to set form data
    public function data(array $data): self
    {
        $this->data = $data;
        return $this;
    }

    public function render(): string
    {
        $html = <<<HTML
            <form id="{$this->id}">
        HTML;

        foreach ($this->fields as $field) {
            $html .= $this->renderField($field);
        }

        $html .= <<<HTML
            </form>
        HTML;

        return $html;
    }

    protected function renderField(array $field): string
    {
        $label = $field['label'];
        $name = $field['name'];
        $type = $field['type'];
        $options = $field['options'];
        
        // Retrieve the value from the FormBuilder's data array
        $value = $this->data[$name] ?? ($options['value'] ?? '');

        switch ($type) {
            case 'textarea':
                $rows = $options['rows'] ?? 5;
                return <<<HTML
                    <div class="mb-3">
                        <label for="{$name}" class="form-label">{$label}</label>
                        <textarea class="form-control" id="{$name}" name="{$name}" rows="{$rows}">{$value}</textarea>
                    </div>
                HTML;

            case 'select':
                $opts = '';
                foreach ($options['choices'] ?? [] as $val => $text) {
                    $selected = ($val == $value) ? 'selected' : '';
                    $opts .= "<option value='{$val}' {$selected}>{$text}</option>";
                }
                return <<<HTML
                    <div class="mb-3">
                        <label for="{$name}" class="form-label">{$label}</label>
                        <select id="{$name}" name="{$name}" class="form-select" >
                            {$opts}
                        </select>
                    </div>
                HTML;
            
            case 'submit':
                $value = $options['value'] ?? $label;
                $class = $options['class'] ?? 'btn btn-primary';
                return <<<HTML
                    <div class="mb-3">
                        <button type="submit" name="{$name}" class="{$class}">{$value}</button>
                    </div>
                HTML;

            default:
                $placeholder = $options['placeholder'] ?? '';
                $readonly = $options['readonly'] ?? '';
                $disabled = $options['disabled'] ?? '';

                return <<<HTML
                    <div class="mb-3">
                        <label for="{$name}" class="form-label">{$label}</label>
                        <input type="{$type}" id="{$name}" name="{$name}" class="form-control" 
                            placeholder="{$placeholder}" 
                            value="{$value}" 
                            {$readonly} {$disabled}>
                    </div>
                HTML;
        }
    }
}