{% extends "agenda_culturel/page.html" %}

{% block title %}{% block og_title %}Importations récurrentes
{% if status %}{{ status }}{% endif %}{% endblock %}{% endblock %}

{% load utils_extra %}
{% load rimports_extra %}
{% load cat_extra %}
{% load static %}

{% block entete_header %}
    {% css_categories %}
    <script src="{% static 'choicejs/choices.min.js' %}"></script>
{% endblock %}
{% block configurer-bouton %}{% endblock %}

{% block sidemenu-bouton %}
    <li><a href="#contenu-principal" aria-label="Aller au contenu">{% picto_from_name "chevron-up" %}</a></li>
    <li><a href="#sidebar" aria-label="Aller au menu latéral">{% picto_from_name "chevron-down" %}</a></li>
{% endblock %}

{% block content %}
<div class="grid two-columns">
<article>
    <header>
        <div class="slide-buttons">
            {% if not status %}
                <a href="{% url 'run_all_rimports' %}" role="button">Exécuter tout {% picto_from_name "play-circle" %}</a>
                <a href="{% url 'run_all_fb_rimports' %}" role="button">Exécuter depuis FB {% picto_from_name "play-circle" %}</a>
                <br>
            {% else %}
                {% if status == "failed" or status == "canceled" %}
                <a href="{% url 'run_all_rimports_status' status %}" role="button">Relancer les imports {% if status == "failed" %}échoués{% else %}annulés{% endif %} {% picto_from_name "play-circle" %}</a>
                {% endif %}
            {% endif %}
            <a href="{% url 'add_rimport'%}" role="button">Ajouter {% picto_from_name "plus-circle" %}</a>
        </div>
        <h1>Importations récurrentes {% if status %}<em>{{ status }}</em>{% endif %}</h1>
        <p>Résumé&nbsp;: {% include "agenda_culturel/rimports-info-inc.html" with all=0 %}</p>

        <h2>Filtrer</h2>
        <form method="get" class="form django-form">
            {{ filter.form.as_div }}<br />
            <button type="submit">Filtrer</button>
        </form>

    </header>

    <table role="grid">
        <thead>
            <tr>
                <th>Id</th>
                <th>Nom</th>
                <th>Récurrence</th>
                <th>Nb d'imports</th>
                <th>Dernier</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
    {% for obj in paginator_filter %}
        <tr>
            <td>{{ obj.pk }}</a></td>
            <td>{{ obj.name }}</a></td>
            <td>{{ obj.get_recurrence_display }}</td>
            <td>{{ obj.nb_imports }}</td>
            <td>
                {% if obj.nb_imports > 0 %}
                    {% with imp=obj.last_import %}
                        <span{% if imp.status == "failed" %} data-tooltip="{{ imp.error_message }}"{% endif %}>
                            {% if imp.status == "running" %}<em>{{ imp.status }}</em>{% else %}{{ imp.status }} {% if imp.status == "success" %} ({{ imp.nb_initial }}){% endif %}{% endif %}
                        </span>
                    {% endwith %}
                {% endif %}
            </td>
            <td>
                <div  class="buttons">
                    {% if not obj.last_import or obj.last_import.status != "running" %}
                        <a href="{% url 'run_rimport' obj.pk %}" role="button">Exécuter</a>
                    {% else %}
                    <a href="{% url 'cancel_import' obj.last_import.pk %}" role="button">Annuler</a>
                    {% endif %}
                    <a href="{% url 'view_rimport' obj.pk %}" role="button">Consulter</a>
                    <a href="{% url 'edit_rimport' obj.pk %}" role="button">Modifier</a>
                </div>
            </td>
        </tr>
    {% endfor %}
        </tbody>
    </table>

    <footer>
    <span>
        {% include 'agenda_culturel/paginator.html' %}
    </span>
</footer>
</article>

{% include "agenda_culturel/side-nav.html" with current="rimports" %}
</div>

<script>
    const imports = document.querySelector('#id_name');
    const choices_imports = new Choices(imports, {
            placeholderValue: 'Chercher un import par son nom',
            allowHTML: true,
            delimiter: ',',
            removeItemButton: true,
            shouldSort: false,
        }

    );
    </script>
    

{% endblock %}