{% extends 'admin/master.html' %}
{% import 'admin/static.html' as admin_static with context%}
{% import 'admin/lib.html' as admin_lib with context %}
{% import 'shelf/lib.html' as lib with context %}
{% import 'shelf/model/layout.html' as model_layout with context %}

{% macro edit_extra_btn() %}
    <li class="actions">
        <a role="submit" class="_continue_editing" href="javascript:void(0)">
            <i class="fa fa-refresh"></i> {{ _gettext('Save and Continue') }}
        </a>
    </li>
    {{ lib.get_view_extensions("extra_btn") }}
{% endmacro %}

{% macro edit_extra_fld() %}
{% endmacro %}

{% block head %}
    {{ super() }}
    {{ admin_lib.form_css() }}
    <link href="{{ url_for('shelf.static', filename='select2/select2-flat.css') }}" rel="stylesheet">
    <link href="{{ url_for('shelf.static', filename='switchery/switchery.min.css') }}" rel="stylesheet">
{% endblock %}

{% block body %}
    <div class="row create">
        <div class="{% if admin_view.form_shortcuts %}col-sm-9 {% endif %}col-xs-12">
            <div class="title ">
                {% block model_create_title %}
                    <h1>{{ admin_view.name.upper() }}</h1>
                {% endblock %}
            </div>
            <div class="content">
              {% call lib.form_tag(form) %}
                  {{ lib.render_form_fields(form, form_opts=form_opts, extra=edit_extra_fld()) }}
              {% endcall %}
            </div>
        </div>
        {% if admin_view.form_shortcuts %}
            {{ lib.render_form_shortcuts(form) }}
        {% endif %}
    </div>
{% endblock %}

{% block page_body %}
    {{ super() }}
    {% call lib.navbar(admin_view.name, pos="bottom") %}
        {{ lib.render_form_buttons(return_url, edit_extra_btn()) }}
    {% endcall %}
{% endblock %}

{% block tail_js %}
    {{ super() }}
    {{ admin_lib.form_js() }}
    <script src="{{ url_for('shelf.static', filename='switchery/switchery.min.js') }}"></script>
    <script language="javascript">
        (function($) {
            $('a.save-model').click(function (e) {
                $("form.model-form").submit();
                e.stopPropagation();
            });

            $('a._continue_editing').click(function (e) {
                $("form.model-form").append(
                    "<input type='hidden' name='_continue_editing' value='1'></input>"
                );
                $("form.model-form").submit();
                e.stopPropagation();
            });

            var elems = Array.prototype.slice.call(document.querySelectorAll('.js-switch'));

            elems.forEach(function(html) {
              var switchery = new Switchery(html, { color: '#a9a3d9', secondaryColor: '#e0e0e0' });
            });
        })(jQuery);
    </script>
{% endblock %}
