{% extends 'form_div_layout.html.twig' %}

{%- block form_widget_simple -%}
    {%- set type = type|default('text') -%}
    {%- set isFile = 'file' == type -%} {# Custom file input #}

    {%- if isFile -%}
        <div class="fakefile">
            {% set attr = attr|merge({class: 'fakefile--file ' ~ attr.class ?? ''}) %}
    {%- endif -%}
    
    <input type="{{ type }}" {{ block('widget_attributes') }} {% if value is not empty %}value="{{ value }}" {% endif %}/>
    
    {%- if isFile -%}
            <input class="fakefile--value" disabled type="text"/>
            <button class="fakefile--button btn btn-small btn-folder">{{'action.browse.label'|trans({}, 'action') }}</button>
        </div>
    {%- endif -%}
{%- endblock form_widget_simple -%}

{%- block form_widget_compound -%}
    <div {{ block('widget_container_attributes') }}>
        {%- if form is rootform -%}
            {{ form_errors(form) }}
        {%- endif -%}
        {{- block('form_rows') -}}
        {{- form_rest(form) -}}
    </div>
{%- endblock form_widget_compound -%}

{# Added the .form-errors class on the <ul> #}
{%- block form_errors -%}
    {%- if errors|length > 0 -%}
        <ul class="form-row-field-errors">
            {%- for error in errors -%}
                <li class="error">{{ error.message }}</li>
            {%- endfor -%}
        </ul>
    {%- endif -%}
{%- endblock form_errors -%}

{% block form_row %}
    {% spaceless %}
        <div class="form-row form-row-field-{{ form.vars.name }}{% if form.vars.errors|length > 0 %} form-row-errors{% endif %} delete-element-container">
            {% if form.vars.label|length > 0 %}
                <div class="form-row-label">
                    {{ form_label(form) }}
                </div>
            {% elseif form.parent.vars.prototype is defined %}
                <div class="form-row-label form-collection-remove--wrapper">
                    {%- import 'Admin/Form/macros.html.twig' as m -%}
                    {{ m.collection_delete(label_delete ?? '') }}
                </div>
            {% endif %}

            <div class="form-row-field">
                {{ form_widget(form) }}

                {% if help is defined and help is not null %}
                    <p class="form-row-field-help">
                        {{ include('Front/_component/icon.html.twig', {id:'info', title:'Aide'}) }}
                        {% if help is defined and help is not null %}
                            <span>{{ help|raw }}</span>
                        {% endif %}
                    </p>
                {% endif %}
                {{ form_errors(form) }}
            </div>
        </div>
    {% endspaceless %}
{% endblock form_row %}

{#
 # Add the * after required labels, only if the the field does not contains the "checked attribute" (in that case, the parent label only will have this character)
 #}
{% block form_label -%}
    {%- if label is not same as(false) -%}
        {% if not compound -%}
            {% set label_attr = label_attr|merge({'for': id}) %}
        {%- endif %}
        {% if required -%}
            {%
                set label_attr = label_attr|merge({'class': (label_attr.class|default('') ~ ' required')|trim})
            %}
        {%- endif %}
        {% if label is empty -%}
            {%- if label_format is not empty -%}
                {% set label = label_format|replace({
                    '%name%': name,
                    '%id%': id,
                }) %}
            {%- else -%}
                {% set label = name|humanize %}
            {%- endif -%}
        {%- endif -%}

        <label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>
            {{ label|trans({}, translation_domain)|raw }}{% if checked is not defined and required %}<abbr class="required-help" title="{{ 'sentence.form.fields.required'|trans({}, 'sentence') }}">*</abbr>{% endif %}
        </label>
    {%- endif -%}
{%- endblock form_label %}

{%- block collection_widget -%}
    <div class="form-collection--wrapper">
        <div class="form-collection" data-prototype="{{ prototype is defined ? form_row(prototype)|e('html') : '' }}">
            {{- block('form_widget') -}}
        </div>
        {% import 'Admin/Form/macros.html.twig' as m %}
        {{ m.collection_add(label_add ?? '') }}
    </div>
{%- endblock collection_widget -%}

{% block file_widget %}
    {% spaceless %}

    {% if image_path is defined and image_path is not null %}
        <div class="contenerInputFile-extras">
            {% set absoluteUrl = (image_filter is defined) ? image_path|imagine_filter(image_filter) : asset(image_path) %}
            {% if 'image_input' in attr.class %}
                <p>
                    <img alt="" src="{{ absoluteUrl }}" style="max-width: 100%;" />
                </p>
                <p>
                    <input id="display-{{ form.vars.id }}" name="remove_picture" type="checkbox" />
                    <span class="checkbox"></span> {# See block checkbox_widget #}
                    <label for="display-{{ form.vars.id }}">Supprimer le fichier</label>
                </p>
            {% else %}
                <p>
                    <a href="{{ absoluteUrl }}">Télécharger le fichier</a>
                </p>
                <p>
                    <input id="display-{{ form.vars.id }}" name="remove_document" type="checkbox" />
                    <span class="checkbox"></span> {# See block checkbox_widget #}
                    <label for="display-{{ form.vars.id }}">Supprimer le fichier</label>
                </p>
            {% endif %}
        </div>
    {% endif %}
    {{ block('form_widget') }}

    {% endspaceless %}
{% endblock file_widget %}

{# Added the <div> tags #}
{%- block submit_widget -%}
    {% spaceless %}

    {%- set type = type|default('submit') -%}
    <div class="form-row form-row-field-{{ form.vars.name }} form-row-submit">
        <div class="form-row-field">
            <div>{{ block('button_widget') }}</div>
        </div>
    </div>

    {% endspaceless %}
{%- endblock submit_widget -%}

{# Removed the <div></div> around the widget #}
{%- block button_row -%}
    {% spaceless %}

    {{- form_widget(form) -}}

    {% endspaceless %}
{%- endblock button_row -%}

{# Add an additional span.checkbox for custom checkbox #}
{%- block checkbox_widget -%}
    <input type="checkbox" {{ block('widget_attributes') }}{% if value is defined %} value="{{ value }}"{% endif %}{% if checked %} checked="checked"{% endif %} />
    <span class="checkbox"></span>
{%- endblock checkbox_widget -%}

{# Add an additional span.radio for custom radio #}
{%- block radio_widget -%}
    <input type="radio" {{ block('widget_attributes') }}{% if value is defined %} value="{{ value }}"{% endif %}{% if checked %} checked="checked"{% endif %} />
    <span class="radio"></span>
{%- endblock radio_widget -%}

{# Add a <div> around each wiget / label couple #}
{%- block choice_widget_expanded -%}
    <div {{ block('widget_container_attributes') }}>
    {%- for child in form %}
        <div class="form-row-field-child">
            {{- form_widget(child) -}}
            {{- form_label(child, null, {translation_domain: choice_translation_domain}) -}}
        </div>
    {% endfor -%}
    </div>
{%- endblock choice_widget_expanded -%}

{#
 # Add the aria-required="true" attribute if necessary.
 #}
{%- block widget_attributes -%}
    id="{{ id }}" name="{{ full_name }}"
    {%- if disabled %} disabled="disabled"{% endif -%}
    {%- if required %} required="required" aria-required="true"{% endif -%}
    {{ block('attributes') }}
{%- endblock widget_attributes -%}