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

{% block main_title_wrap %}
  <div class="col-8">
    <div class="first-line d-flex align-items-center">
      <h2>{% block main_title %}{{ mainTitle|default() }}{% endblock %}</h2>
      {% if backToEntityListUrl is defined and backToEntityListUrl %}
        <a href="{{ backToEntityListUrl }}" class="btn btn-secondary ml-4">
          <i class="far fa-arrow-turn-up"></i>
          <span>Retour à la liste</span>
        </a>
      {% endif %}
    </div>

    <h1 class="content-title">{{ aa_content_title(entity) }}</h1>
  </div>
{% endblock %}

{% block navbar_top_right %}
  <div class="col-4 d-flex flex-column align-items-end">
    <div class="heading-actions">
      <a class="btn btn-success mb-2" id="submit_fake">
        <span class="icon mr-2"><i class="fas fa-floppy-disk"></i></span>
        <span class="text">Enregistrer</span>
      </a>
    </div>
    <div class="d-flex flex-row topbar-actions">
      {% include '@AutoAdminAdmin/entity/_partials/next_prev.html.twig' %}
      {% include '@AutoAdminAdmin/entity/_partials/see_content.html.twig' %}
      {% include '@AutoAdminAdmin/entity/_partials/topbar_actions.html.twig' %}
    </div>
  </div>
{% endblock %}

{% block content_tabs %}
  {# admin form tab menu #}
  <div class="content-tabs col-12">
    {% set adminTabs = aa_admin_entity_tab_menu() %}
    <div class="nav nav-pills">
      {% for tab, tabConfig in adminTabs %}
        <a class="nav-link{% if tabConfig.active|default(false) == true %} active{% endif %}"
           href="{{ admin_path(aa.entity.slug, 'update', tabConfig.routeParams|default({})|merge({
             id: entity.id,
           })) }}"
        ><i class="{{ tabConfig.icon }}"></i> {{ (tabConfig.label|default(tab))|trans({}, 'aa_admin') }}</a>
      {% endfor %}
    </div>
  </div>
{% endblock %}

{% block page_top %}
{% endblock %}

{% block content_rows %}
  <!-- Content Row -->
  <div class="row">
    {% block content_inner %}
      {# 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 %}
            {% include "@AutoAdminAdmin/_partials/_locale-switcher.html.twig" %}
            {% form_theme form with ['@AutoAdminAdmin/form/form_theme.html.twig', '@AutoAdminMedia/Form/fields.html.twig'] %}

            <em class="d-block">* Eléments obligatoires</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-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>
    {% endblock %}
  </div>
{% endblock %}