{% 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.iteritems %}
    <input type="hidden" name="{{ table.prefix }}{{ key }}" value="{{ value }}">
    {% endfor %}
    <input type="hidden" name="{{ table.prefix }}search" value="{{ request.GET.search }}">
    {% if request.GET.end %}<input type="hidden" name="{{ table.prefix }}end" value="{{ request.GET.end }}">{% endif %}
    {% if request.GET.start %}<input type="hidden" name="{{ table.prefix }}start" value="{{ request.GET.start }}">{% endif %}
    {% if request.GET.health_check %}<input type="hidden" name="{{ table.prefix }}health_check" value="{{ request.GET.health_check }}">{% endif %}
    <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.Meta.searches|get_search_data:table.prefix %}
  <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.iteritems %}
      <input type="hidden" name="{{ table.prefix }}{{ key }}" value="{{ value }}">
      {% endfor %}
      <input type="hidden" name="{{ table.prefix }}length" value="{{ table|get_prefix_length:request.GET }}">
      <div class="form-group">
        <label>Search</label>
        <input type="text" name="{{ table.prefix }}search" value="{{ terms_data|get_terms_data:table.prefix|join:" " }}">
      </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|get_search_data:table.prefix %}
                <dt>Simple text</dt>
                <dd>{{ table.Meta.searches|get_search_data:table.prefix|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|get_search_data:table.prefix %}
                <dt>Custom query text</dt>
                <dd>{{ table.Meta.queries|get_search_data:table.prefix|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|get_search_data:table.prefix %}
                <dt>Time based</dt>
                <dd>{{ table.Meta.times|get_search_data:table.prefix|join:", " }} <abbr
                title="add the filter and a number in the query string, e.g. end_time=48 - all rows must match.">(?)</abbr></dd>
              {% endif %}
              {% if table.Meta.discrete|get_discrete_data:table.prefix %}
                <dt>Exclusive text</dt>
                <dd>{{ table.Meta.discrete|get_discrete_data:table.prefix|join:", " }} <abbr
                title="add field=text in the querystring to make case sensitive matches - all fields must match.">(?)</abbr></dd>
              {% endif %}
              {% if table.Meta.terms|get_terms_data:table.prefix %}
                <dt>Case-sensitive text</dt>
                <dd>{{ table.Meta.terms|get_terms_data:table.prefix|join:", " }}</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 }}">{{ table.empty_text }}</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 %}
