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

{# ---------- FORM START / END ---------- #}
{% block form_start -%}
    {{ parent() }}
{%- endblock %}
{% block form_end -%}
    {{ parent() }}
{%- endblock %}

{# ---------- ROW ---------- #}
{% block form_row %}
    <div class="mb-5">
        {{ form_label(form) }}
        <div class="mt-1">
            {{ form_widget(form) }}
        </div>
        {% if not compound and not form.vars.valid %}
            {# Affiche l'erreur en bas du champ simple #}
            <p class="text-sm text-red-600 mt-1">{{ form_errors(form) }}</p>
        {% else %}
            {# Affiche l'erreur pour les champs composés (si form_errors n'est pas déjà dans le widget) #}
            {{ form_errors(form) }}
        {% endif %}
    </div>
{% endblock %}

{# ---------- LABEL ---------- #}
{% block form_label %}

    {% if label is not same as(false) %}
        <label for="{{ id }}" class="block text-sm font-medium text-gray-700">
            {{ label|trans({}, translation_domain) }}
            {% if required %}
                <span class="text-red-500">*</span>
            {% endif %}
        </label>
    {% endif %}
{% endblock %}

{# ---------- ERRORS ---------- #}
{% block form_errors %}
    {% if errors|length > 0 %}
        <ul class="mt-1 text-sm text-red-600 list-disc list-inside">
            {% for error in errors %}
                <li>{{ error.message }}</li>
            {% endfor %}
        </ul>
    {% endif %}
{% endblock %}

{# ---------- WIDGET DISPATCH ---------- #}
{% block form_widget %}
    {% if compound %}
        {{ block('form_widget_compound') }}
    {% else %}
        {{ block('form_widget_simple') }}
    {% endif %}
{% endblock %}

{# --- STYLE COMMUN POUR WIDGETS (Light Mode) --- #}

{# ---------- SIMPLE INPUTS (text, email, number...) ---------- #}
{% block form_widget_simple %}
    {% set type = type|default('text') %}
    <input
        type="{{ type }}"
        {{ block('widget_attributes') }}
        value="{{ value }}"
        class="form-input mt-1 block w-full px-3 py-2 bg-white border border-gray-300 text-gray-900 placeholder-gray-400 rounded-md shadow-sm focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm transition duration-150"
    />
{% endblock %}

{# ---------- TEXTAREA ---------- #}
{% block textarea_widget %}
    <textarea
        {{ block('widget_attributes') }}
        class="form-textarea form-input mt-1 block w-full px-3 py-2 bg-white border border-gray-300 text-gray-900 placeholder-gray-400 rounded-md shadow-sm focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm transition duration-150"
    >{{ value }}</textarea>
{% endblock %}

{# ---------- SELECT ---------- #}
{% block choice_widget_collapsed %}
    <select
        {{ block('widget_attributes') }}
        class="form-select form-input mt-1 block w-full px-3 py-2 bg-white border border-gray-300 text-gray-900 placeholder-gray-400 rounded-md shadow-sm focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm transition duration-150"
    >
        {% if placeholder is not none %}
            <option value="" {% if required and value is empty %}selected{% endif %}>
                {{ placeholder != '' ? (placeholder|trans({}, translation_domain)) : '' }}
            </option>
        {% endif %}
        {% for group_label, choice in choices %}
            {% if choice is iterable %}
                <optgroup label="{{ group_label|trans({}, translation_domain) }}">
                    {% for nested_choice in choice %}
                        <option value="{{ nested_choice.value }}" {% if nested_choice is selectedchoice(value) %}selected{% endif %}>
                            {{ nested_choice.label|trans({}, translation_domain) }}
                        </option>
                    {% endfor %}
                </optgroup>
            {% else %}
                <option value="{{ choice.value }}" {% if choice is selectedchoice(value) %}selected{% endif %}>
                    {{ choice.label|trans({}, translation_domain) }}
                </option>
            {% endif %}
        {% endfor %}
    </select>
{% endblock %}

{# ---------- CHECKBOX ---------- #}
{% block checkbox_widget %}
    <div class="flex items-center">
        <input type="checkbox"
            {{ block('widget_attributes') }}
            {% if value not in ['', null] %} value="{{ value }}"{% endif %}
            {% if checked %}checked="checked"{% endif %}
               class="form-checkbox h-5 w-5 text-indigo-600 border-gray-300 rounded focus:ring-indigo-500">
    </div>
{% endblock %}

{# ---------- RADIO ---------- #}
{% block radio_widget %}
    <input type="radio"
        {{ block('widget_attributes') }}
           value="{{ value }}"
        {% if checked %}checked="checked"{% endif %}
           class="form-radio h-5 w-5 text-indigo-600 border-gray-300 focus:ring-indigo-500">
{% endblock %}

{# ---------- FILE ---------- #}
{% block file_widget %}
    <input type="file"
        {{ block('widget_attributes') }}
           class="block w-full text-sm text-gray-800 file:mr-4 file:py-2 file:px-4
                  file:rounded-md file:border-0
                  file:text-sm file:font-semibold
                  file:bg-indigo-600 file:text-white
                  hover:file:bg-indigo-700
                  bg-white border border-gray-300 rounded-md shadow-sm">
{% endblock %}
