{% extends "admin/base.html" %}
{% load i18n static admin_urls admin_modify %}

{# ---------- TITLE ---------- #}
{% block title %}
  {% if add %}{% blocktrans with name=opts.verbose_name %}Add {{ name }}{% endblocktrans %}{% else %}{{ original|striptags }}{% endif %} |
  {{ site_title|default:'' }}
{% endblock %}

{# ---------- BODY CLASS ---------- #}
{% block bodyclass %}{{ opts.app_label }}-{{ opts.model_name }} change-form-page{% endblock %}

{# ---------- BREADCRUMBS (в нашем стиле, без имени приложения) ---------- #}
{% block breadcrumbs %}
  {% if not is_popup %}
    <div class="breadcrumbs">
      <a href="{% url 'admin:index' %}">{% trans 'Home' %}</a>
      {% if has_view_permission %}
        &rsaquo; <a href="{% url opts|admin_urlname:'changelist' %}">{{ opts.verbose_name_plural|capfirst }}</a>
      {% else %}
        &rsaquo; {{ opts.verbose_name_plural|capfirst }}
      {% endif %}
      &rsaquo;
      {% if add %}
        {% trans 'Add' %} {{ opts.verbose_name }}
      {% else %}
        {{ original|striptags }}
      {% endif %}
    </div>
  {% endif %}
{% endblock %}

{# ---------- Убираем дублирующий текст перед h1 (оставляем только наш заголовок) ---------- #}
{% block content_title %}{% endblock %}

{# ---------- EXTRA HEAD: стандартные js admin + наши стили уже в base_site ---------- #}
{% block extrahead %}
  {{ block.super }}
  {% if adminform and add %}
    <script type="text/javascript">
      (function() {
        window.addEventListener('load', function() {
          var form = document.getElementById('{{ opts.model_name }}_form');
          if (!form) return;
          var first = form.querySelector('input, select, textarea');
          if (first && typeof first.focus === 'function') {
            first.focus();
          }
        });
      })();
    </script>
  {% endif %}
{% endblock %}

{# ---------- CONTENT ---------- #}
{% block content %}
  <h1 class="lucus-change-form-title">{{ opts.verbose_name|capfirst }}</h1>
  <div id="content-main" class="lucus-content">
    {% if errors %}
      <p class="errornote">
        {% if errors|length == 1 %}
          {% trans "Please correct the error below." %}
        {% else %}
          {% trans "Please correct the errors below." %}
        {% endif %}
      </p>
      {{ adminform.form.non_field_errors }}
    {% endif %}

    <form {% if has_file_field %}enctype="multipart/form-data"{% endif %}
          action="{{ form_url }}"
          method="post"
          id="{{ opts.model_name }}_form"
          novalidate>
      {% csrf_token %}

      {% if is_popup %}
        <input type="hidden" name="_popup" value="1">
      {% endif %}
      {% if to_field %}
        <input type="hidden" name="_to_field" value="{{ to_field }}">
      {% endif %}

      {% block form_top %}{% endblock %}

      <div class="lucus-form-sections">
        {# ---------- FIELDSETS ---------- #}
        {% block field_sets %}
          {% for fieldset in adminform %}
            {% include "admin/includes/fieldset.html" %}
          {% endfor %}
        {% endblock %}

        {% block after_field_sets %}{% endblock %}

        {# ---------- INLINES ---------- #}
        {% block inline_field_sets %}
          {% for inline_admin_formset in inline_admin_formsets %}
            {% include inline_admin_formset.opts.template %}
          {% endfor %}
        {% endblock %}

        {% block after_related_objects %}{% endblock %}
      </div>

      {# ---------- SUBMIT ROW ---------- #}
      {% block submit_buttons_bottom %}
        {% submit_row %}
      {% endblock %}

      {# ---------- PREPOPULATED FIELDS JS ---------- #}
      {% prepopulated_fields_js %}
    </form>
  </div>
{% endblock %}

