{#
 * Copyright (C) 2014-2015, Champs Libres Cooperative SCRLFS,
 <info@champs-libres.coop> / <http://www.champs-libres.coop>
 *
 * This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
#}

{% block form_row %}
{% apply spaceless %}
    <div class="mb-2">
        <div class="row">
            {% if form.vars.fullWidth is not defined or form.vars.fullWidth == false %}
                <div class="{% apply spaceless %}
                    {% if attr.class is defined and ('cf-title' in attr.class or 'cf-fields' in attr.class ) %}
                        col-sm-12
                    {% elseif attr.class is defined and 'multiple-cf-inline' in attr.class %}
                        col-sm-2 col-md-4 clear
                    {% else %}
                        col-sm-4 clear
                    {% endif %}
                {% endapply %}">
                {% if attr.class is not defined or ('cf-title' not in attr.class and 'cf-fields' not in attr.class ) %}
                    {{ form_label(form) }}
                {% endif %}
                </div>
                <div class="{% apply spaceless %}
                    {% if attr.class is defined and 'cf-title' in attr.class  %}
                        col-sm-12
                    {% elseif attr.class is defined and 'cf-fields' in attr.class %}
                        col-sm-12 parent
                    {% elseif attr.class is defined and 'multiple-cf-inline' in attr.class %}
                        col-sm-2 col-md-8 multiple-cf-inline
                    {% else %}
                        col-sm-8
                    {% endif %}
                {% endapply %}">
                    {{ form_widget(form) }}
                    {{ form_errors(form) }}
                </div>
            {% else %}
                <div class="col-12 clear">{{ form_label(form) }}</div>
                <div class="col-12">{{ form_widget(form) }}</div>
            {% endif %}
        </div>
    </div>
{% endapply %}
{% endblock form_row %}
{#
    The block 'form_row' above may be removed !
    Read this note: https://gitlab.com/Chill-Projet/chill-bundles/-/merge_requests/502#note_1311993084
#}

{% block choice_widget_expanded %}
{% apply spaceless %}
    <div {{ block('widget_container_attributes') }} class="choice-widget-expanded">
    {% for child in form %}
        <span class="inline-choice">
            {{ form_widget(child) }}
            {{ form_label(child, '', {'label_attr': { 'class' : 'inline'} }) }}
        </span>
    {% endfor %}
    </div>
    <script type="text/javascript">
        document.addEventListener('load', function(e) {
            chill.checkNullValuesInChoices("{{ form.vars.full_name }}");
        });
    </script>
{% endapply %}
{% endblock choice_widget_expanded %}


{% block  choice_with_other_widget %}
{% apply spaceless %}
    {% for child in form._choices %}
        {%- if child.vars.value != '_other'  -%}
            <div class="inline-choice">
                {{ form_widget(child) }}
                {{ form_label(child, '', {'label_attr': { 'class' : 'inline'} }) }}
            </div>
        {%- else -%}
            <div class="container-fluid">
                <div class="row g-0">
                    <div class="col-md-6">
                        <span class="inline-choice">
                            {{ form_widget(child) }}
                            {{ form_label(child, '', {'label_attr': { 'class' : 'inline'} }) }}
                        </span>
                    </div>
                    <div class="col-md-6">
                        {{ form_widget(form._other, {'attr': {'class': 'input-text-other-value'} }) }}
                    </div>
                </div>
            </div>
        {%- endif -%}
    {% endfor %}
    <script type="text/javascript">
        document.addEventListener('load', function(e) {
            chill.checkNullValuesInChoices("{{ form._choices.vars.full_name }}");
        });
    </script>
{% endapply %}
{% endblock  choice_with_other_widget %}

{% block money_widget %}
    <div class="chill-form-money">
        {{ block('form_widget_simple') }}
        <span class="chill-form-money__money">{{ money_pattern|form_encode_currency }}</span>
    </div>
{% endblock money_widget %}

{%- block time_widget -%}
    {%- if widget == 'single_text' -%}
        {{ block('form_widget_simple') }}
    {%- else -%}
        {%- set vars = widget == 'text' ? { 'attr': { 'size': 1 }} : {} -%}
        {% set attr = {'class' : 'time_compound ' ~ attr.class|default('') } %}
        <div {{ block('widget_container_attributes') }}>
            {{ form_widget(form.hour, vars) }}{% if with_minutes %}<span class="separator">:</span>{{ form_widget(form.minute, vars) }}{% endif %}{% if with_seconds %}<span class="separator">:</span>{{ form_widget(form.second, vars) }}{% endif %}
        </div>
    {%- endif -%}
{%- endblock time_widget -%}

{% block form_errors %}
{% apply spaceless %}
    {% if errors|length > 0 %}
    <ul class="errors chill-form__errors">
        {% for error in errors %}
            <li class="chill-form_errors__entry {% if 'severity' in error.cause.constraint.payload|keys %}chill-form__errors__entry--{{ error.cause.constraint.payload.severity }}{% endif %}">{{ error.message }}</li>
        {% endfor %}
    </ul>
    {% endif %}
{% endapply %}
{% endblock form_errors %}

{% block _formatter__aggregator_placement_csv_formatter_row %}
    <h3>{{ form_label(form) }}</h3>

    {{ form_row(form.order) }}

    {{ form_row(form.position) }}

{% endblock %}

{% block _formatter__aggregator_placement_spreadsheet_formatter_row %}
    <h3>{{ form_label(form) }}</h3>

    {{ form_row(form.order) }}

{% endblock %}

{% block chill_collection_widget %}
    <div class="chill-collection">
        <ul class="list-entry"
            {{ form.vars.js_caller }}="{{ form.vars.js_caller }}"
            data-uniqid="{{ form.vars.uniqid|escape('html_attr') }}"
            data-collection-name="{{ form.vars.name|escape('html_attr') }}"
            data-collection-identifier="{{ form.vars.identifier|escape('html_attr') }}"
            data-collection-button-remove-label="{{ form.vars.button_remove_label|trans|e }}"
            data-collection-allow-add="{{ form.vars.allow_add|escape('html_attr') }}"
            data-collection-allow-delete="{{ form.vars.allow_delete|escape('html_attr') }}">
            {% for entry in form %}
                <li class="entry" data-collection-is-persisted="1">
                    <div>
                        {{ form_widget(entry) }}
                        {{ form_errors(entry) }}
                    </div>
                </li>
            {% else %}
                <li data-collection-empty-explain="1">
                    <span class="chill-no-data-statement">{{ form.vars.empty_collection_explain|default('No entities')|trans }}</span>
                </li>
            {% endfor %}
        </ul>

        {% if form.vars.allow_add == 1 %}
        <button class="add-entry btn btn-misc"
            type="button"
            data-uniqid="{{ form.vars.uniqid|escape('html_attr') }}"
            data-collection-add-target="{{ form.vars.name|escape('html_attr') }}"
            data-form-prototype="{{ ('<div>' ~ form_widget(form.vars.prototype) ~ '</div>')|escape('html_attr') }}" >
            <i class="fa fa-plus fa-fw"></i>
            {{ form.vars.button_add_label|trans }}
        </button>
        {% endif %}

    </div>
{% endblock %}

{% block address_widget %}
    {% for entry in form %}
        {% if entry.vars.name != 'customs' %}
            {{ form_row(entry) }}
        {% else %}
            {{ form_widget(entry) }}
        {% endif %}
    {% endfor %}
{% endblock %}


{% block private_comment_row %}
    {{ form_row(form) }}
{% endblock %}

{% block private_comment_widget %}
    {% for entry in form %}
        {{ form_widget(entry) }}
    {% endfor %}
{% endblock %}

{% block comment_row %}
    {{ form_row(form) }}
{% endblock %}

{% block comment_widget %}
    {% for entry in form %}
        {{ form_widget(entry) }}
    {% endfor %}
{% endblock comment_widget %}

{% block pick_center_widget %}
    {{ form_widget(form.center) }}
{% endblock pick_center_widget %}

{% block pick_center_row %}
    {% if (not form.vars.is_hidden) %}
        {{ block('form_row') }}
    {% else %}
        {{ form_widget(form.center) }}
    {% endif %}
{% endblock %}

{% block pick_entity_dynamic_row %}
    <div class="row">
        <div class="col-md-12">
            {{ form_label(form) }}
            {{ form_help(form) }}
        </div>
    </div>
    <div class="row justify-content-end">
        <div class="col-md-7 col-sm-12">
            {{ form_widget(form) }}
        </div>
    </div>
{% endblock %}

{% block pick_entity_dynamic_widget %}
    <input type="hidden" {{ block('widget_attributes') }} {% if value is not empty %}value="{{ value|escape('html_attr') }}" {% endif %} data-input-uniqid="{{ form.vars['uniqid'] }}"/>
    <div data-module="pick-dynamic"
         data-types="{{ form.vars['types']|json_encode }}"
         data-multiple="{{ form.vars['multiple'] }}"
         data-uniqid="{{ form.vars['uniqid'] }}"
         data-suggested="{{ form.vars['suggested']|json_encode|escape('html_attr') }}"
         data-as-id="{{ form.vars['as_id'] }}"
         data-submit-on-adding-new-entity="{{ form.vars['submit_on_adding_new_entity'] }}"
         data-label="{{ form.vars['label']|trans|escape('html_attr') }}"></div>
{% endblock %}

{% block pick_linked_entities_row %}
    <div class="row">
        <div class="col-md-12">
            {{ form_label(form) }}
            {{ form_help(form) }}
        </div>
    </div>
    <div class="row justify-content-end">
        <div class="col-md-7 col-sm-12">
            {{ form_widget(form) }}
        </div>
    </div>
{% endblock %}


{% block pick_linked_entities_widget %}
    <input type="hidden" {{ block('widget_attributes') }}
        {% if value is not empty %}value="{{ value|escape('html_attr') }}" {% endif %}
       data-input-uniqid="{{ form.vars['uniqid'] }}"/>
    <div
        data-input-uniqid="{{ form.vars['uniqid'] }}"
        data-module="pick-linked-entities"
        data-pick-entities-type="{{ form.vars['pick-entities-type'] }}"
         data-suggested="{{ form.vars['suggested']|json_encode|escape('html_attr') }}"
    ></div>
{% endblock %}

{% block pick_postal_code_widget %}
    {{ form_help(form)}}
    <input type="hidden" {{ block('widget_attributes') }} {% if value is not empty %}value="{{ value }}" {% endif %} data-input-uniqid="{{ form.vars['uniqid'] }}"/>
    <div data-module="pick-postal-code"  data-uniqid="{{ form.vars['uniqid'] }}"></div>
{% endblock %}

{% block pick_rolling_date_widget %}
    <div data-rolling-date="{{ form.vars['uniqid'] }}" class="row">
        <div class="roll-wrapper">
            {{ form_widget(form.roll, { 'attr': { 'data-roll-picker': 'data-roll-picker'}}) }}
            {{ form_errors(form.roll) }}
        </div>
        <div class="fixed-wrapper">
            {{ form_widget(form.fixedDate) }}
            {{ form_errors(form.fixedDate) }}
        </div>
    </div>
{% endblock %}

{% block chill_datetime_label %}
    <label for="{{ form.date.vars.id }}" class="col-form-label col-sm-4 required">
        {{ "Date"|trans }}
    </label>
{% endblock %}

{% block chill_datetime_widget %}
    <div class="col-sm-8 d-flex align-items-start gap-2">
        {#date#}
        {{ form_widget(form.date, {
            attr: { class: 'form-control', style: 'flex: 1 1 auto;' }
        }) }}
        {#time#}
        {{ form_widget(form.time, {
            attr: {
                class: 'form-select',
                style: 'flex: 0 0 200px; max-width: 200px; white-space: nowrap; padding:0;'
            }
        }) }}
    </div>
{% endblock %}

{% block chill_datetime_row %}
    <div class="mb-3 row">
        {{ block('chill_datetime_label') }}
        {{ block('chill_datetime_widget') }}
    </div>
{% endblock %}
