{% set originalValue = originalValue is defined ? originalValue : value %}
{% set toggleableChecked = field.toggleable and (originalValue is not null and originalValue is not empty) %}
{% set isDisabledToggleable = field.toggleable and not toggleableChecked %}
{% set value = (is_object(value) or value is null ? field.default : value) %}
{% set cookie_name = 'forms-' ~ form.name ~ '-' ~ field.name %}
{% set value = (is_object(value) or value is null ? (get_cookie(cookie_name) is null ? field.default : get_cookie(cookie_name)) : value) %}
{% set errors = attribute(form.messages, field.name) %}
{% set required = client_side_validation and field.validate.required in ['on', 'true', 1] %}
{% set autofocus = (inline_errors == false) and field.autofocus in ['on', 'true', 1] %}

{% if inline_errors and errors %}
    {% set autofocus = true %}
{% endif %}

{% block field %}
    <div class="{{ form_field_outer_classes ?: 'form-field' }} {{ field.outerclasses }} {% if errors %} has-errors{% endif %} {% block outer_field_classes %}{% endblock %}">
        {% block contents %}
            {% if field.label is not same as(false) %}
                <div class="{{ form_field_outer_label_classes ?: 'form-label' }} {{ field.labelclasses }}">
                    <label class="{{ form_field_label_classes ?: 'inline' }}" {% if field.id is defined %}for="{{ field.id|e }}" {% endif %} >
                    {% block label %}
                        {% if field.help %}
                            {% if field.markdown %}
                                <span class="tooltip" data-asTooltip-position="w" title="{{ field.help|t|markdown(false)|e }}">{{ field.label|markdown(false)|default(field.name|capitalize)|t }}</span>
                            {% else %}
                                <span class="tooltip" data-asTooltip-position="w" title="{{ field.help|t|e }}">{{ field.label|default(field.name|capitalize)|t }}</span>
                            {% endif %}
                        {% else %}
                            {% if field.markdown %}
                                {{ field.label|markdown(false)|default(field.name|capitalize)|t }}
                            {% else %}
                                {{ field.label|default(field.name|capitalize)|t }}
                            {% endif %}
                        {% endif %}

                        {{ field.validate.required in ['on', 'true', 1] ? '<span class="required">*</span>' }}
                    {% endblock %}
                    </label>
                </div>
            {% endif %}
            <div class="{{ form_field_outer_data_classes ?: 'form-data' }} {{ field.dataclasses }}"
                {% block global_attributes %}
                data-grav-field="{{ field.type }}"
                data-grav-disabled="{{ originalValue is null ? 'true' : 'false' }}"
                data-grav-default="{{ field.default|json_encode()|e('html_attr') }}"
                {% endblock %}
            >
                {% block group %}
                    {% block input %}
                        <div class="{{ form_field_wrapper_classes ?: 'form-input-wrapper' }} {{ field.size }} {{ field.wrapper_classes }}">
                            {% block prepend %}{% endblock prepend %}
                            <input
                                {# required attribute structures #}
                                name="{{ (scope ~ field.name)|fieldName }}"
                                value="{{ value|join(', ')|e('html_attr') }}"
                                {# input attribute structures #}
                                {% block input_attributes %}
                                    class="{{ form_field_input_classes }} {{ field.classes }}"
                                    {% if field.id is defined %}id="{{ field.id|e }}" {% endif %}
                                    {% if field.style is defined %}style="{{ field.style|e }}" {% endif %}
                                    {% if field.disabled or isDisabledToggleable %}disabled="disabled"{% endif %}
                                    {% if field.placeholder %}placeholder="{{ field.placeholder|t }}"{% endif %}
                                    {% if autofocus %}autofocus="autofocus"{% endif %}
                                    {% if field.novalidate in ['on', 'true', 1] %}novalidate="novalidate"{% endif %}
                                    {% if field.readonly in ['on', 'true', 1] %}readonly="readonly"{% endif %}
                                    {% if field.autocomplete in ['on', 'off', 'new-password'] %}autocomplete="{{ field.autocomplete }}"{% endif %}
                                    {% if field.attributes is defined %}
                                      {% for attribute in field.attributes %}
                                          {{ attribute.name }}="{{ attribute.value|e }}"
                                      {% endfor %}
                                    {% endif %}
                                    {% if required %}required="required"{% endif %}
                                    {% if field.validate.pattern %}pattern="{{ field.validate.pattern|e }}"{% endif %}
                                    {% if field.validate.message %}title="{{ field.validate.message|t|e }}"
                                    {% elseif field.title is defined %}title="{{ field.title|t|e }}" {% endif %}
                                {% endblock %}
                            />
                            {% block append %}{% endblock append %}
                            {% if inline_errors and errors %}
                                <div class="{{ form_errors_classes ?: 'form-errors' }}">
                                    {% for error in errors %}
                                        <p class="form-message"><i class="fa fa-exclamation-circle"></i> {{ error }}</p>
                                    {% endfor %}
                                </div>
                            {% endif %}
                        </div>
                    {% endblock %}
                {% endblock %}
                {% if field.description %}
                    <div class="form-extra-wrapper {{ field.size|e }} {{ field.wrapper_classes }}">
                        <span class="form-description">
                            {% if field.markdown %}
                                {{ field.description|t|markdown(false)|raw }}
                            {% else %}
                                {{ field.description|t|raw }}
                            {% endif %}
                        </span>
                    </div>
                {% endif %}

            </div>
        {% endblock %}
    </div>
{% endblock %}
