{% extends "admin/base_site.html" %}
{% load i18n admin_urls admin_static admin_modify suit_tags %}

{% block extrahead %}{{ block.super }}
  <script type="text/javascript" src="{% url 'admin:jsi18n' %}"></script>
  {{ media }}
{% endblock %}

{% block extrastyle %}{{ block.super }}{% endblock %}

{% block coltype %}colM{% endblock %}

{% block bodyclass %}{{ block.super }} app-{{ opts.app_label }} model-{{ opts.model_name }} change-form{% endblock %}

{% block extrajs %}
  {{ block.super }}

  {% if 'CONFIRM_UNSAVED_CHANGES'|suit_conf %}
    <!-- Warn on leaving unsaved form -->
    <script src="{% static 'suit/js/suit-form-confirm.js' %}"></script>
    <script type="text/javascript">
      confirmExitIfModified('{{ opts.module_name }}_form', '{% trans 'You have unsaved changes' %}.');
    </script>
  {% endif %}

  {% if adminform.model_admin.suit_form_tabs %}
    <script type="text/javascript">
      (function ($) {
        $(function () {
          $('#suit_form_tabs').suit_form_tabs();
        });
      }(Suit.$))
    </script>
  {% endif %}

  <script>
    {#    (function ($) {#}
    {#      $(function () {#}
    {#        $("#{{ opts.module_name }}_form").suit_form_debounce();#}
    {#      });#}
    {#    }(Suit.$))#}
  </script>

{% endblock %}

{% block breadcrumbs %}
  <div class="clearfix">

    {% if not is_popup %}
      <ol class="breadcrumb">
        <li><a href="{% url 'admin:index' %}">{% trans 'Home' %}</a></li>
        <li><a href="{% url 'admin:app_list' app_label=opts.app_label %}">{{ opts.app_config.verbose_name }}</a></li>
        <li>{% if has_change_permission %}
          <a href="{% url opts|admin_urlname:'changelist' %}">{{ opts.verbose_name_plural|capfirst }}</a>{% else %}{{ opts.verbose_name_plural|capfirst }}{% endif %}
        </li>
        <li class="active">
          {% if add %}{% trans 'Add' %} {{ opts.verbose_name }}{% else %}{{ original|truncatewords:"18" }}{% endif %}</li>
      </ol>
    {% endif %}

    {% block object-tools %}
      {% if change %}
        {% if not is_popup %}
          <div class="btn-toolbar pull-right">
            <div class=" btn-group">
              {% block object-tools-items %}
                {% url opts|admin_urlname:'history' original.pk|admin_urlquote as history_url %}
                <a href="{% add_preserved_filters history_url %}" class="historylink btn btn-primary">{% trans "History" %}</a>
                {% if has_absolute_url %}
                  <a href="{{ absolute_url }}" class="viewsitelink btn btn-primary" target="_blank">{% trans "View on site" %}</a>
                {% endif %}
              {% endblock %}
            </div>
          </div>
        {% endif %}
      {% endif %}
    {% endblock %}
  </div>
{% endblock %}


{% block content %}
  <div id="content-main">

    {% block suit_form_tabs %}
      {% if adminform.model_admin.suit_form_tabs %}

{#        <nav id="suit_form_tabs" class="navbar navbar-default panel navbar-suit-tabs" data-tab-prefix="suit-tab">#}
{#            <div class="container-fluid">#}
{#                <ul class="nav navbar-nav">#}
{#                  {% for tab in adminform.model_admin.suit_form_tabs %}#}
{#                    <li><a href="#{{ tab.0 }}">{{ tab.1 }}</a></li>{% endfor %}#}
{#                </ul>#}
{#            </div>#}
{#        </nav>#}

        <ul id="suit_form_tabs" class="nav nav-tabs nav-tabs-suit" data-tab-prefix="suit-tab">
          {% for tab in adminform.model_admin.suit_form_tabs %}
            <li><a href="#{{ tab.0 }}">{{ tab.1 }}</a></li>{% endfor %}
        </ul>

      {% endif %}
    {% endblock %}

    <form {% if has_file_field %}enctype="multipart/form-data" {% endif %}action="{{ form_url }}" method="post" id="{{ opts.model_name }}_form" class="form-horizontal" novalidate>
      {% csrf_token %}{% block form_top %}{% endblock %}
      <div class="tab-content tab-content-main">
        {% if is_popup %}
          <input type="hidden" name="{{ is_popup_var }}" value="1"/>{% endif %}
        {% if to_field %}
          <input type="hidden" name="{{ to_field_var }}" value="{{ to_field }}"/>{% endif %}
        {% if save_on_top %}{% block submit_buttons_top %}{% submit_row %}{% endblock %}{% endif %}
        {% 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 %}

        {# render suit_form_includes at top position #}
        {% include 'suit/includes/change_form_includes.html' with position='top' %}

        {% block field_sets %}
          {% for fieldset in adminform %}
            {% include "admin/includes/fieldset.html" %}
          {% endfor %}
        {% endblock %}


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

      {# render suit_form_includes at middle position #}
      {% include 'suit/includes/change_form_includes.html' with position='middle' %}

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

      {# render suit_form_includes at last position #}
      {% include 'suit/includes/change_form_includes.html' with position='bottom' %}

      {% block submit_buttons_bottom %}{% submit_row %}{% endblock %}

      {% if adminform and add %}
        <script type="text/javascript">
          (function ($) {
            $(document).ready(function () {
              $('form#{{ opts.model_name }}_form :input:visible:enabled:first').focus()
            });
          })(django.jQuery);
        </script>
      {% endif %}

      {# JavaScript for prepopulated fields #}
      {% prepopulated_fields_js %}


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