{% extends "base.html" %}

{% block content %}
    <ul class="nav nav-pills" role="tablist">
        <li class="nav-item">
            <a class="nav-link d-flex align-items-center" id="all-jobs-tab" href="{% url 'jobs:jobs' %}"
               aria-controls="all-jobs" role="tab" aria-selected="false">
                <span class="d-none d-sm-block">All jobs</span>
            </a>
        </li>
        <li class="nav-item">
            <a class="nav-link d-flex align-items-center active" id="job-history-tab" href="{% url 'jobs:job-history' %}"
               aria-controls="job-history" role="tab" aria-selected="true">
                <span class="d-none d-sm-block">Job history</span>
            </a>
        </li>
    </ul>

    {% if job_list %}
        <div class="card my-4">
            <div class="card-body">
                <form method="GET" novalidate>
                    <div class="row">
                        <div class="col form-group mr-4">
                            <label for="id_created_by" class="mr-2">Search by support user</label>
                            {{ filters_form.created_by }}
                        </div>
                        <div class="col form-group mr-4">
                            <label for="id_job_name_search" class="mr-2">Search by job name</label>
                            {{ filters_form.job_name_search }}
                        </div>
                    </div>
                    <div class="inline-block mb-2">
                        <button class="btn btn-primary mr-4" type="submit">Apply filters</button>
                        <a href="{% url 'jobs:job-history' %}{% if page_obj.paginator.per_page %}?paginate_by={{ page_obj.paginator.per_page }}{% endif %}">Clear filters</a>
                    </div>
                </form>
            </div>
        </div>
        <div class="card">
            <table class="table table-striped table-borderless">
                <thead class="thead-light">
                    <tr>
                        <th>Job</th>
                        <th>Created at</th>
                        <th>Created by</th>
                        <th>Started at </th>
                        <th>Finished at</th>
                        <th>Cancelled at</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    {% for job in job_list %}
                        <tr>
                            <td>
                                <code>{{ job.name }}</code>
                            </td>
                            <td>
                                {{ job.created_at }}
                            </td>
                            <td>
                                {{ job.created_by }}
                            </td>
                            <td>
                                {% if job.started_at %}
                                    {{ job.started_at }}
                                {% else %}
                                    Not applicable
                                {% endif %}
                            </td>
                            <td>
                                {% if job.finished_at %}
                                    {{ job.finished_at }}
                                {% else %}
                                    Not applicable
                                {% endif %}
                            </td>
                            <td>
                                {% if job.cancelled_at %}
                                    {{ job.cancelled_at }}
                                {% else %}
                                    Not applicable
                                {% endif %}
                            </td>
                            <td>
                                <a href="{% url 'jobs:job-status' job.pk %}">View status</a>
                            </td>
                        </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    {% else %}
        <p>No job run history found.</p>
    {% endif %}

{% endblock %}
