{% use '@EasyAdmin/form/bootstrap_4.html.twig' %}



{# TODO à corriger le theme requiert des infos easyAdmin il devrait être possible de ne pas en utiliser #}


{% block form_widget -%}
    {{ parent() }}

    {% set transHelp = label|lower~"Help" %}
    {% if transHelp|trans({}, 'lcshop') != transHelp %}
        <span class="lc-edit-help-block"><span
                    class="glyphicon glyphicon-info-sign"></span>{{ transHelp|trans({}, 'lcshop') }}</span>
        <span class="glyphicon glyphicon-info-sign info-tooltip" data-toggle="tooltip" data-position="bottom"
              title=""></span>
    {% endif %}

{% endblock form_widget %}

{% block money_widget %}
    {%- set attr = attr|merge({pattern: ('^[0-9]+(\.[0-9]{1,2})?$')|trim}) -%}
    {%- set money_pattern = "{{ widget }} €" -%}

    {{ parent() }}
{% endblock %}

{%- block choice_widget_collapsed -%}
    {% if placeholder is empty %}{% set placeholder = 'field.default.placeholder' %}{% endif %}
    {{ parent() }}
{% endblock %}

{% block form_row -%}

    {% set _field_type = easyadmin.field.fieldType|default('default') %}
    <div class="form-group {% if (not compound or force_error|default(false)) and not valid %}has-error{% endif %} field-{{ block_prefixes|slice(-2)|first }}">
        {{- form_label(form) -}}
        <div class="form-widget">
            {% set has_prepend_html = easyadmin.field.prepend_html|default(null) is not null %}
            {% set has_append_html = easyadmin.field.append_html|default(null) is not null %}
            {% set has_input_groups = has_prepend_html or has_append_html %}

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

                {{- form_widget(form) -}}

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

            {#{% if _field_type in ['datetime', 'datetime_immutable', 'date', 'date_immutable', 'dateinterval', 'time', 'time_immutable', 'birthday'] and easyadmin.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 labelHelp = 'field.'~easyadmin['entity']['name']~'.'~name~'Help' %}
            {% if labelHelp|trans({}, 'lcshop') == labelHelp %}{% set labelHelp = 'form.field.default.'~name~'Help' %}{% endif %}
            {% if labelHelp|trans({}, 'lcshop') != labelHelp %}
                <small class="form-text text-muted">{{ labelHelp|trans({}, 'lcshop')|raw }}</small>
            {% endif %}

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



{% block easyadmin_widget_groups %}

    {% import '@LcShop/backend/default/block/macros.html.twig' as macros %}
    {% for group_name, group_config in easyadmin_form_groups|filter(group_config => not group_config.form_tab or group_config.form_tab == tab_name) %}
        {{ macros.startCard(group_config.columns|default('12'), group_config.label, 'primary') }}

        {% for field in form|filter(field => 'hidden' not in field.vars.block_prefixes and field.vars.easyadmin.form_group == group_name) %}

            {% if not field.vars.easyadmin.form_tab or field.vars.easyadmin.form_tab == tab_name %}
                <div class="col-{{ field.vars.easyadmin.field.columns|default('12') }} {{ field.vars.easyadmin.field.css_class|default('') }}">
                    {{ form_row(field) }}
                </div>
            {% endif %}
        {% endfor %}
        {{ macros.endCard() }}
    {% else %}

        {{ macros.startCard(8) }}
        {% for field in form|filter(field => 'hidden' not in field.vars.block_prefixes and (not field.vars.easyadmin.form_tab or field.vars.easyadmin.form_tab == tab_name)) %}
            <div class="col-{{ field.vars.easyadmin.field.columns|default('12') }} {{ field.vars.easyadmin.field.css_class|default('') }}">
                {{ form_row(field) }}
            </div>
        {% endfor %}
        {{ macros.endCard() }}
    {% endfor %}
{% endblock easyadmin_widget_groups %}

{% block form_label -%}
    {% if label is not same as(false) -%}
        {%- 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_format is not empty -%}
            {% set label = label_format|replace({
                '%name%': name,
                '%id%': id,
            }) %}
        {%- else -%}
            {% if 'field.MerchantConfig.' in label %}
                {% set name_trad = label|replace({'field.MerchantConfig.': ''}) %}
                {% set trad = name_trad|lc_trad(easyadmin['entity']['name'], 'field') %}
            {% else %}
                {% set trad = name|lc_trad(easyadmin['entity']['name'], 'field') %}
            {% endif %}

        {%- endif -%}
        <{{ element|default('label') }}{% if label_attr %}{% with { attr: label_attr } %}{{ block('attributes') }}{% endwith %}{% endif %}>{{ trad }}</{{ element|default('label') }}>
    {%- endif -%}
{%- endblock form_label %}

{% block easyadmin_rest %}

    {% if form.status is defined %}
        <div class="card">
            <div class="card-footer field-status-inline">
                {{ form_row(form.status) }}
            </div>
        </div>
    {% endif %}
    {{- form_rest(form) -}}
    <div class="card">
        <div class="card-footer">
            {{- block('item_actions') -}}
        </div>
    </div>
{% endblock easyadmin_rest %}


{% 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 -%}

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

            {#  {% if translation_domain == 'lcshop' %}
                  {{ name|lc_trad(easyadmin['entity']['name'], 'field') }}
              {% else %}#}
            {{- label is not same as(false) ? (translation_domain is same as(false) ? label : label|trans(label_translation_parameters, translation_domain))|raw -}}
            {#{% endif %}#}
            {{- form_errors(form) -}}
        </label>
    {%- endif -%}
{%- endblock checkbox_radio_label %}


{% block vich_image_widget %}
    {% spaceless %}
        <div class="vich-image">
            {{ form_widget(form.file) }}
            {% if form.delete is defined %}
                {{ form_row(form.delete) }}
            {% endif %}

            {% if image_uri %}
                <img src="{{ lc_liip(image_uri, 'tile') }}" alt=""/></a>
            {% endif %}

        </div>
    {% endspaceless %}
{% endblock %}



{% block item_actions %}
    {% set _translation_domain = easyadmin.entity.translation_domain %}
    {% set _trans_parameters = { '%entity_name%':  easyadmin.entity.name|trans(domain = _translation_domain), '%entity_label%': easyadmin.entity.label|trans(domain = _translation_domain) } %}

    {# the 'save' action is hardcoded for the 'edit' and 'new' views #}
    <button type="submit" name="save_and_leave" class="btn btn-primary action-save">
        <span class="btn-label">{{ 'action.save'|trans(_trans_parameters, _translation_domain) }}</span>
    </button>
    {% if  easyadmin.entity.name =='ProductFamily' %}
    <button name="submitAndStay" value="1" type="submit" class="btn btn-success action-save">
        <span class="btn-label">{{ 'action.saveAndStay'|trans(_trans_parameters, _translation_domain) }}</span>
    </button>
    {% endif %}

    {% set _entity_actions = (easyadmin.view == 'new')
        ? easyadmin_get_actions_for_new_item(easyadmin.entity.name)
        : easyadmin_get_actions_for_edit_item(easyadmin.entity.name) %}

    {% set _entity_id = (easyadmin.view == 'new')
        ? null
        : attribute(easyadmin.item, easyadmin.entity.primary_key_field_name) %}

    {% set _request_parameters = { entity: easyadmin.entity.name, referer: app.request.query.get('referer') } %}

    {{ include('@EasyAdmin/default/includes/_actions.html.twig', {
        actions: _entity_actions,
        entity_config: easyadmin.entity,
        request_parameters: _request_parameters,
        translation_domain: _translation_domain,
        trans_parameters: _trans_parameters,
        item_id: _entity_id,
        item: easyadmin.item
    }, with_context = false) }}
{% endblock item_actions %}



{#

{% block _user_niche_row %}

    {% import '@LcShop/backend/default/block/macros.html.twig' as macros %}

    <div class="row" id="{{ id }}" data-prototype="

        {% filter escape %}

            {% embed "@LcShop/backend/default/block/embed_modal.twig" %}

                {% block id %}modal-edit-address-___name__ {% endblock %}
                {% block title %}{{ "action.address.edit"|trans }}{% endblock %}

                {% block content %}
                    <div class="col">
                        {{ form_widget(prototype) }}
                    </div>
                {% endblock %}

                {% block footer %}
                    <button class="btn btn-primary" data-dismiss="modal" value="Ok"></button>
                {% endblock %}
            {% endembed %}
            {% set button = '<button type="button" data-toggle="modal" data-target="#modal-edit-address-___name__" >EDIT</button>' %}
            <div class="col-4">
                 {% embed '@LcShop/backend/default/block/embed_box.twig' %}
                    {% trans_default_domain 'lcshop' %}
                    {% block class %}bg-info{% endblock %}
                    {% block icon %}'credit-card{% endblock %}
                    {% block label %}NICHE{% endblock %}
                    {% block value %}NICHE{% endblock %}
                    {% if button %}
                        {% block button %}{{ button|raw }}{% endblock %}
                    {% endif %}
                {% endembed %}
            </div>
        {% endfilter %}
    ">

        {{ block('form_label') }}

        {% for address in form.vars.value %}
            {% embed "@LcShop/backend/default/block/embed_modal.twig" %}

                {% block id %}modal-edit-address-{{ address.id }}{% endblock %}
                {% block title %}{{ "action.address.edit"|trans }}{% endblock %}

                {% block content %}
                    <div class="col">
                        {{ form_widget(form) }}
                    </div>
                {% endblock %}

                {% block footer %}
                    <button class="btn btn-primary" data-dismiss="modal" value="Ok"></button>
                {% endblock %}
            {% endembed %}
            {% set button = '<button type="button" data-toggle="modal" data-target="#modal-edit-address-{{ address.id }}" >EDIT</button>' %}
            <div class="col-4">
                {{ macros.box_info('bg-info', 'address-card', address.title, address.summary, button) }}
            </div>

        {% endfor %}

        {% if allow_add|default(false) %}
            {% set js_add_item %}
                $(function() {
                event.preventDefault();

                var collection = $('#{{ id }}');
                // Use a counter to avoid having the same index more than once
                var numItems = collection.data('count') || collection.children('div.form-group').length;

                collection.prev('.collection-empty').remove();

                var newItem = collection.attr('data-prototype')
                .replace(/\>{{ prototype.vars.name }}label__\</g, '>' + numItems + '<')
                .replace(/_{{ name }}_{{ prototype.vars.name }}/g, '_{{ name }}_' + numItems)
                .replace(/{{ name }}\]\[{{ prototype.vars.name }}\]/g, '{{ name }}][' + numItems + ']')
                ;

                // Increment the counter and store it in the collection
                collection.data('count', ++numItems);

                collection.append(newItem).trigger('easyadmin.collection.item-added');
                log( $('button[data-toggle=\'modal\']'));
                $('button[data-toggle=\'modal\']').on('click', function(){
                    $($(this).data('target')).modal('show')
                });

                });
            {% endset %}

            <div class="form-group field-collection-action">
                <a id="easyadmin-add-collection-item-{{ form.vars.id }}" href="#"
                   onclick="{{ js_add_item|raw }}; return false;">
                    <i class="fa fa-plus pr-1"></i>
                    {{ 'action.add_new_item'|trans({}, 'EasyAdminBundle') }}
                </a>
            </div>
        {% endif %}


        {% do form.setRendered %}


    </div>
{% endblock %}
#}
