{% extends "smartmin/base.html" %}

{% load smartmin i18n %}

{% block content %}

{% block table-controls %}
<div class="row">
  <div class="col-md-8">
    {% if view.search_fields %}
    {% block search-form %}
    <form class="form-inline" role="form">
      <input type="text" class="form-control input-medium search-query" name="search" value="{{ search }}">
      <button type="submit" class="btn btn-default">{% trans "Search" %}</button>
    </form>
    {% endblock %}
    {% else %}
    &nbsp;
    {% endif %}
  </div>

  <div class="col-md-4 buttons">
    {% block table-buttons %}
    {% if view.add_button %}
    <a class="btn btn-primary pull-right" href="./create/">{% trans "Add" %}</a>
    {% endif %}
    {% endblock table-buttons %}
  </div>
</div>
{% endblock %}

{% block pjax %}
<div id="pjax">
<div class="row">
  <div class="col-md-12">
    {% block pre-table %}{% endblock %}

    {% block table %}
    <table class="list-table {% get_list_class object_list %} table table-bordered table-striped" cellspacing="0">
      <thead>
        <tr>
          {% for field in fields %}
          <th class="header-{{field}} {% if view|field_orderable:field %}header {% if field == order %}{% if order_asc %}headerSortUp{% else %}headerSortDown{% endif %}{% endif %}{% endif %}" id="header-{{field}}">{% get_label field %}</th>
          {% endfor %}
        </tr>
      </thead>
      <tbody>
        {% for obj in object_list %}
        <tr class="{% cycle 'row2' 'row1' %} {% if not obj.is_active and obj|is_smartobject %}inactive{% endif %}">
          {% for field in fields %}
          <td class="value-{{field}} {% get_class field obj %}{% if field in link_fields %} clickable{% endif %}">
            {% if field in link_fields %}<a {% if pjax %}data-pjax='{{ pjax }}'{% endif %} href="{% get_field_link field obj %}">{% endif %}{% get_value obj field %}{% if field in link_fields %}</a>{% endif %}
          </td>
          {% endfor %}
        </tr>
        {% empty %}
        <tr class="empty_list">
          {% for field in fields %}
          <td></td>
          {% endfor %}
        </tr>
        {% endfor %}

        {% block extra-rows %}
        {% endblock extra-rows %}
      </tbody>
    </table>
    {% endblock table %}

    {% block post-table %}{% endblock post-table %}
  </div>
</div>

{% block paginator %}
<div class="row">
  <div class="col-md-3">
    <div class="pagination-text">
      {% if not paginator or paginator.num_pages <= 1 %}
	{% blocktrans count counter=object_list|length %}
	 {{ counter }} result
	{% plural %}
         {{ counter }} results
        {% endblocktrans %}
      {% else %}
	{% blocktrans with start=page_obj.start_index end=page_obj.end_index count=paginator.count %}
	Results {{ start }}-{{ end }} of {{ count }}
	{% endblocktrans %}
      {% endif %}
    </div>
  </div>
  <div class="col-md-9">
    {% if paginator and paginator.num_pages > 1 %}
      <ul class="pagination pull-right">
        {% if page_obj.has_previous %}
        <li class="prev"><a href="{{url_params|safe}}page={{page_obj.previous_page_number}}">&larr; {% trans "Previous" %}</a></li>
        {% else %}
        <li class="prev disabled"><a href="#">&larr; {% trans "Previous" %}</a></li>
        {% endif %}
	
        {% for page_num in paginator.page_range %}
	  {% if page_obj.number < 5 %}
	    {% if page_num < 10 %}
	      {% if not page_num == page_obj.number %}
	        <li><a href="{{url_params|safe}}{{order_params|safe}}page={{page_num}}">{{ page_num }}</a></li>
	      {% else %}
                <li class="active"><a href="#">{{ page_num }}</a></li>
              {% endif %}
            {% endif %}
	  {% elif page_num < page_obj.number|add:"5" and page_num > page_obj.number|add:"-5" %}
	    {% if not page_num == page_obj.number %}
	      <li><a href="{{url_params|safe}}{{order_params|safe}}page={{page_num}}">{{ page_num }}</a></li>
	    {% else %}
              <li class="active"><a href="#">{{ page_num }}</a></li>
            {% endif %}
	  {% elif page_obj.number > paginator.num_pages|add:"-5" %}
	    {% if page_num > paginator.num_pages|add:"-9" %}
	      {% if not page_num == page_obj.number %}
	        <li><a href="{{url_params|safe}}{{order_params|safe}}page={{page_num}}">{{ page_num }}</a></li>
	      {% else %}
                <li class="active"><a href="#">{{ page_num }}</a></li>
              {% endif %}
	    {% endif %}

          {% endif %}
        {% endfor %}

        {% if page_obj.has_next %}
        <li class="next"><a href="{{url_params|safe}}{{order_params|safe}}page={{page_obj.next_page_number}}">{% trans "Next" %} &rarr;</a></li>
        {% else %}
        <li class="next disabled"><a href="#">{% trans "Next" %} &rarr;</a></li>
        {% endif %}
      </ul>
    {% endif %}
  </div>
</div>
{% endblock %}

</div>
{% endblock pjax %}
{% endblock content %}

{% block extra-script %}
{{ block.super }}

<script>
  $(document).ready(function(){
    $("th.header").click(function(evt){
      // build up our ordering
      var field = evt.target.id.substr(evt.target.id.indexOf("-")+1)

      {% if order_asc %}
       if (field == "{{order}}"){
         field = "-" + field;
       } 
       {% endif %}

       document.location = "{{url_params|safe}}_order=" + field;
    });
  });
</script>

{% endblock %}
