<?php

namespace GriffinForms\Admin\App\FormControls;

class Text extends Format
{
    protected function getInputType(): string
    {
        return $this->field['type'] ?? 'text';
    }

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

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

        $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 .= '<input' . $this->renderAttributes($attrs) . ' />';
        $html .= $this->renderDescription();
        $html .= '</div>';

        return $html;
    }
}
