{% extends "@AutoAdminAdmin/entity_layout.html.twig" %}

{% block main_title_wrap %}
  <div class="main-title-wrap">
    <div class="first-line d-flex align-items-center">
      {% set listingUrl = admin_path(entity, 'list') %}
      <h2>
      {% if listingUrl|default() is not empty %}
        <a href="{{ listingUrl }}"
           title="Retour au listing"
           data-bs-toggle="tooltip"
           data-bs-placement="right"
        >{{ block('main_title') }} <i class="fa-solid fa-turn-up"></i></a>
      {% else %}
        {{ block('main_title') }}
      {% endif %}
      </h2>
    </div>
    {% set localeData = aa_admin_locale_switches(entity|default(null)) %}
    {% set cls = localeData.locale_current_switch %}
    <h1 class="content-title">
      {% if cls.url|default() is not empty %}
        <a href="{{ cls.url }}"
           target="_blank"
           title="Voir en ligne"
           data-bs-toggle="tooltip"
           data-bs-placement="right"
        >{{ aa_content_title(entity) }} <i class="fa-solid fa-link"></i></a>
      {% else %}
        {{ aa_content_title(entity) }}
      {% endif %}
    </h1>
  </div>
{% endblock %}

{% block pre_content_wrap %}
  {# admin form tab menu #}
  <div class="row">
    <div class="col">
      <div class="update-tabs">
        {% include "@AutoAdminAdmin/_partials/_locale_switcher.html.twig" %}

        {% set adminTabs = aa_admin_entity_tab_menu() %}
        <div class="nav nav-pills">
          {% for tab, tabConfig in adminTabs %}
            {% set tabUrl = admin_path(aa.entity.slug, 'update', tabConfig.routeParams|default({})|merge({id: entity.id,})) %}
            <a class="nav-link{% if tabConfig.active|default(false) == true %} active{% endif %}"
               href="{{ tabUrl }}"
            ><i class="{{ tabConfig.icon }}"></i> {{ (tabConfig.label|default(tab))|trans({}, 'aa_admin') }}</a>
          {% endfor %}
        </div>
      </div>
    </div>
    <div class="col d-flex flex-row-reverse">
      {% include '@AutoAdminAdmin/entity/_partials/next_prev.html.twig' %}
    </div>
  </div>
{% endblock %}

{% block inner_page_content %}
  <!-- Content Row -->
  <div class="row">
      {# display builder or form #}
      <div class="col-12">
        {% if builderUrl is defined and builderUrl is not empty %}
          {# plug in builder code #}
          {% include '@AutoAdminBrickBuilder/builder/include_builder.html.twig' with {brick: builderRootBrick} %}
        {% else %}
          {# build classical form #}
          {% if form is defined and form is not empty %}
            {% form_theme form with ['@AutoAdminAdmin/form/form_theme.html.twig', '@AutoAdminMedia/Form/fields.html.twig'] %}

            <em class="d-block">{{ 'mandatory_fields'|trans({}, 'aa_admin') }}</em>
            <div class="clearfix"></div>

            {{ form_start(form, {attr: {id: "entity_form"}}) }}
              {% set submits = [] %}
              {% set fields = [] %}
              {% set headlights = [] %}

              {% for formChild in form %}
                {% if formChild.vars.priority == 1 %}
                  {% set headlights = headlights|merge([formChild]) %}
                {% elseif formChild.vars.name|slice(0, 6) == 'submit' %}
                  {% set submits = submits|merge([formChild]) %}
                {% else %}
                  {% set fields = fields|merge([formChild]) %}
                {% endif %}
              {% endfor %}

              <div class="row">
                <div class="col-{% if headlights is not empty %}9{% else %}12{% endif %}">
                  {% for field in fields %}
                    {% if field.vars.errors|length %}
                      {{ form_row(field, field.vars|merge({'attr': {'autofocus': null}})) }}
                    {% else %}
                      {{ form_row(field) }}
                    {% endif %}
                  {% endfor %}
                </div>

                {% if headlights is not empty %}
                  <div class="col-3">
                    <div class="form-right position-fixed">
                      {% for headlight in headlights %}
                        {{ form_row(headlight) }}
                      {% endfor %}
                    </div>
                  </div>
                {% endif %}
              </div>

              <div class="form-group submit-actions d-inline-flex">
                <div class="btn-group">
                  {% for key, submit in submits %}
                    {% if key > 0 %}
                      {{ form_widget(submit, {attr: {class: 'btn-secondary'}}) }}
                    {% else %}
                      {{ form_widget(submit) }}
                    {% endif %}
                  {% endfor %}
                </div>
              </div>
            {{ form_end(form) }}
          {% endif %}
        {% endif %}
      </div>
  </div>
{% endblock %}