{% extends "admin/base_site.html" %}
{% load i18n l10n admin_urls %}

{% block bodyclass %}{{ block.super }} app-{{ opts.app_label }} model-{{ opts.model_name }} cms-import{% endblock %}

{% block extrastyle %}
{{ block.super }}
<style>
.imported_item {
    font-weight: bold;
}
.imported_item.waiting {
    color: darkblue;
}
.imported_item.running {
    color: orange;
}
.imported_item.ok {
    color: darkgreen;
}
.imported_item.failed {
    color: darkred;
}
</style>
{% endblock %}

{% block extrahead %}
{{ block.super }}
{{ media.js }}
{% endblock %}

{% block breadcrumbs %}
<div class="breadcrumbs">
<a href="{% url 'admin:index' %}">{% trans 'Home' %}</a>
&rsaquo; <a href="{% url 'admin:app_list' app_label=opts.app_label %}">{{ opts.app_config.verbose_name }}</a>
&rsaquo; <a href="{% url opts|admin_urlname:'changelist' %}">{{ opts.verbose_name_plural|capfirst|escape }}</a>
&rsaquo; {{ title }}
</div>
{% endblock %}

{% block content %}

<ul>
    {% for item in items %}
    <li>{{ item.title }} ({{ item.post_type }}): <span id="status_{{ item.id }}" class="imported_item waiting">{% trans 'waiting' %}</span></li>
    {% endfor %}
</ul>

<script>

(function($) {
    $(document).ready(function($) {
        var item_ids = [{% for item in items %}{{ item.id }}{% if not forloop.last %}, {% endif %}{% endfor %}];
        import_item(item_ids.pop());
        function import_item(item_id) {
            s = $("#status_" + item_id);
            s.removeClass("waiting");
            s.addClass("running");
            s.text("{% trans 'running' %}")
            $.get("{% url 'admin:import_wordpress_item_import_item' %}", { options_id: {{ options.id }}, item_id: item_id } )
            .done(function() {
                s.addClass("ok");
                s.text("{% trans 'OK' %}")
            })
            .fail(function() {
                s.addClass("failed");
                s.text("{% trans 'failed' %}")
            })
            .always(function() {
                s.removeClass("running");
                item_id = item_ids.shift();
                if (item_id) import_item(item_id);
            });
        }
    });
})(django.jQuery);

</script>

{% endblock %}
