{% extends "base.html" %}
{% block title %}Failed jobs{% endblock %}
{% load colour_table_rows %}
{% load naturaltime from humanize %}
{% load static %}

{% block body %}
    {% if queue %}
        <div class="column">
            <div class="row">
                <div class="col-md-12 text-center">
                    <h2>Failed Jobs</h2>
                </div>
            </div>
            {% if message %}
                <div class="alert alert-danger word-wrap" role="alert">
                    <i class="fa fa-exclamation fa-exclamation-circle fa-lg"></i>
                    {{ message }}
                </div>
            {% endif %}
            <div>
                <label for="runAction" hidden>Select action to apply to selected runs</label>
                <select id="runAction" style="float: left;">
                    <option value="default">Select action to apply to selected runs</option>
                    <option value="rerun">Re-run</option>
                    <option value="hide">Hide</option>
                </select>
                <form id="actionForm" method="POST" action="{% url 'runs:failed' %}"
                      style="float: left; padding-left: 5px;">
                    <input type='hidden' name='csrfmiddlewaretoken' value='{{ csrf_token }}'/>
                    <input type="hidden" name="selectedRuns">
                    <input type="hidden" name="action">
                    <input type="submit" id="runActionButton" value="Apply">
                </form>
            </div>
            <table class="table table-striped table-bordered">
                <thead>
                    <tr>
                        <th>
                            <label for="selectAllRuns" hidden>Select all runs</label>
                            <input type='checkbox' id="selectAllRuns">
                        </th>
                        <th>Run Number</th>
                        <th>Instrument</th>
                        <th>Message</th>
                        <th>Submitted</th>
                    </tr>
                </thead>
                <tbody>
                    {% for job in queue %}
                        {% if job.retry_run %}
                            <tr class="{% colour_table_row job.retry_run.status.value_verbose %}">
                                {% else %}
                            <tr class="{% colour_table_row job.status.value_verbose %}">
                        {% endif %}
                        <td>
                            <label for="selectRun{{ job.run_number }}-{{ job.run_version }}" hidden>
                                Select run
                            </label>
                            <input type='checkbox'
                                   id="selectRun{{ job.run_number }}-{{ job.run_version }}"
                                   class='runCheckbox'
                                   data-run_number='{{ job.run_number }}'
                                   data-run_version='{{ job.run_version }}'
                                   data-rb_number='{{ job.experiment.reference_number }}'>
                        </td>
                        <td>
                            <a href="{% url 'runs:summary' instrument_name=job.instrument.name run_number=job.run_number run_version=job.run_version %}">
                                {{ job.title }}</a>
                        </td>
                        <td>{{ job.instrument.name }}</td>
                        <td style="width:600px;"><strong>{{ job.message }}</strong></td>
                        <td title="{{ job.created|date:'SHORT_DATETIME_FORMAT' }}">{{ job.created|naturaltime }}</td>
                                           {% endfor %}
                </tbody>
            </table>
        </div>
    {% else %}
        <div class="row">
            <div class="col-md-12 text-center">
                <h2>No failed jobs.</h2>
            </div>
        </div>
    {% endif %}
{% endblock %}

{% block scripts %}
    <script src='{% static "javascript/runs_failed.js" %}'></script>
{% endblock %}
