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

{# Voir https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig #}

{# On commence par simplement ajouter le form-group au row de nos formulaires #}
{% block form_row -%}
    <div class="form-group {{ errors|length > 0 ? 'has-error' : '' }} mb-3">
      {{- form_label(form) }}
      {{- form_widget(form) }}
      {{ form_errors(form) }}
    </div>
{%- endblock form_row %}

{# Puis on modifie très simplement nos input et textarea
les plus importants pour y ajouter le class imposée par Bootstrap 3 #}
{% block textarea_widget %}
  {% set attr = attr|merge({'class': attr.class|default('') ~ ' form-control'}) %}
  {{ parent() }}
{% endblock textarea_widget %}

{% block form_widget_simple %}
  {% set attr = attr|merge({'class': attr.class|default('') ~ ' form-control'}) %}
  {{ parent() }}
{% endblock form_widget_simple %}

{% block form_label -%}
  {% set label_attr = label_attr|merge({'class': (label_attr.class|default('') ~ ' control-label')|trim}) %}
  {% if 'checkbox' not in block_prefixes %}
    {% 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 -%}
         {% set label = name|humanize %}
      {%- endif -%}
      <label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>
      {{ label|trans({}, translation_domain)|raw }}
      <span class="mandatory">{% if required %}*{% endif %}</span>
      </label>
    {%- endif %}
  {% endif %}
{%- endblock form_label %}

{# et enfin les erreurs #}
{% block form_errors %}
    {% if errors|length > 0 %}
      {% if attr.help is defined and attr.help %}
        <p class="help-block text-danger">
          {% for error in errors %}
            {{ error.message }}<br />
          {% endfor %}
        </p>
      {% else %}
        <div class="alert alert-danger alert-dismissible">
          <button type="button" class="close" data-dismiss="alert">
            <span aria-hidden="true">&times;</span>
            <span class="sr-only">Close</span>
          </button>
          {% for error in errors %}
            {{ error.message|raw }}<br />
          {% endfor %}
        </div>
      {% endif %}
    {% endif %}
{% endblock form_errors %}

{# Personnalisation des boutons #}
{% block button_widget -%}
  {% if label is empty -%}
  {% set label = name|humanize %}
  {%- endif -%}
  {% set attr = attr|merge({'class': (attr.class|default('') ~ '')|trim}) %}
  <button type="{{ type|default('button') }}" {{ block('button_attributes') }}>{{
      label|trans({}, translation_domain) }}
      {% if type is defined and type == 'submit' -%}
        
      {% endif %}
  </button>
{%- endblock button_widget %}

{# Personnalisation des attributs des boutons #}
{% block button_attributes -%}
  {% if type is defined and type == 'submit' -%}
    {% set class = 'btn-primary' %}
  {% else %}
    {% set class = 'btn-secondary' %}
  {%- endif -%}
  {% set attr = attr|merge({'class': (attr.class|default('') ~ ' btn ' ~ class)|trim}) %}
  {{ parent() }}
{%- endblock button_attributes %}


{# Personnalisation des select #}
{% block choice_widget_collapsed %}
    {% set attr = attr|merge({'class': (attr.class|default('') ~ ' form-control')|trim}) %}
    {{ parent() }}
{%- endblock choice_widget_collapsed %}

{% block choice_widget %}
    {% if expanded %}
        <ul {{ block('widget_container_attributes') }} style="list-style: none; padding-left: 0">
        {% for child in form %}
            <li>
                {{ form_widget(child) }}
                {{ form_label(child) }}
            </li>
        {% endfor %}
        </ul>
    {% else %}
        {{ parent() }}
    {% endif %}
{% endblock choice_widget %}

{% block checkbox_widget %}
  <label for="{{ id }}">
  <input type="checkbox" {{ block('widget_attributes') }}{% if value is defined %} value="{{ value }}"{% endif %}{% if checked %} checked="checked"{% endif %} />
  {{ label|trans({}, translation_domain) }}</label>
{% endblock checkbox_widget %}

{% block radio_widget %}
  <label  for="{{ id }}">
    <input type="radio" {{ block('widget_attributes') }}{% if value is defined %} value="{{ value }}"{% endif %}{% if checked %} checked="checked"{% endif %} />
    {{ label|trans({}, translation_domain) }}
  </label>&nbsp;&nbsp;
{% endblock radio_widget %}

{# Inline date marcro #}
{% macro date_form_widget(form) %}
    <div class="col-xs-4">
      {{ form_widget(form) }}
    </div>
{% endmacro %}

{# Inline date #}
{% block date_widget %}
    {% if widget == 'single_text' %}
        {{ block('form_widget_simple') }}
    {% else %}
        {% import _self as self %}
        <div class="row">
          {{ date_pattern|replace({
            '{{ year }}':  self.date_form_widget(form.year),
            '{{ month }}': self.date_form_widget(form.month),
            '{{ day }}':   self.date_form_widget(form.day),
          })|raw }}       
        </div>
    {% endif %}
{% endblock date_widget %}


{% block file_widget %}
    {% set type = type|default('file') %}
    <input type="{{ type }}" {{ block('widget_attributes') }} />
{% endblock file_widget %}


