{% extends "admin_base" %}
{% block title %}{% if admin_edit_title %}{{ admin_edit_title }}{% endif %} — {{ resource.title }}{% endblock %}
{% block breadcrumb %}
  <span class="admin-topbar__sep">/</span>
  <a href="{{ admin_prefix }}/{{ resource.key }}/list">{{ resource.title }}</a>
  <span class="admin-topbar__sep">/</span>
  <span>{% if admin_edit_breadcrumb %}{{ admin_edit_breadcrumb }}{% endif %}</span>
{% endblock %}
{% block content %}
  {% block edit_header %}
  <header class="admin-page__header">
    <h1>{% if admin_edit_title %}{{ admin_edit_title }}{% endif %} — {{ resource.title }}</h1>
  </header>
  {% endblock %}

  {% set_global can_edit = false %}
  {% if current_user %}
    {% if current_user.is_superuser %}
      {% set_global can_edit = true %}
    {% else %}
      {% for groupe in current_user.groupes %}
        {% for perm in groupe.permissions %}
          {% if perm.resource_key == resource.key and perm.can_update %}
            {% set_global can_edit = true %}
          {% endif %}
        {% endfor %}
      {% endfor %}
    {% endif %}
  {% endif %}

  {% if current_user and can_edit %}
  {% block edit_form %}
  <div class="admin-card admin-card--form" data-admin-auto-init>
    <div class="admin-card__header">
      <h2>{% if admin_edit_card_info %}{{ admin_edit_card_info }}{% endif %}</h2>
    </div>
    <div class="admin-card__body">
      <form method="POST" action="{{ admin_prefix }}/{{ resource.key }}/{{ object_id }}/edit" enctype="multipart/form-data">
        {% csrf %}
        {% if orig_updated_at is defined %}
        <input type="hidden" name="__original_updated_at" value="{{ orig_updated_at }}">
        {% elif object.updated_at is defined %}
        <input type="hidden" name="__original_updated_at" value="{{ object.updated_at }}">
        {% endif %}
        {% if return_qs is defined and return_qs %}
        <input type="hidden" name="return_qs" value="{{ return_qs }}">
        {% endif %}
        {% block edit_form_fields %}
        <div class="form-grid">
          {% if form_fields is defined and form_fields.html is defined and form_fields.html %}
            {{ form_fields.html }}
          {% else %}
            <p>{% if admin_edit_no_fields %}{{ admin_edit_no_fields }}{% endif %}</p>
          {% endif %}
        </div>
        {% if m2m_fields is defined and m2m_fields %}
        <div class="admin-m2m__fields">
          {% for field in m2m_fields %}
          <div class="form-group admin-m2m__group">
            <label class="form-label">{{ field.label }}</label>
            <div class="admin-m2m__choices">
              {% for choice in field.choices %}
              <label class="admin-m2m__choice">
                <input type="checkbox" name="m2m_{{ field.field_name }}__{{ choice[0] }}" value="1"
                  {% if choice[0] in field.selected %}checked{% endif %}>
                {{ choice[1] }}
              </label>
              {% endfor %}
            </div>
          </div>
          {% endfor %}
        </div>
        {% endif %}
        {% endblock %}
        {% block edit_form_actions %}
        <div class="admin-form__actions">
          <a href="{{ admin_prefix }}/{{ resource.key }}/list{% if return_qs is defined and return_qs %}?{{ return_qs }}{% endif %}" class="btn btn-secondary">{% if admin_edit_btn_cancel %}{{ admin_edit_btn_cancel }}{% endif %}</a>
          <button type="submit" class="btn btn-primary">{% if admin_edit_btn_submit %}{{ admin_edit_btn_submit }}{% endif %}</button>
        </div>
        {% endblock %}
      </form>
    </div>
  </div>
  {% endblock %}
  {% else %}
  {% block edit_denied %}
  <div class="admin-card">
    <div class="admin-card__body">
      <div class="admin-empty-state">
        <p class="admin-empty-state__desc">{% if admin_access_denied %}{{ admin_access_denied }}{% else %}Vous n'avez pas les droits nécessaires pour modifier cet élément.{% endif %}</p>
        <a href="{{ admin_prefix }}/{{ resource.key }}/list" class="btn btn-secondary">{% if admin_edit_btn_cancel %}{{ admin_edit_btn_cancel }}{% else %}Retour{% endif %}</a>
      </div>
    </div>
  </div>
  {% endblock %}
  {% endif %}
{% endblock %}
{% block extra_js %}
{{ super() }}
  <script {% csp %} src="{{ "js/admin/admin-actions.js" | runique_static }}"></script>
  <script {% csp %} src="{{ "js/admin/checkbox-toggle.js" | runique_static }}"></script>
  <script {% csp %} src="{{ "js/admin/choice-filter.js" | runique_static }}"></script>
{% endblock %}
