{%- if not modal -%}
    {% extends 'admin/master.html' %}
{%- endif -%}
{% import 'admin/lib.html' as lib with context %}

{% macro extra() %}
  <input name="_continue_editing" type="submit" class="btn btn-large" value="{{ _gettext('Save and Continue') }}" />
{% endmacro %}

{% block head %}
  {%- if not modal -%}
    {{ super() }}
    {{ lib.form_css() }}
  {%- endif -%}
{% endblock %}

{% block body %}
    {%- if modal -%}
      {# remove save and continue button for modal (it won't function properly) #}
      {{ lib.render_form(form, return_url, extra=None, form_opts=form_opts,
                         action=request.url, is_modal=True) }}
    {%- else -%}
      {% block navlinks %}
        <ul class="nav nav-tabs">
          <li>
              <a href="{{ return_url }}">{{ _gettext('List') }}</a>
          </li>
          <li>
              <a href="{{ get_url('.create_view', url=return_url) }}">{{ _gettext('Create') }}</a>
          </li>
          <li class="active">
              <a href="javascript:void(0)">{{ _gettext('Edit') }}</a>
          </li>
      </ul>
      {% endblock %}

      {{ lib.render_form(form, return_url, extra(), form_opts) }}
    {%- endif -%}
{% endblock %}

{% block tail %}
    {%- if modal -%}
      <script>
      // fill the header of modal dynamically
      $('.modal-header h3').html('{% block header_text -%}
        {{ _gettext('Edit Record') + ' #' + request.args.get('id') }}
      {%- endblock %}');

      // fixes "remote modal shows same content every time"
      $('.modal').on('hidden', function() {
        $(this).removeData('modal');
      });

      $(function() {
        // Apply flask-admin global styles after the modal is loaded
        window.faForm.applyGlobalStyles(document);
      });
      </script>
    {%- else -%}
      {{ super() }}
      {{ lib.form_js() }}
    {%- endif -%}
{% endblock %}
