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

{% if not static_generation %}
  <div class="col-xl-6 offset-xl-3">
    <form action="{% url 'ui: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-10">
                <label for="path" {% if request.GET.path %}style="font-weight:bold;"{% endif %}>Path</label>
                <input type="text" class="form-control" id="path" name="path" placeholder="ex: /path/to/playbook.yml (or) playbook.yml" value="{{ search_form.path.value | default_if_none:'' }}">
              </div>
            </div>
            <div class="form-row">
              <div class="form-group col-xl-5">
                <label for="ansible_version" {% if request.GET.ansible_version %}style="font-weight:bold;"{% endif %}>Ansible version</label>
                <input type="text" class="form-control" id="ansible_version" name="ansible_version" placeholder="ex: 2.10.5 (or) 2.10" value="{{ search_form.ansible_version.value | default_if_none:'' }}">
              </div>
              <div class="form-group col-xl-5">
                <label for="controller" {% if request.GET.controller %}style="font-weight:bold;"{% endif %}>Ansible controller</label>
                <input type="text" class="form-control" id="controller" name="controller" placeholder="ex: host.domain.tld (or) host" value="{{ search_form.controller.value | default_if_none:'' }}">
              </div>
            </div>
            <div class="form-row">
              <div class="form-group col-xl-5">
                <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: install server" value="{{ search_form.name.value | default_if_none:'' }}">
              </div>
              <div class="form-group col-xl-5">
                <label for="label" {% if request.GET.label %}style="font-weight:bold;"{% endif %}>Label</label>
                <input type="text" class="form-control" id="label" name="label" placeholder="ex: check:False" value="{{ search_form.label.value | default_if_none:'' }}">
              </div>
            </div>
            <div class="form-row">
              <div class="form-group col-xl-12">
                <label class="align-middle" for="status" {% if request.GET.status %}style="font-weight:bold;"{% endif %}>Status: </label>
                {% for value, text in search_form.status.field.choices %}
                  {% if value != "unknown" %}
                    <div class="form-check form-check-inline {% if value == 'completed' %}text-success{% elif value == 'failed' %}text-danger{% elif value == 'running' %}text-info{% else %}text-warning{% endif %}">
                      <input class="form-check-input" type="checkbox" id="{{ value }}" value="{{ value }}" name="status" {% if value in search_form.status.data %}checked{% endif %}>
                      <label class="form-check-label" for="{{ value }}">
                        {% include "partials/status_icon.html" with status=value %} {{ value }}
                      </label>
                    </div>
                  {% endif %}
                {% endfor %}
              </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="started_after" value="{% past_timestamp with minutes=60 %}">Last 60 minutes
                  </button>
                  <button type="submit" class="btn btn-outline-success" name="started_after" value="{% past_timestamp with hours=24 %}">Last 24 hours
                  </button>
                  <button type="submit" class="btn btn-outline-success" name="started_after" value="{% past_timestamp with days=7 %}">Last 7 days
                  </button>
                  <button type="submit" class="btn btn-outline-success" name="started_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:index' %}" role="button">Reset</a>
                </div>
              </div>
            {% endif %}
          </div>
        </div>
      </div>
    </div>
  </div>
{% endif %}

<div>
  {% if not static_generation %}
    {% include "partials/pagination.html" %}
  {% endif %}
  <div class="table-responsive">
    <table class="table table-sm table-hover" id="playbooks">
      <thead>
        <tr style="height:50px;">
          <th class="text-center" title="Status of the playbook run">Status</th>
          <th class="text-center" title="Link to the playbook report">Report</th>
          <th title="Date at which the playbook started">{% include "partials/sort_by_date.html" %}</th>
          <th class="text-nowrap" title="Duration of the playbook (HH:MM:SS.ms)">{% include "partials/sort_by_duration.html" %}</th>
          <th class="text-center" title="Number of hosts involved in the playbook">Hosts</th>
          <th class="text-center" title="Number of tasks run in the playbook">Tasks</th>
          <th class="text-center" title="Number of results recorded from the playbook">Results</th>
          <th class="text-center" title="Version of Ansible that ran the playbook">Ansible</th>
          <th class="text-center" title="Host that ran the playbook">Controller</th>
          <th title="Name (provided by ara_playbook_name) or path to the playbook file">Name (or path)</th>
          <th class="text-center" title="CLI arguments given to the ansible command">CLI</th>
          <th title="Labels applied to the playbook (provided by ara_playbook_labels)">Labels</th>
        </tr>
      </thead>
      <tbody>
      {% for playbook in data.results %}
        <tr>
          <td class="text-center">
            {% include "partials/playbook_status_icon.html" with status=playbook.status %}
          </td>
          <td class="text-center">
            {% url 'ui:playbook' playbook.id as playbook_url %}
            <a role="button" class="btn btn-outline-primary btn-link btn-sm" href="{% static_url playbook_url %}{% if playbook.status == 'failed' %}?status=failed&status=unreachable#results{% endif %}">
              <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-box-arrow-up-right" viewBox="0 0 16 16">
                <path fill-rule="evenodd" d="M8.636 3.5a.5.5 0 0 0-.5-.5H1.5A1.5 1.5 0 0 0 0 4.5v10A1.5 1.5 0 0 0 1.5 16h10a1.5 1.5 0 0 0 1.5-1.5V7.864a.5.5 0 0 0-1 0V14.5a.5.5 0 0 1-.5.5h-10a.5.5 0 0 1-.5-.5v-10a.5.5 0 0 1 .5-.5h6.636a.5.5 0 0 0 .5-.5z"></path>
                <path fill-rule="evenodd" d="M16 .5a.5.5 0 0 0-.5-.5h-5a.5.5 0 0 0 0 1h3.793L6.146 9.146a.5.5 0 1 0 .708.708L15 1.707V5.5a.5.5 0 0 0 1 0v-5z"></path>
              </svg>
            </a>
          </td>
          <td class="text-nowrap">{{ playbook.started | format_date }}</td>
          <td class="text-nowrap">{{ playbook.duration | format_duration }}</td>
          <td class="text-center">{{ playbook.items.hosts }}</td>
          <td class="text-center">{{ playbook.items.tasks }}</td>
          <td class="text-center">{{ playbook.items.results }}</td>
          <td class="text-center">
            {% if not static_generation %}
              <a href="{% url 'ui:index' %}?ansible_version={{ playbook.ansible_version }}">{{ playbook.ansible_version }}</a>
            {% else %}
              {{ playbook.ansible_version }}
            {% endif %}
          </td>
          <td class="text-center">
            {% if not static_generation %}
              <a href="{% url 'ui:index' %}?controller={{ playbook.controller }}">{{ playbook.controller }}</a>
            {% else %}
              {{ playbook.controller }}
            {% endif %}
          </td>
          <td title="{{ playbook.path }}">
            {% if not static_generation %}
              {% if playbook.name is not None %}
                <a href="{% url 'ui:index' %}?name={{ playbook.name }}">{{ playbook.name }}</a>
              {% else %}
                <a href="{% url 'ui:index' %}?path={{ playbook.path }}">{{ playbook.path | truncatepath:50 }}</a>
              {% endif %}
            {% else %}
              {% if playbook.name is not None %}
                {{ playbook.name }}
              {% else %}
                {{ playbook.path | truncatepath:50 }}
              {% endif %}
            {% endif %}
          </td>
          <td class="text-center">
            <button type="button" class="btn btn-outline-secondary btn-link btn-sm" data-toggle="modal" data-target="#cli_{{ playbook.id }}">
              <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-terminal" viewBox="0 0 16 16">
                <path d="M6 9a.5.5 0 0 1 .5-.5h3a.5.5 0 0 1 0 1h-3A.5.5 0 0 1 6 9zM3.854 4.146a.5.5 0 1 0-.708.708L4.793 6.5 3.146 8.146a.5.5 0 1 0 .708.708l2-2a.5.5 0 0 0 0-.708l-2-2z"/>
                <path d="M2 1a2 2 0 0 0-2 2v10a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V3a2 2 0 0 0-2-2H2zm12 1a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H2a1 1 0 0 1-1-1V3a1 1 0 0 1 1-1h12z"/>
              </svg>
            </button>
            <div class="modal fade" id="cli_{{ playbook.id }}" tabindex="-1" aria-labelledby="cli_{{ playbook.id }}" aria-hidden="true">
              <div class="modal-dialog modal-lg">
                <div class="modal-content">
                  <div class="modal-header">
                    <h3 class="modal-title">CLI arguments</h3>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                      <span aria-hidden="true">&times;</span>
                    </button>
                  </div>
                  <div class="modal-body">
                    <div>
                      <div class="row">
                        <div class="col-md-12">
                          {% if playbook.name is not None %}
                            <h5>{{ playbook.name }} @ {{ playbook.started | format_date }}</h5>
                          {% else %}
                            <h5>{{ playbook.path | truncatepath:50 }} @ {{ playbook.started | format_date }}</h5>
                          {% endif %}
                        </div>
                      </div>
                      <div class="row">
                        <div class="col-md-12">
                          {% include "partials/cli_arguments.html" with arguments=playbook.arguments %}
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </td>
          <td class="col-md-2">
            {% for label in playbook.labels %}
              {% if not static_generation %}
                <a href="{% url 'ui:index' %}?label={{ label.name }}" class="btn btn-outline-success ara-label">{{ label.name }}</a>
              {% else %}
                <span class="btn btn-outline-success ara-label">{{ label.name }}</span>
              {% endif %}
            {% endfor %}
          </td>
        </tr>
      {% endfor %}
      </tbody>
    </table>
  </div>
</div>
</form>
{% endblock %}
