{% extends "base.html" %}
{% load datetime_formatting %}
{% load truncatepath %}
{% load static_url %}

{% block title %}| Host reports{% if not static_generation %}: {{ current_page_results }} of {{ data.count }}{% endif %}{% endblock %}
{% block body %}
{% if not static_generation %}
  <div class="col-xl-6 offset-xl-3">
    <form action="{% url 'ui:host_index' %}" method="get">
    <div class="accordion" id="search_accordion">
      <div class="card">
        <div class="card-header" id="search_form">
          <button class="btn btn-link btn-lg" type="button" data-toggle="collapse" data-target="#collapse_search" aria-expanded="{{ expand_search }}" aria-controls="collapse_search">
            <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-search" viewBox="0 0 16 16">
              <path d="M11.742 10.344a6.5 6.5 0 1 0-1.397 1.398h-.001c.03.04.062.078.098.115l3.85 3.85a1 1 0 0 0 1.415-1.414l-3.85-3.85a1.007 1.007 0 0 0-.115-.1zM12 6.5a5.5 5.5 0 1 1-11 0 5.5 5.5 0 0 1 11 0z"></path>
            </svg>
            Search and filter
          </button>
        </div>
        <div id="collapse_search" class="{% if expand_search %}show{% endif %} collapse" aria-labelledby="search_form" data-parent="#search_accordion">
          <div class="card-body">
            <div class="form-row">
              <div class="form-group col-xl-8">
                <label for="name" {% if request.GET.name %}style="font-weight:bold;"{% endif %}>Name</label>
                <input type="text" class="form-control" id="name" name="name" placeholder="ex: localhost (or) hostname.fqdn" value="{{ search_form.name.value | default_if_none:'' }}">
              </div>
            </div>
            <div class="form-row">
              <div class="form-group col-xl-5">
                <div class="form-check form-check-inline">
                  <input class="form-check-input" type="checkbox" id="latest" value="false" name="latest" {{ checkbox_status }}>
                </div>
                <label class="align-middle" for="latest" title="Include every playbook report, not only the latest one for each host" {% if request.GET.latest or checkbox_status %}style="font-weight:bold;"{% endif %}>
                  Include all playbook reports
                </label>
              </div>
            </div>
            <div class="form-row">
              <div class="form-group col-xl-12">
                <label class="align-middle" for="status">Status: </label>
                <div class="form-check form-check-inline">
                  <input class="form-check-input" type="checkbox" id="ok__gt" value="0" name="ok__gt" {% if search_form.ok__gt.data %}checked{% endif %}/>
                  <label class="form-check-label" for="ok__gt">
                    {% include "partials/result_status_icon.html" with status='ok' %}
                  </label>
                </div>
                <div class="form-check form-check-inline">
                  <input class="form-check-input" type="checkbox" id="changed__gt" value="0" name="changed__gt" {% if search_form.changed__gt.data %}checked{% endif %}/>
                  <label class="form-check-label" for="changed__gt">
                    {% include "partials/result_status_icon.html" with status='changed' %}
                  </label>
                </div>
                <div class="form-check form-check-inline">
                  <input class="form-check-input" type="checkbox" id="failed__gt" value="0" name="failed__gt" {% if search_form.failed__gt.data %}checked{% endif %}/>
                  <label class="form-check-label" for="failed__gt">
                    {% include "partials/result_status_icon.html" with status='failed' %}
                  </label>
                </div>
                <div class="form-check form-check-inline">
                  <input class="form-check-input" type="checkbox" id="unreachable__gt" value="0" name="unreachable__gt" {% if search_form.unreachable__gt.data %}checked{% endif %}/>
                  <label class="form-check-label" for="unreachable__gt">
                    {% include "partials/result_status_icon.html" with status='unreachable' %}
                  </label>
                </div>
                <div class="form-check form-check-inline">
                  <input class="form-check-input" type="checkbox" id="skipped__gt" value="0" name="skipped__gt" {% if search_form.skipped__gt.data %}checked{% endif %}/>
                  <label class="form-check-label" for="skipped__gt">
                    {% include "partials/result_status_icon.html" with status='skipped' %}
                  </label>
                </div>
              </div>
            </div>
            <div class="form-row">
              <div class="col-xl-12">
                <div class="btn-group" role="group" aria-label="Filter by date">
                  <button type="submit" class="btn btn-primary">
                    Submit
                  </button>
                  <button type="submit" class="btn btn-outline-success" name="updated_after" value="{% past_timestamp with minutes=60 %}">Last 60 minutes
                  </button>
                  <button type="submit" class="btn btn-outline-success" name="updated_after" value="{% past_timestamp with hours=24 %}">Last 24 hours
                  </button>
                  <button type="submit" class="btn btn-outline-success" name="updated_after" value="{% past_timestamp with days=7 %}">Last 7 days
                  </button>
                  <button type="submit" class="btn btn-outline-success" name="updated_after" value="{% past_timestamp with days=30 %}">Last 30 days
                  </button>
                </div>
              </div>
            </div>
            {% if request.GET %}
              <br />
              <div class="form-row">
                <div class="col-xl-12">
                  <a class="btn btn-outline-danger" href="{% url 'ui:host_index' %}" role="button">Reset</a>
                </div>
              </div>
            {% endif %}
          </div>
        </div>
      </div>
    </div>
  </div>
{% endif %}

<div>
  {% if data.results %}
    {% if not static_generation %}
      {% include "partials/pagination.html" %}
    {% endif %}
    <div class="table-responsive">
      <table class="table table-sm table-hover" id="hosts">
        <thead>
          <tr style="height:50px;">
            <th class="text-center" title="Status of the playbook">Playbook</th>
            <th class="text-center" title="Link to the host report">Report</th>
            <th title="Last update">Last update {% include "partials/sort_by_date.html" with arg="updated" %}</th>
            <th title="Duration of the last playbook the host was involved in">Playbook duration</th>
            <th title="Name of the inventory host">Host name {% include "partials/sort_by_name.html" %}</th>
            <th title="Number of task results and their statuses">Last results</th>
            <th title="Host facts">Host facts</th>
          </tr>
        </thead>
        <tbody>
        {% for host in data.results %}
        {# /latesthosts/ returns the host under a nested host attribute -- /hosts/ returns it directly #}
        {% if host.host %}
          {% include "partials/host_index_row.html" with host=host.host %}
        {% else %}
          {% include "partials/host_index_row.html" with host=host %}
        {% endif %}
        {% endfor %}
        </tbody>
      </table>
    </div>
  {% else %}
    <br/>
    <div>
      {% if not request.GET %}
        <h2>No recorded results found.</h2>
        <h3>Record a playbook with the ara callback plugin enabled and try again.</h3>
      {% else %}
        <h3>No results found matching your query: try <a href="{% url 'ui:host_index' %}">clearing search filters</a>.</h3>
      {% endif %}
    </div>
  {% endif %}
</div>
</form>
{% endblock %}
