{% load static %}
<h4>Crawlers</h4>
{% if crawlers %}
  <div class="results">
    <table id="result_list">
      <thead>
        <th><div class="text">No</div></th>
        <th><div class="text">PID</div></th>
        <th><div class="text">State</div></th>
      </thead>
      <tbody>
        {% for crawler in crawlers %}
          <tr>
              <td>{{ crawler.worker_no }}</td>
              <td>{{ crawler.pid }}</td>
              <td>{{ crawler.state }}</td>
          </tr>
        {% endfor %}
      </tbody>
    </table>
    <br/>
    {% if 'se.change_crawlerstats' in perms %}
      <form method="POST">
        {% csrf_token %}
        {% if pause %}
          <input type="submit" name="pause" value="Pause">
        {% else %}
          <input type="submit" name="resume" value="Resume">
        {% endif %}
      </form>
    {% endif %}
</div>
{% else %}
  No crawlers running.
{% endif %}

<h4>
  Crawl Queue
</h4>

<ul>
  <li>
    {{ queue_new_count }} document{{ queue_new_count|pluralize:"s" }} never crawled before
  </li>
  <li>
    {{ queue_sched_count }} document{{ queue_sched_count|pluralize:"s" }} to be recrawled
  </li>
  <li>
    <a href="{% url 'admin:se_document_changelist' %}?is_queued=yes&o=8">Documents queued</a>
  </li>
  <li>
    <a href="{% url 'admin:se_document_changelist' %}?o=-7">Documents last indexed</a>
  </li>
</ul>

<h4>
  Documents crawled
</h4>
{% if queue %}
  <div class="results">
    <table id="result_list" style="width: 100%">
      <thead>
        <th><div class="text">Url</div></th>
        <th><div class="text">Title</div></th>
        <th><div class="text">Status</div></th>
        <th><div class="text">Crawl next</div></th>
        <th><div class="text">Crawled last</div></th>
      </thead>
      <tbody>
        {% for doc in queue %}
          <tr {% if doc.pending %}class="{% if doc.worker_no is not None %}running{% elif doc.crawl_last is None or doc.crawl_next < now %}pending{% endif %}"{% endif %}>
            <td>
              <a href="{% url 'admin:se_document_change' doc.id %}">
                {{ doc.url }}
              </a>
            </td>
            <td>
              <a href="{% url 'admin:se_document_change' doc.id %}">
                {{ doc.title }}
              </a>
            </td>
            <td>
                {% if doc.error %}
                    <img src="{% static "admin/img/icon-no.svg" %}" alt="False">
                {% else %}
                    <img src="{% static "admin/img/icon-yes.svg" %}" alt="True">
                {% endif %}
            </td>
            {% if doc.worker_no is not None %}
               <td>In progress</td>
            {% elif doc.crawl_last is None or doc.crawl_next < now %}
               <td>Pending</td>
            {% elif doc.crawl_next and doc.pending %}
              <td title="{{ doc.crawl_next }}">{{ doc.crawl_next_human }}</td>
            {% else %}
              <td></td>
            {% endif %}
            {% if doc.crawl_last %}
              <td title="{{ doc.crawl_last }}">{{ doc.crawl_last_human }}</td>
            {% else %}
              <td></td>
            {% endif %}
          </tr>
        {% endfor %}
      </tbody>
    </table>
  </div>
{% else %}
  No document
{% endif %}
