{% load django_tables2 %}
{% load tables %}
{% block content %}

{% load i18n %}
{% block table %}
<div class="row">
  <div class="col-md-6">
  <form class="form-inline" id="{{ table.prefix }}search_form" action="{% querystring %}#{{ table.prefix|default:"table" }}" method="GET">
    {% for key,value in request.GET.items %}
      {% if key != table.prefix|add:"length" %}
        <input type="hidden" name="{{ key }}" value="{{ value }}">
      {% endif %}
    {% endfor %}
    <div class="form-group">
      Show <select class="form-control input-sm" id="{{ table.prefix }}length" name="{{ table.prefix }}length" onchange="submit_search_form('{{ table.prefix }}');">
        {{ table|get_length_select:request.GET }}
      </select> entries
    </div>
    </form>
  </div>
{%  if table.has_searches %}
  <div class="col-md-5">
    <form class="form-inline pull-right" action="{% querystring "search"="" %}#{{ table.prefix|default:"table" }}" method="GET">
      {% for key,value in request.GET.items %}
        {% if key != table.prefix|add:"search" %}
          <input type="hidden" name="{{ key }}" value="{{ value }}">
        {% endif %}
      {% endfor %}
      <div class="form-group">
        <label>Search</label>
        <input type="text" name="{{ table.prefix }}search" value="{{ table|get_prefix_search:request.GET }}" placeholder="Press ? for search help">
      </div>
    </form>
  </div>
  <div class="col-md-1">
    <button class="btn btn-info btn-xs" data-toggle="modal" data-target="#{{ table.prefix }}SearchModal">?</button>
    <div class="modal fade" id="{{ table.prefix }}SearchModal" tabindex="-1" role="dialog" aria-labelledby="{{ table.prefix }}SearchModalLabel" aria-hidden="true">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            <h4 class="modal-title" id="{{ table.prefix }}SearchModalLabel">Search help</h4>
          </div>
          <div class="modal-body">
            <p>Available methods and fields</p>
            <dl class="dl-horizontal">
              {% if table.Meta.searches %}
                <dt>Simple text</dt>
                <dd>{{ table.get_simple_text_searches|join:", " }} <abbr
                title="text entered in the search box will be used in case sensitive matches - any field can match.">(?)</abbr></dd>
              {% endif %}
              {% if table.Meta.queries %}
                <dt>Custom query text</dt>
                <dd>{{ table.get_query_searches|join:", " }} <abbr
                title="add the filter and a value in the query string, e.g. requested_device_type=qemu - all rows must match">(?)</abbr></dd>
              {% endif %}
              {% if table.Meta.times %}
                <dt>Time based</dt>
                <dd>{{ table.get_time_searches|join:", " }} <abbr
                title="add the filter and a number in the query string, e.g. end_time=48 - rows will be filtered where difference between now and filter cannot be larger than passed number of hours.">(?)</abbr></dd>
              {% endif %}
            </dl>
          </div>
        </div>
      </div>
    </div>
  </div>
{% endif %}
</div>

<table id="{{ table.prefix|default:"table" }}" class="table table-hover">
    <colgroup>
    {% for column in table.columns %}
      <col{% if column.is_ordered %} class="sorted"{% endif %}>
    {% endfor %}
    </colgroup>
    {% block table.thead %}
    <thead>
        <tr>
        {% for column in table.columns %}
            {% if column.orderable %}
            <th {{ column.attrs.th.as_html }}>
              <a href="{% querystring table.prefixed_order_by_field=column.order_by_alias.next %}#{{ table.prefix|default:"table" }}">{{ column.header }}
                <small>
                {% if column.is_ordered %}
                  {% if column.order_by_alias.0 == "-" %}
                  <span class="glyphicon glyphicon-sort-by-attributes-alt"></span>
                  {% else %}
                  <span class="glyphicon glyphicon-sort-by-attributes"></span>
                  {% endif %}
                {% else %}
                  <span class="glyphicon glyphicon-sort"></span>
                {% endif %}
                </small>
              </a>
            </th>
            {% else %}
            <th {{ column.attrs.th.as_html }}>{{ column.header }}</th>
            {% endif %}
        {% endfor %}
        </tr>
    </thead>
    {% endblock table.thead %}
    {% block table.tbody %}
    <tbody>
        {% for row in table.page.object_list|default:table.rows %} {# support pagination #}
        {% block table.tbody.row %}
        <tr class="{% cycle "odd" "even" %}">
            {% for column, cell in row.items %}
                <td {{ column.attrs.td.as_html }}>{{ cell }}</td>
            {% endfor %}
        </tr>
        {% endblock table.tbody.row %}
        {% empty %}
        {% if table.empty_text %}
        {% block table.tbody.empty_text %}
        <tr><td colspan="{{ table.columns|length }}"><div style="text-align: center">{{ table.empty_text }}</div></td></tr>
        {% endblock table.tbody.empty_text %}
        {% endif %}
        {% endfor %}
    </tbody>
    {% endblock table.tbody %}
    {% block table.tfoot %}
    <tfoot></tfoot>
    {% endblock table.tfoot %}
</table>
{% endblock table %}

{% if table.page %}
{% block pagination %}
{% if table.page.has_previous or table.page.has_next %}
<ul class="pager">
  {% block pagination.previous %}
    {% if table.page.has_previous %}
    <li class="previous">
      <a href="{% querystring table.prefixed_page_field=table.page.previous_page_number %}#{{ table.prefix|default:"table" }}">
    {% else %}
    <li class="previous disabled">
      <a>
    {% endif %}
      <span class="glyphicon glyphicon-backward"></span> {% trans "Previous" %}</a>
    </li>
  {% endblock pagination.previous %}

  {% block pagination.current %}
    <li>Page {{ table.page.number }} / {{ table.paginator.num_pages }}</li>
  {% endblock pagination.current %}


  {% block pagination.cardinality %}
    <li>(showing {{ table.page.object_list|length }} of {{ table.page.paginator.count }})</li>
  {% endblock %}

  {% block pagination.next %}
    {% if table.page.has_next %}
    <li class="next">
      <a href="{% querystring table.prefixed_page_field=table.page.next_page_number %}#{{ table.prefix|default:"table" }}">
    {% else %}
    <li class="next disabled">
      <a href="#">
    {% endif %}
      {% trans "Next" %} <span class="glyphicon glyphicon-forward"></span></a>
  </li>
  {% endblock pagination.next %}
</ul>
{% endif %}
{% endblock pagination %}
{% endif %}
{% endblock %}
