{% extends "web/_base.html" %}
{% load static %}
{% load workflows %}
{% block title %}List of {{ workspace.name }} workflows{% endblock %}
{% block content %}
    <h1>{{ workspace.name }} workflows</h1>
    <p>
        This page lets you monitor <a href="https://freexian-team.pages.debian.net/debusine/explanation/concepts.html#workflows">workflows</a> started in the {{ workspace }} workspace.
    </p>
    <b>Filter</b>
    <div class="card">
        <div class="card-body">
            {% if filter_form.errors %}
                <div class="alert alert-danger" role="alert">
                    <p>There were issues with your filter criteria:</p>
                    <ul class="mb-0">
                        {% for field, errors in filter_form.errors.items %}
                            <li>
                                <strong>{{ field }}:</strong> {{ errors|join:", " }}
                            </li>
                        {% endfor %}
                    </ul>
                </div>
            {% endif %}
            <form method="get" action=".">
                <div class="row mb-2">
                    <div class="col-md-4">
                        <div class="form-group">
                            <label for="{{ filter_form.statuses.id_for_label }}">Statuses:</label>
                            <br>
                            {{ filter_form.statuses_runtime_statuses }}
                        </div>
                    </div>
                    <div class="col-md-4">
                        <div class="form-group">
                            <label for="{{ filter_form.results.id_for_label }}">Results:</label>
                            <br>
                            {{ filter_form.results }}
                        </div>
                    </div>
                    <div class="col-md-4">
                        <div class="form-group">
                            <label for="{{ filter_form.workflow_templates.id_for_label }}">Workflow templates:</label>
                            <br>
                            {{ filter_form.workflow_templates }}
                        </div>
                    </div>
                </div>
                <div class="row mb-2">
                    <div class="col-md-4">
                        <div class="form-group">
                            <label for="{{ filter_form.started_by.id_for_label }}">Started by:</label>
                            <br>
                            {{ filter_form.started_by }}
                        </div>
                    </div>
                    <div class="col-md-4">
                        <p></p>
                        <label>{{ filter_form.with_failed_work_requests }} With failed work requests</label>
                    </div>
                </div>
                <button type="submit" class="btn btn-primary">Search</button>
            </form>
        </div>
    </div>
    {% if workflow_list %}
        <table class="table table-hover" id="workflow-list-table">
            <thead>
                <tr>
                    <th>
                        ID
                        {% include "web/_order.html" with name="id" order=order asc=asc only %}
                    </th>
                    <th>
                        Workflow template
                        {% include "web/_order.html" with name="workflow_template" order=order asc=asc only %}
                    </th>
                    <th>
                        Status
                        {% include "web/_order.html" with name="status" order=order asc=asc only %}
                    </th>
                    <th>
                        Result
                        {% include "web/_order.html" with name="result" order=order asc=asc only %}
                    </th>
                    <th>
                        Started at
                        {% include "web/_order.html" with name="started_at" order=order asc=asc only %}
                    </th>
                    <th>
                        Completed at
                        {% include "web/_order.html" with name="completed_at" order=order asc=asc only %}
                    </th>
                    <th>Count of work requests</th>
                    <th title="Latest activity recorded for workflow’s work request">
                        Last activity
                        {% include "web/_order.html" with name="last_activity" order=order asc=asc only %}
                    </th>
                    <th>
                        Started by
                        {% include "web/_order.html" with name="started_by" order=order asc=asc only %}
                    </th>
                </tr>
            </thead>
            <tbody>
                {% for workflow in workflow_list %}
                    <tr>
                        <td>
                            <a href="{{ workflow.get_absolute_url }}">{{ workflow.id }}</a>
                        </td>
                        <td>{{ workflow.workflow_display_name_parameters }}</td>
                        <td>{% workflow_runtime_status_small workflow %}</td>
                        <td class>{% include "web/_work_request-result-small.html" with result=workflow.result only %}</td>
                        <td class="text-nowrap">{{ workflow.started_at|date:"Y-m-d H:i" }}</td>
                        <td class="text-nowrap">{{ workflow.completed_at|date:"Y-m-d H:i" }}</td>
                        <td class="text-nowrap">
                            {% spaceless %}
                                {% include "web/_badge-count.html" with title="Work requests completed successfully" bg_class="success" count=workflow.workflow_work_requests_success only %}
                                <!-- -->-<!-- -->
                                {% include "web/_badge-count.html" with title="Work requests terminated with a failure" bg_class="danger" count=workflow.workflow_work_requests_failure only %}
                            {% endspaceless %}
                            {% spaceless %}
                                {% include "web/_badge-count.html" with title="Pending work requests" bg_class="secondary" count=workflow.workflow_work_requests_pending only %}
                                <!-- -->-<!-- -->
                                {% include "web/_badge-count.html" with title="Blocked work requests" bg_class="dark" count=workflow.workflow_work_requests_blocked only %}
                            {% endspaceless %}
                        </td>
                        <td class="text-nowrap">{{ workflow.workflow_last_activity_at|date:"Y-m-d H:i" }}</td>
                        <td>{{ workflow.created_by }}</td>
                    </tr>
                {% endfor %}
            </tbody>
        </table>
        {% include "web/_pagination.html" %}
    {% else %}
        <p>No workflows.</p>
    {% endif %}
    <!-- Include Select2  -->
    <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>
            (function() {
            function main()
            {
                $("#id_statuses").select2({width: "80%"});
                $("#id_runtime_statuses").select2({width: "80%"});
                $("#id_results").select2({width: "80%"});
                $("#id_workflow_templates").select2({width: "80%"});
                $("#id_started_by").select2({width: "80%"});
            }

            document.addEventListener("DOMContentLoaded", evt => { main(); });
            })()
    </script>
{% endblock %}
