{% extends "sidebar.html" %}
{% load i18n static filters %}

{% block content %}
<main id="rules--page" class="main" data-render-sidebar=true>
  <div class="main__container">
    <form class="form rules-form">
      {% csrf_token %}

      <section class="section form__section form__section--actions">
        <div class="form__actions">
          <a class="link button button--confirm"
            href="{% url 'news:collection:feed-create' %}">{% trans "Add a feed" %}</a>
          <a class="link button button--confirm"
            href="{% url 'news:collection:import' %}">{% trans "Import feeds" %}</a>
        </div>
      </section>

      <section class="section form__section form__section--actions">
        <fieldset class="fieldset form__fieldset">
          <input type="submit" class="button button--primary" formaction="{% url 'news:collection:rules-enable' %}"
            formmethod="post" value="{% trans " Enable" %}" />
          <input type="submit" class="button button--primary" formaction="{% url 'news:collection:rules-disable' %}"
            formmethod="post" value="{% trans " Disable" %}" />
          <input type="submit" class="button button--error" formaction="{% url 'news:collection:rules-delete' %}"
            formmethod="post" value="{% trans " Delete" %}" />
        </fieldset>
      </section>

      <section class="section form__section">
        <table class="table rules-table" border="0" cellspacing="0">
          <thead class="table__header rules-table__header">
            <tr class="table__row rules-table__row">
              <th class="table__heading rules-table__heading--select">
                {% include "components/form/checkbox.html" with id="select-all" data_input="rules" id_for_label="select-all" %}
              </th>
              <th class="table__heading rules-table__heading rules-table__heading--name">
                {% trans "Name" %}
              </th>
              <th class="table__heading rules-table__heading rules-table__heading--category">
                {% trans "Category" %}
              </th>
              <th class="table__heading rules-table__heading rules-table__heading--url">
                {% trans "URL" %}
              </th>
              <th class="table__heading rules-table__heading rules-table__heading--succeeded">
                {% trans "Successfuly ran" %}
              </th>
              <th class="table__heading rules-table__heading rules-table__heading--enabled">
                {% trans "Enabled" %}
              </th>
            </tr>
          </thead>
          <tbody class="table__body rules-table__body">
            {% for rule in rules %}
            <tr class="table__row {% if rule.failed %}table__row--error{% endif %} rules-table__row">
              <td class="table__item rules-table__item--select">
                {% with rule|id_for_label:"rules" as id_for_label %}
                {% include "components/form/checkbox.html" with name="rules" value=rule.pk id=id_for_label
                id_for_label=id_for_label %}
                {% endwith %}
              </td>

              <td class="table__item rules-table__item rules-table__item--name" title="{{ rule.name }}">
                <a class="link" href="{{ rule.update_url }}">
                  {{ rule.name }}
                </a>
              </td>

              <td class="table__item rules-table__item rules-table__item--category" title="{{ rule.category.name }}">
                {% if rule.category %}
                <a class="link" href="{% url 'news:core:category-update' pk=rule.category.pk %}">
                  {{ rule.category.name }}
                </a>
                {% endif %}
              </td>

              <td class="table__item rules-table__item rules-table__item--url" title="{{ rule.source_url }}">
                <a class="link" href="{{ rule.source_url }}" target="_blank" rel="noopener noreferrer">
                  {{ rule.source_url }}
                </a>
              </td>

              <td class="table__item rules-table__item rules-table__item--failed">
                {% if rule.failed %}
                <i class="fas fa-exclamation-triangle"></i>
                {% else %}
                <i class="fas fa-check"></i>
                {% endif %}
              </td>

              <td class="table__item rules-table__item rules-table__item--enabled">
                {% if rule.enabled %}
                <i class="fas fa-check"></i>
                {% else %}
                <i class="fas fa-pause"></i>
                {% endif %}
              </td>
            </tr>
            {% endfor %}
          </tbody>
        </table>
      </section>
    </form>

    <div class="table__footer">
      <div class="pagination">
        <span class="pagination__previous">
          {% if page_obj.has_previous %}
          <a class="link button" href="?page=1">{% trans "first" %}</a>
          <a class="link button" href="?page={{ page_obj.previous_page_number }}">
            {% trans "previous" %}
          </a>
          {% endif %}
        </span>

        <span class="pagination__current">
          {% blocktrans with current_number=page_obj.number total_count=page_obj.paginator.num_pages %}
          Page {{ current_number }} of {{ total_count }}
          {% endblocktrans %}
        </span>

        <span class="pagination__next">
          {% if page_obj.has_next %}
          <a class="link button" href="?page={{ page_obj.next_page_number }}">
            {% trans "next" %}
          </a>

          <a class="link button" href="?page={{ page_obj.paginator.num_pages }}">
            {% trans "last" %}
          </a>
          {% endif %}
        </span>
      </div>
    </div>
  </div>
</main>
{% endblock %}
