{% extends "admin/base_site.html" %}

<!-- LOADING -->
{% load url from future %}
{% load admin_static i18n admin_modify grp_tags %}

<!-- STYLESHEETS -->
{% block stylesheets %}
    {{ block.super }}
{% endblock %}

<!-- JAVASCRIPTS -->
{% block javascripts %}
    {{ block.super }}
    {% url 'admin:jsi18n' as jsi18nurl %}
    <script type="text/javascript" src="{{ jsi18nurl|default:'../../../jsi18n/' }}"></script>
    <script type="text/javascript" charset="utf-8">
        (function($) {
            $(document).ready(function() {
                grappelli.initDateAndTimePicker();
                $("#grp-content-container .grp-group").grp_collapsible_group();
                $("#grp-content-container .grp-collapse").grp_collapsible({
                    on_init: function(elem, options) {
                        // open collapse (and all collapse parents) in case of errors
                        if (elem.find("ul.errorlist").length > 0) {
                            elem.removeClass("grp-closed")
                                .addClass("grp-open");
                            elem.parents(".grp-collapse")
                                .removeClass("grp-closed")
                                .addClass("grp-open");
                        }
                    }
                });
                var related_lookup_fields_fk = {% get_related_lookup_fields_fk adminform.model_admin %};
                var related_lookup_fields_m2m = {% get_related_lookup_fields_m2m adminform.model_admin %};
                var related_lookup_fields_generic = {% get_related_lookup_fields_generic adminform.model_admin %};
                var autocomplete_fields_fk = {% get_autocomplete_lookup_fields_fk adminform.model_admin %};
                var autocomplete_fields_m2m = {% get_autocomplete_lookup_fields_m2m adminform.model_admin %};
                var autocomplete_fields_generic = {% get_autocomplete_lookup_fields_generic adminform.model_admin %};
                $.each(related_lookup_fields_fk, function() {
                    $("#id_" + this).grp_related_fk({lookup_url:"{% url 'grp_related_lookup' %}"});
                });
                $.each(related_lookup_fields_m2m, function() {
                    $("#id_" + this).grp_related_m2m({lookup_url:"{% url 'grp_m2m_lookup' %}"});
                });
                $.each(related_lookup_fields_generic, function() {
                    var content_type = "#id_" + this[0],
                        object_id = "#id_" + this[1];
                    $(object_id).grp_related_generic({content_type:content_type, object_id:object_id, lookup_url:"{% url 'grp_related_lookup' %}"});
                });
                $.each(autocomplete_fields_fk, function() {
                    $("#id_" + this).grp_autocomplete_fk({
                        lookup_url:"{% url 'grp_related_lookup' %}",
                        autocomplete_lookup_url:"{% url 'grp_autocomplete_lookup' %}"
                    });
                });
                $.each(autocomplete_fields_m2m, function() {
                    $("#id_" + this).grp_autocomplete_m2m({
                        lookup_url:"{% url 'grp_m2m_lookup' %}",
                        autocomplete_lookup_url:"{% url 'grp_autocomplete_lookup' %}"
                    });
                });
                $.each(autocomplete_fields_generic, function() {
                    var content_type = "#id_" + this[0],
                        object_id = "#id_" + this[1];
                    $(object_id).grp_autocomplete_generic({
                        content_type:content_type,
                        object_id:object_id,
                        lookup_url:"{% url 'grp_related_lookup' %}",
                        autocomplete_lookup_url:"{% url 'grp_autocomplete_lookup' %}"
                    });
                });
                $("a#grp-open-all").bind("click", function(){
                    $("#grp-content .grp-collapse-handler").each(function() {
                        $(this).parent(".grp-collapse").removeClass("grp-closed").addClass("grp-open");
                    });   
                });
                $("a#grp-close-all").bind("click", function(){
                    $("#grp-content .grp-collapse-handler").each(function() {
                        $(this).parent(".grp-collapse").removeClass("grp-open").addClass("grp-closed");
                    });
                });
                // HACK: get rid of currently/change with URL–fields. F**K!!!
                $('p.url').each(function() {
                    $(this).find("a").remove();
                    var text = $(this).html();
                    text = text.replace(/^\w*: /, "");
                    text = text.replace(/<br>.*: /, "");
                    $(this).html(text);
                });
            });
        })(grp.jQuery);
    </script>
    {{ media }}
{% endblock %}

<!-- COLTYPE/BODYCLASS -->
{% block bodyclass %}{{ opts.app_label }}-{{ opts.object_name.lower }} grp-change-form{% endblock %}
{% block content-class %}{% endblock %}


<!-- PAGE-TOOLS -->
{% block page-tools %}
    {% if not is_popup %}
        <ul>
            <li><a href="javascript://" class="grp-tool" id="grp-open-all" title="{% trans 'Open All Items' %}">&nbsp;</a></li>
            <li><a href="javascript://" class="grp-tool" id="grp-close-all" title="{% trans 'Close All Items' %}">&nbsp;</a></li>
        </ul>
    {% endif %}
{% endblock %}

<!-- BREADCRUMBS -->
{% block breadcrumbs %}
    {% if not is_popup %}
        <ul>
            <li><a href="../../../">{% trans "Home" %}</a></li>
            <li><a href="../../">{% trans app_label|capfirst|escape %}</a></li>
            <li>{% if has_change_permission %}<a href="../">{{ opts.verbose_name_plural|capfirst }}</a>{% else %}{{ opts.verbose_name_plural|capfirst }}{% endif %}</li>
            <li>{% if add %}{% trans "Add" %} {{ opts.verbose_name }}{% else %}{{ original|truncatewords:"18" }}{% endif %}</li>
        </ul>
    {% endif %}
{% endblock %}

<!-- OBJECT TOOLS -->
{% block object-tools %}
    {% if change %}
        {% if not is_popup %}
            <ul class="grp-object-tools">
                {% block object-tools-items %}
                    <li><a href="history/">{% trans "History" %}</a></li>
                    {% if has_absolute_url %}<li><a href="../../../r/{{ content_type_id }}/{{ object_id }}/" class="grp-state-focus" target="_blank">{% trans "View on site" %}</a></li>{% endif%}
                {% endblock %}
            </ul>
        {% endif %}
    {% endif %}
{% endblock %}

<!-- CONTENT -->
{% block content %}
    <form novalidate {% if has_file_field %}enctype="multipart/form-data" {% endif %}action="{{ form_url }}" method="post" id="{{ opts.module_name }}_form">{% csrf_token %}{% block form_top %}{% endblock %} 
        <div>
            <!-- Popup Hidden Field -->
            {% if is_popup %}<input type="hidden" name="_popup" value="1" />{% endif %}
            
            <!-- No Submit-Row on Top -->

            <!-- Errors -->
            {% if errors %}
                <p class="errornote">{% blocktrans count errors|length as counter %}Please correct the error below.{% plural %}Please correct the errors below.{% endblocktrans %}</p>
                <ul class="errorlist">{% for error in adminform.form.non_field_errors %}<li>{{ error }}</li>{% endfor %}</ul>
            {% endif %}
            
            <!-- Fieldsets -->
            {% block field_sets %}
                {% for fieldset in adminform %}
                    {% include "admin/includes/fieldset.html" %}
                {% endfor %}
            {% endblock %}
            
            {% block after_field_sets %}{% endblock %}
            
            <!-- Inlines -->
            {% 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 %}
            
            <!-- Submit-Row -->
            {% block submit_buttons_bottom %}{% submit_row %}{% endblock %}
            
            {% if adminform and add %}
                {% block extrahead %}
                    {{ block.super }}
                    <script type="text/javascript">
                        (function($){
                            $(function(){
                                try {
                                    // focus on first form-field
                                    $("#{{ adminform.first_field.auto_id }}, #{{ adminform.first_field.auto_id }}_0")
                                    .eq(0).focus();
                                } catch(e) {}
                            });
                        }(grp.jQuery));
                    </script>
                {% endblock %}
            {% endif %}
            
            <!-- JS for prepopulated fields -->
            {% prepopulated_fields_js %}
            
        </div>
    </form>
{% endblock %}
