{% extends "base.html" %}
{% load form %}
{% load util_tags %}
{% block nav_results %}{% if not detail %}active{% endif %}{% endblock %}

{% block js %}
<script src="{{ STATIC_URL }}js/main.js"></script>
{% if filtered_results %}
<script type="text/javascript">
  $(document).ready(function(){
    {% for result_id in filtered_results %}
    load_and_expand_result({{ result_id }});
    {% endfor %}
});
</script>
{% endif %}
{% endblock %}

{% block content %}

<div id="toolbar" class="very-light-grey">
    <form id="clear-form" action="{{ request.path }}" method="GET">
        <input type="submit" class="btn btn-default" value="Clear" id="clear-btn" />
    </form>
    <form id="filter-form" class="form-inline" method="GET" action="{{ request.path }}" name="filterform">
        {{ filter_form.search }}
        <button class="btn btn-default fa fa-search search-button" type="submit"> </button>
        {{ filter_form.risk }}
        <button class="btn btn-default fa fa-filter filter-button global-filter" type="submit"> </button>
        {{ filter_form.hosts }}
        <a class="btn btn-danger global-filter" href="{% url 'delete-older' %}?host={{ request.GET.host }}" title="Keep only the last run for each host">Delete older runs</a>
    </form>
</div>

<form id="action-form" class="form-inline" method="POST" action="{{ request.path }}?{{ request.META.QUERY_STRING }}" name="actionform">{% csrf_token %}
<div id="action-form-toolbar" class="very-light-grey">
    <button type="submit" class="btn btn-danger" name="action" value="delete_selected" id="delete-selected-btn">Delete selected</button>
</div>

{# cellspacing is a workaround for firefox's 1px bug with collapse #}
{# http://stackoverflow.com/questions/1035706/firefox-1-pixel-bug-with-border-collapse-workaround #}
<table id="hostruns-table" cellspacing="0"> {# HOSTRUNS #}
    <thead>
        <tr>
            <th class="hostrun-select">
                <label for="hostrun-select-all">
                    <input type="checkbox" title="select all" id="hostrun-select-all"/>
                </label>
            </th>
            <th>
                Date
                <span class="fa fa-sort-amount-desc hostruns-table-header-date"></span>
            </th>
            <th>
                Completed
            </th>
            <th>
                In-place Upgrade Risk
            </th>
            <th>
                Failed
            </th>
            <th>
                Needs Action
            </th>
            <th>
                Needs Inspection
            </th>
            <th>
                Host Name
            </th>
            <th>
                Actions
            </th>
        </tr>
     </thead>
    <tbody>
    {% for hostrun in hostruns %}
        <tr id="hostrun-{{ hostrun.id }}-result-{{ hostrun.result.id }}" class="hostrun-row{% if hostrun.running %} disabled-row{% endif %}">
            <td class="hostrun-select">
                <label for="hostrun-{{ hostrun.id }}-select-{{ hostrun.result.id }}">
                    <input type="checkbox" name="runs" value="{{ hostrun.id }}" class="hostrun-select-input" id="hostrun-{{ hostrun.id }}-select-{{ hostrun.result.id }}"/>
                </label>
            </td>
            <td{% if hostrun.result %} class="hostrun-toggle"{% endif %}>
                {% if hostrun.result %}<span class="fa icon closed"></span>{% endif %}
                <span class="row-title">{{ hostrun.result.dt_finished|date:"Y-m-d H:i:s" }}</span>
            </td>
            <td{% if hostrun.result %} class="hostrun-toggle"{% endif %}>
                <span class="text">
                {% if hostrun.finished %}
                <span class="pficon pficon-ok green-text run-state-icon"></span>
                {% else %}
                    {% if hostrun.running %}
                    <span class="pficon pficon-running fa-spin run-state-icon"></span>
                    {% endif %}
                {% endif %}
                </span>
            </td>
            <td{% if hostrun.result %} class="hostrun-toggle"{% endif %}>
                {{ hostrun.risk.lower|capfirst }}
            </td>
            <td {% if hostrun.result %} class="hostrun-toggle"{% endif %}
                {% if hostrun.result.failed_test_count <= 0 %}class="disabled-row"{% else %}class="group-failed-tests-count bold"{% endif %}>
                {% if hostrun.result %}{{ hostrun.result.failed_test_count|default_if_none:"" }}{% endif %}
            </td>
            <td {% if hostrun.result %} class="hostrun-toggle"{% endif %}
                {% if hostrun.result.na_test_count <= 0 %}class="disabled-row"{% else %}class="group-na-tests-count bold"{% endif %}>
                {% if hostrun.result %}{{ hostrun.result.na_test_count|default_if_none:""|default_if_none:"" }}{% endif %}
            </td>
            <td {% if hostrun.result %} class="hostrun-toggle"{% endif %}
                {% if hostrun.result.ni_test_count <= 0 %}class="disabled-row"{% else %}class="group-ni-tests-count bold"{% endif %}>
                {% if hostrun.result %}{{ hostrun.result.ni_test_count|default_if_none:"" }}{% endif %}
            </td>
            <td{% if hostrun.result %} class="hostrun-toggle"{% endif %}>
                {% if hostrun.result %}{{ hostrun.result.hostname|default_if_none:"" }}{% else %}{{ hostrun.host.hostname }}{% endif %}
            </td>
            <td>
              <a href="{% url 'result-delete' hostrun.id %}" class="btn btn-danger btn-sm">Delete</a>
            </td>
        </tr>
        {% if hostrun.result %}
        <tr id="result-{{ hostrun.result.id }}-runhosts" class="group-row">
            {# append stuff from ajax here #}
        </tr>
        {% endif %}
    {% endfor %}
    </tbody>
</table>
</form>

{% if is_paginated %}
<ul class="pager">
{% if page_obj.has_previous %}
    <li class="previous">
        <a href="{% url 'results-list' %}?{% url_replace request 'page' page_obj.previous_page_number %}">
            <span class="i fa fa-angle-left"></span>
            Previous
        </a>
    </li>
{% endif %}
    <li>
        Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
    </li>
{% if page_obj.has_next %}
    <li class="next">
        <a href="{% url 'results-list' %}?{% url_replace request 'page' page_obj.next_page_number %}">
            Next
            <span class="i fa fa-angle-right"></span>
        </a>
    </li>
{% endif %}
</ul>
{% endif %}

{% endblock %}
