{% 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 extra_btn() %}
    <li class="actions">
        <a role="submit" class="_add_another" href="javascript:void(0)">
            <i class="fa fa-refresh"></i> {{ _gettext('Save and Add') }}
        </a>
    </li>
{% endmacro %}

{% macro extra_fld() %}
{% endmacro %}

{% block head %}
    {{ super() }}
    {{ admin_lib.form_css() }}
    <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=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, extra_btn()) }}
    {% endcall %}
{% endblock %}

{% block tail_js %}
    <!-- super -->
    {{ super() }}
    <!-- hey -->
    {{ admin_lib.form_js() }}
    <!-- ho -->
    <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._add_another').click(function (e) {
                $("form.model-form").append(
                    "<input type='hidden' name='_add_another' 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' });
            });

            $('[data-toggle="tooltip"]').tooltip(); //Pour faire fonctionnerles tooltip de bootstrap
        })(jQuery);
    </script>
{% endblock %}
