<?php

namespace GriffinForms\Admin\App\FormControls;

class Checkbox extends Format
{
    protected function getCheckboxLabel(): string
    {
        return $this->field['checkbox_label'] ?? $this->getLabel();
    }

    public function render(): string
    {
        $attrs = array_merge($this->getInputAttributes(), [
            'type' => 'checkbox',
            'id' => $this->getHtmlId(),
            'name' => $this->getFieldName(),
            'class' => 'form-check-input gf-app-control-checkbox',
            'value' => '1',
        ]);

        if (!empty($this->field_value)) {
            $attrs['checked'] = true;
        }

        $html  = '<div class="' . esc_attr($this->getContainerClasses()) . '">';
        $html .= '<div class="form-check d-flex align-items-center">';
        $html .= '<input' . $this->renderAttributes($attrs) . ' />';
        $html .= sprintf(
            '<label class="form-check-label gf-app-checkbox-label" for="%s">%s</label>',
            esc_attr($this->getHtmlId()),
            esc_html($this->getCheckboxLabel())
        );
        $html .= '</div>';
        $html .= $this->renderDescription();
        $html .= '</div>';

        return $html;
    }
}
