{% extends "skeleton.html" %}
{% block page_title %}Queue Viewer: {{ queue_name }}{% endblock %}

{% block body %}
  {% if error %}
  <div class="errorbox">
    {{ error }}
  </div>
  {% endif %}

<h3>Tasks for Queue: {{ queue_name }}</h3>

{% if tasks %}
  <p>
    About {{ queue_info.tasks_in_queue }} tasks in <strong>{{ queue_name }}</strong> queue.
    {% if is_push_queue %}
      Push the 'Run' button to execute a task manually.
    {% endif %}
  </p>

  {% if queue_info.tasks_in_queue > tasks|length %}
    (first {{ tasks|length }} viewable)
  {% endif %}

  {% set num_tasks_per_page = 100 %}
  {% set num_pages =
      (tasks|length / num_tasks_per_page)|round(method='ceil')|int %}
  {% set page = page|int(default=1) %}
  {% if page > num_pages %}
    {% set page = num_pages %}
  {% elif page < 1 %}
    {% set page = 1 %}
  {% endif %}
  {% set first_task_index = (page - 1) * num_tasks_per_page %}
  {% set last_task_index = first_task_index + num_tasks_per_page %}

  <table class="ae-table">
    <thead>
      <tr>
        <th>Task Name</th>
        <th>ETA (UTC)</th>
        <th>Method</th>
        <th>URL</th>
        <th></th>
      </tr>
    </thead>
    <tbody>
      {% for task in tasks[first_task_index:last_task_index] %}
        <tr>
          <td>
            {{ task.name }}
          </td>
          <td>
            {% if task.eta_usec != 0 %}
              <div>{{ task.eta }}</div>
              <div>({{ task.eta_delta }})</div>
            {% endif %}
          </td>
          <td>
            {{ task.method }}
          </td>
          <td>
            {{ task.url }}
          </td>
          <td>
            <form action="{{ request.uri }}" method="POST">
            <input type="hidden" name="xsrf_token" value="{{ xsrf_token }}"/>
            <input type="hidden" name="task_name" value="{{ task.name }}"/>
            <input type="hidden" name="page" value="{{ page }}"/>
            <input type="submit" class="ae-button" name="action:runtask" value="Run"/>
            <input type="submit" class="ae-button" name="action:deletetask" value="Delete"/>
            </form>
          </td>
        </tr>
      {% endfor %}
    </tbody>
  </table>
  {% if num_pages > 1 %}
    <div class="ae-paginator">
      {% if page != 1 %}
        <a href="?page={{ page - 1 }}">Previous</a>
      {% endif %}

      {% for page_count in range(1, num_pages+1) %}
        {% if page_count == page %}
          {{ page_count }}
        {% else %}
          <a href="?page={{ page_count }}">{{ page_count }}</a>
        {% endif %}
      {% endfor %}

      {% if page != num_pages %}
        <a href="?page={{ page + 1 }}">Next</a>
      {% endif %}
    </div>
  {% endif %}


{% else %}
  This queue doesn't contain any tasks.
{% endif %}
{% endblock %}
