{% load debusine %}
{% load static %}
{% load users %}
{% load workflows %}
<div class="d-flex flex-column" id="{{ table.prefix }}workflow-list-table">
    {% if table.filters or table.orderings and not table.is_preview %}
        <div class="border-bottom">{% widget table.filters %}</div>
    {% endif %}
    {% if paginator %}
        <div class="flex-grow-1" id="workflow-list-table-rows">
            {% for workflow in paginator.page_obj %}
                <div class="p-2 border-bottom">
                    <div class="d-flex justify-content-between">
                        <div class="flex-grow-1">
                            <b>{{ workflow.workflow_display_name_parameters }}</b>
                        </div>
                        <div class="me-4">
                            {% workflow_runtime_status_small workflow %}
                            {% include "web/_work_request-result-small.html" with result=workflow.result only %}
                        </div>
                        <div>
                            {% spaceless %}
                                {% with work_request_result_count=work_request_result_counts|lookup:workflow.id %}
                                    {% include "web/_badge-count.html" with title="Work requests completed successfully" bg_class="success" count=work_request_result_count.success only %}
                                    <!-- -->-<!-- -->
                                    {% include "web/_badge-count.html" with title="Work requests terminated with a failure" bg_class="danger" count=work_request_result_count.failure only %}
                                {% endwith %}
                            {% endspaceless %}
                            {% spaceless %}
                                {% with work_request_status_count=work_request_status_counts|lookup:workflow.id %}
                                    {% include "web/_badge-count.html" with title="Pending work requests" bg_class="secondary" count=work_request_status_count.pending only %}
                                    <!-- -->-<!-- -->
                                    {% include "web/_badge-count.html" with title="Blocked work requests" bg_class="dark" count=work_request_status_count.blocked only %}
                                {% endwith %}
                            {% endspaceless %}
                        </div>
                    </div>
                    <div class="d-flex justify-content-between">
                        <div>
                            <a href="{{ workflow.get_absolute_url }}">#{{ workflow.id }}</a>
                            {% if workflow.started_at %}
                                Started at {{ workflow.started_at|date:"Y-m-d H:i" }}
                            {% else %}
                                Created at {{ workflow.created_at|date:"Y-m-d H:i" }}
                            {% endif %}
                            by {{ workflow.created_by|user_link }}
                        </div>
                        <div>
                            {% if workflow.completed_at %}Completed at {{ workflow.completed_at|date:"Y-m-d H:i" }},{% endif %}
                            Last activity {{ workflow.workflow_last_activity_at|date:"Y-m-d H:i" }}
                        </div>
                    </div>
                </div>
            {% empty %}
                <div class="p-2 border-bottom text-center">There are no workflows matching the current filters.</div>
            {% endfor %}
        </div>
        {% if paginator.has_page_navigation %}
            <div>{% widget paginator.page_navigation %}</div>
        {% endif %}
    {% else %}
        <div class="p-2 border-bottom text-center">There are no workflows to display.</div>
    {% endif %}
</div>
<script src="{% static "vendor/jquery/jquery.min.js" %}"></script>
<link href="{% static "vendor/select2.js/select2.min.css" %}"
      rel="stylesheet" />
<script src="{% static "vendor/select2.js/select2.min.js" %}"></script>
<script type="module">
    for (const field of ["statuses", "results", "workflow_templates", "started_by"])
        $(`#id_{{table.prefix}}filter-${field}`).select2({theme: "debusine"}).change(function(evt) {
            evt.target.form.submit();
        });
</script>
