{# @var ea \EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext #}
{% use '@EasyAdmin/crud/form_theme.html.twig' %}

{% block form_start %}
    {% if form.vars.errors|length > 0 and 'ea_crud' in form.vars.block_prefixes|default([]) %}
        {{ form_errors(form) }}
    {% endif %}

    {%- do form.setMethodRendered() -%}
    {% set method = method|upper %}
    {%- if method in ["GET", "POST"] -%}
        {% set form_method = method %}
    {%- else -%}
        {% set form_method = "POST" %}
    {%- endif -%}
<form{% if name != '' %} name="{{ name }}"{% endif %}
        method="{{ form_method|lower }}"{% if action != '' %} action="{{ action }}"{% endif %}{{ block('attributes') }}{% if multipart %} enctype="multipart/form-data"{% endif %}>
    {%- if form_method != method -%}
        <input type="hidden" name="_method" value="{{ method }}"/>
    {%- endif -%}

    <input type="hidden" name="referrer"
           value="{% if ea is defined %}{{ ea.request.query.get('referrer') }}{% endif %}">
    {% endblock form_start %}

    {% block form_row %}
        {% set row_attr = row_attr|merge({
            class: row_attr.class|default('') ~ ' form-group'
        }) %}

        <div {% with { attr: row_attr } %}{{ block('attributes') }}{% endwith %}>
            {{- form_label(form) -}}
            <div class="form-widget">

                {#
                {% set has_prepend_html = ea.field.prepend_html|default(null) is not null %}
                {% set has_append_html = ea.field.append_html|default(null) is not null %}
                {% set has_input_groups = has_prepend_html or has_append_html %}
                #}

                {% set has_prepend_html = false %}
                {% set has_append_html = false %}
                {% set has_input_groups = false %}

                {% if ea_crud_form.ea_field is defined and ea_crud_form.ea_field is not null %}
                    {% set prepend_html = ea_crud_form.ea_field.customOptions.get('prependHtml') %}
                    {% set append_html = ea_crud_form.ea_field.customOptions.get('appendHtml') %}
                    {% set has_prepend_html = prepend_html is not null %}
                    {% set has_append_html = append_html is not null %}
                    {% set has_input_groups = has_prepend_html or has_append_html %}
                {% endif %}

                {% if has_input_groups %}
                <div class="input-group">{% endif %}
                    {% if has_prepend_html %}
                        <div class="input-group-prepend">
                            <span class="input-group-text">{{ prepend_html|raw }}</span>
                        </div>
                    {% endif %}

                    {{ form_widget(form) }}

                    {% if has_append_html %}
                        <div class="input-group-append">
                            <span class="input-group-text">{{ append_html|raw }}</span>
                        </div>
                    {% endif %}
                    {% if has_input_groups %}</div>{% endif %}

                {% set nullable_fields_fqcn = [
                    'EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField',
                    'EasyCorp\Bundle\EasyAdminBundle\Field\DateField',
                    'EasyCorp\Bundle\EasyAdminBundle\Field\TimeField',
                ] %}
                {% if form.vars.ea_crud_form.ea_field.fieldFqcn|default(false) in nullable_fields_fqcn and ea.field.nullable|default(false) %}
                    <div class="nullable-control">
                        <label>
                            <input type="checkbox" {% if data is null %}checked="checked"{% endif %}>
                            {{ 'label.nullable_field'|trans({}, 'EasyAdminBundle') }}
                        </label>
                    </div>
                {% endif %}

                {% set help_message = name|sov_trans_admin_help(form.parent.vars.data) %}
                {% if help_message != '' %}
                    <small class="form-help">{{ help_message }}</small>
                {% endif %}

                {{- form_errors(form) -}}
            </div>
        </div>
    {% endblock form_row %}

    {% block form_label -%}

    {% if label is same as(false) -%}
    <label>{# the empty <label> is needed to not break the form design #}</label>
    {%- else -%}
    {%- if compound is defined and compound -%}
        {%- set element = 'legend' -%}
        {%- set label_attr = label_attr|merge({class: (label_attr.class|default('') ~ ' col-form-label')|trim}) -%}
    {%- else -%}
        {%- set label_attr = label_attr|merge({for: id, class: (label_attr.class|default('') ~ ' form-control-label')|trim}) -%}
    {%- 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 -%}

    {% set entityNameOrObject = form.parent.vars.data %}

    {% if form.parent.vars.translation_entity_name is defined %}
        {% set entityNameOrObject = form.parent.vars.translation_entity_name %}
    {% endif %}

    {#
    {% if not entityNameOrObject and form.parent.vars.errors.form.config.dataClass is defined %}
        {% set entityNameOrObject = form.parent.vars.errors.form.config.dataClass %}
    {% endif %}
    #}

    <{{ element|default('label') }}{% if label_attr %}{% with { attr: label_attr } %}{{ block('attributes') }}{% endwith %}{% endif %}
    >{{ (label is not empty and '.' in label) ? label|trans({}, translation_domain) : name|sov_trans_admin_field(entityNameOrObject) }}
</{{ element|default('label') }}>

    {%- endif -%}
{%- endblock form_label %}

{% block gallery_manager_row %}
    {{ block('collection_row') }}
{% endblock gallery_manager_row %}

{% block gallery_manager_widget %}
    {{ block('collection_widget') }}
{% endblock gallery_manager_widget %}

{% block collection_row %}
    {% if prototype is defined and not prototype.rendered %}
        {% set row_attr = row_attr|merge({ 'data-prototype': form_row(prototype) }) %}
    {% endif %}

    {% set row_attr = row_attr|merge({
        'data-entry-is-complex': form.vars.ea_crud_form.ea_field and form.vars.ea_crud_form.ea_field.customOptions.get('entryIsComplex') ? 'true' : 'false',
        'data-allow-add': allow_add ? 'true' : 'false',
        'data-allow-delete': allow_delete ? 'true' : 'false',
        'data-num-items': form.children|length,
        'data-form-type-name-placeholder': prototype is defined ? prototype.vars.name : '',
    }) %}

    {{ block('form_row') }}
{% endblock collection_row %}

{% block collection_widget %}
    {{ block('form_widget') }}
    {% if allow_add|default(false) %}
        <button type="button" class="btn btn-link field-collection-add">
            <i class="fa fa-plus pr-1"></i>
            {{ 'action.add_new_item'|trans({}, 'EasyAdminBundle') }}
        </button>
    {% endif %}
{% endblock collection_widget %}

{% block collection_entry_widget %}
    {% set is_complex = form_parent(form).vars.ea_crud_form.ea_field.customOptions.get('entryIsComplex') ?? false %}

    <div class="field-collection-item {{ is_complex ? 'field-collection-item-complex' }}">
        {% if form_parent(form).vars.allow_delete|default(false) %}
            <button type="button" class="btn btn-link field-collection-delete"
                    title="{{ 'action.remove_item'|trans({}, 'EasyAdminBundle') }}">
                <i class="fas fa-times"></i>
            </button>
        {% endif %}
        {{ form_widget(form) }}
    </div>
{% endblock collection_entry_widget %}

{% block file_manager_image_row %}
    {{ form_widget(form) }}
{% endblock file_manager_image_row %}

{% block file_manager_legend_row %}
    {{ form_widget(form) }}
{% endblock file_manager_legend_row %}

{% block file_manager_position_row %}
    {{ form_widget(form) }}
{% endblock file_manager_position_row %}


{% block file_manager_widget %}
    {% if form.vars.ea_crud_form.ea_field is not null %}
        {% set managerDir = form.vars.ea_crud_form.ea_field.customOptions.get('managerDir') %}
        {% set type = form.vars.ea_crud_form.ea_field.customOptions.get('type') %}
    {% else %}
        {% if form.vars.attr.type is defined %}
            {% set managerDir = form.vars.attr.type %}
            {% set type = form.vars.attr.type %}
        {% endif %}
    {% endif %}

    {% if type is defined %}
        <div class="lc-filemanager row">
            {% if type == 'image' %}
                <div class="col-md-3 col-xs-12 form-group">
                    <div class="lc-filemenager-preview card">
                        <div class="no-image">
                            <i class="fa fa-image"></i>
                        </div>
                        <img src="{{ form.path.vars.value }}" id="{{ form.path.vars.id }}_preview" alt=""
                             class="card-img-top">
                    </div>
                </div>
            {% else %}
                <div class="callout callout-success">
                    <h5><i class="fa fa-file-alt"></i>
                        <span id="{{ form.path.vars.id }}_preview_text">{{ form.path.vars.value }}</span>
                    </h5>
                </div>
            {% endif %}

            <div class="col-12">
                {# {{ dump(form.vars) }} #}
                {# {{ dump(form.vars.ea_crud_form.ea_field) }} #}
                <div class="input-group">
                    <div class="input-group-prepend">
                        {% if form.parent.vars['row_attr']['data-sortable'] is defined %}
                            <button type="button" class="btn btn-success lc-btn-sortable" data-toggle="tooltip"
                                    title="Trier les images">
                                <i class="fa fa-arrows-alt"></i>
                            </button>
                        {% endif %}
                        <button type="button" class="btn btn-primary lc-filemanager-open"
                                data-id="{{ form.path.vars.id }}"
                                data-toggle="tooltip" title="Sélectionner un fichier"
                                data-target="{{ path('file_manager', {module:1, conf: managerDir})|raw }}">
                            <i class="fa fa-folder-open"></i>
                        </button>
                    </div>
                    {{ form_widget(form.legend) }}
                    <div class="input-group-append">
                        <button type="button" class="btn btn-danger lc-filemanager-delete" data-toggle="tooltip"
                                title="Supprimer l'image"
                                data-id="{{ form.path.vars.id }}">
                            <i class="fa fa-trash"></i>
                        </button>
                    </div>

                    {{ form_rest(form) }}
                </div>
            </div>
        </div>
    {% endif %}
{% endblock file_manager_widget %}

{% block checkbox_radio_label -%}
    {#- Do not display the label if widget is not defined in order to prevent double label rendering              -#}
    {%- if widget is defined -%}
        {% set is_parent_custom = parent_label_class is defined and ('checkbox-custom' in parent_label_class or 'radio-custom' in parent_label_class or 'switch-custom' in parent_label_class) %}
        {% set is_custom = label_attr.class is defined and ('checkbox-custom' in label_attr.class or 'radio-custom' in label_attr.class or 'switch-custom' in label_attr.class) %}
        {%- if is_parent_custom or is_custom -%}
            {%- set label_attr = label_attr|merge({class: (label_attr.class|default('') ~ ' custom-control-label')|trim}) -%}
        {%- else %}
            {%- set label_attr = label_attr|merge({class: (label_attr.class|default('') ~ ' form-check-label')|trim}) -%}
        {%- endif %}
        {%- 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 parent_label_class is defined -%}
            {%- set label_attr = label_attr|merge({class: (label_attr.class|default('') ~ ' ' ~ parent_label_class)|replace({'checkbox-inline': '', 'radio-inline': '', 'checkbox-custom': '', 'radio-custom': ''})|trim}) -%}
        {%- endif -%}
        {%- if label is not same as(false) and 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 -%}

        {% if attr.disabled is defined and attr.disabled %}
            {%- set label_attr = label_attr|merge({class: (label_attr.class|default('') ~ ' disabled')|trim}) -%}
        {% endif %}

        <label{% with { attr: label_attr } %}{{ block('attributes') }}{% endwith %}>
            {{ widget|raw }}
            <span class="checkmark"></span>

            {# {{- label is not same as(false) ? (translation_domain is same as(false) ? label : label|trans(label_translation_parameters, translation_domain))|raw -}} #}

            {% set entityNameOrObject = form.parent.vars.data %}

            {% if form.parent.vars.translation_entity_name is defined %}
                {% set entityNameOrObject = form.parent.vars.translation_entity_name %}
            {% endif %}

            <!--            lorsque que le name est un entier "case radio"-->
            {% if name matches '/^\\d+$/' %}
                {{- label|trans({}, 'admin') -}}
            {% else %}
                {{- (label is not empty and '.' in label) ? label|trans({}, 'admin') : name|sov_trans_admin_field(entityNameOrObject) -}}
            {% endif %}
            {{- form_errors(form) -}}
        </label>
    {%- endif -%}
{%- endblock checkbox_radio_label %}

{% block ea_crud_widget_panels %}
    {% embed '@LcSov/adminlte/embed/tabs/tabs.html.twig' %}
        {# items menu #}
        {% import '@LcSov/adminlte/macro/tabs.html.twig' as mtabs %}
        {% block menu_items %}
            {% for panel_name, panel_config in ea_crud_form.form_panels|filter(panel_config => not panel_config.form_tab or panel_config.form_tab == tab_name) %}
                {{ mtabs.menu_item(panel_name, loop.first, panel_config.label|sov_trans_admin_panel(ea.getEntity().getFqcn())) }}
            {% endfor %}
        {% endblock %}

        {# content #}
        {% block content %}
            {# panes #}
            {% for panel_name, panel_config in ea_crud_form.form_panels|filter(panel_config => not panel_config.form_tab or panel_config.form_tab == tab_name) %}
                {% embed '@LcSov/adminlte/embed/tabs/pane.html.twig' %}
                    {% block class %}{{ loop.first ? 'active' }}{% endblock %}
                    {% block id %}{{ panel_name }}{% endblock %}
                    {% block content %}
                        {% if panel_config['prepend_content_path'] is defined %}
                            {% include panel_config['prepend_content_path'] %}
                        {% endif %}
                        {% for field in form|filter(field => 'hidden' not in field.vars.block_prefixes and field.vars.ea_crud_form.form_panel == panel_name) %}
                            {% if not field.vars.ea_crud_form.form_tab or field.vars.ea_crud_form.form_tab == tab_name %}
                                {{ form_row(field) }}
                            {% endif %}
                        {% endfor %}
                        {% if panel_config['append_content_path'] is defined %}
                            {% include panel_config['append_content_path'] %}
                        {% endif %}
                    {% endblock %}
                {% endembed %}
            {% else %}
                {% for field in form|filter(field => 'hidden' not in field.vars.block_prefixes and (not field.vars.ea_crud_form.form_tab or field.vars.ea_crud_form.form_tab == tab_name)) %}
                    {{ form_row(field) }}
                {% endfor %}
            {% endfor %}
        {% endblock %}
    {% endembed %}
{% endblock ea_crud_widget_panels %}

{% block ckeditor_widget %}

    <textarea class="lc-ckeditor" {{ block('widget_attributes') }}>{{ value }}</textarea>

{% endblock %}