<?php

namespace GriffinForms\Admin\App\FormControls;

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

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

        if (isset($this->field['min'])) {
            $attrs['min'] = (int) $this->field['min'];
        }

        if (isset($this->field['max'])) {
            $attrs['max'] = (int) $this->field['max'];
        }

        $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;
    }
}
