<?php

namespace GriffinForms\Admin\App\FormControls;

class Textarea extends Format
{
    protected function getPlaceholder(): string
    {
        return $this->field['placeholder'] ?? '';
    }

    protected function getRows(): int
    {
        return max(3, (int) ($this->field['rows'] ?? 4));
    }

    public function render(): string
    {
        $attrs = array_merge($this->getInputAttributes(), [
            'id' => $this->getHtmlId(),
            'name' => $this->getFieldName(),
            'class' => $this->getInputClasses(),
            'rows' => $this->getRows(),
        ]);

        $placeholder = $this->getPlaceholder();
        if ($placeholder !== '') {
            $attrs['placeholder'] = $placeholder;
        }

        if ($this->isRequired()) {
            $attrs['required'] = true;
        }

        $html  = '<div class="' . esc_attr($this->getContainerClasses()) . '">';
        $html .= $this->renderLabel();
        $html .= '<textarea' . $this->renderAttributes($attrs) . '>';
        $html .= esc_textarea((string) ($this->field_value ?? ''));
        $html .= '</textarea>';
        $html .= $this->renderDescription();
        $html .= '</div>';

        return $html;
    }
}
