{% extends "base.html" %}

{% macro format_authors(authors) %}
  {% for author in authors %}
    <a href='/feed?authors={{ author.username|urlencode }}'>
      {{ author.format_name }}
    </a>
    {% if not loop.last %}
      ,
    {% endif %}
  {% endfor %}
{% endmacro %}


{% block title %} Knowledge Repo {% endblock %}

{% block content %}

  <div class="row">
    <br>
    <div class="col-md-5">
      <div class="btn-group btn-group-justified index-view-btn-group" role="group">
        <a href="/feed" class="btn btn-default btn-card no-underline" role="button">
          <i class="glyphicon glyphicon-post-org glyphicon-align-justify"></i>
          <span class="index-view-name"> Card </span>
        </a>
        <a href="/table" class="btn btn-default btn-table no-underline" role="button">
          <i class="glyphicon glyphicon-post-org glyphicon-th"></i>
          <span class="index-view-name"> Table </span>
        </a>
        <a href="/cluster" class="btn btn-default btn-cluster no-underline" role="button">
          <i class="glyphicon glyphicon-post-org glyphicon-th-list"></i>
          <span class="index-view-nane"> Cluster </span>
        </a>
      </div>
    </div>

  {% if request.endpoint != 'index.render_cluster' %}
  <div class='col-md-3 col-md-offset-4'>
    {% set start = feed_params['start'] | default(0) %}
    {% set results = feed_params['results'] | default(10) %}
    {% if start == 0 %}
      {% set page = 1 %}
    {% else %}
      {% set page = start//results + 1 %}
    {% endif %}
    <div class="btn-group btn-sm btn-group-justified pagination-group" role="group">
      {% if page == 1 %}
        <a href="#" class="btn btn-default disabled">
          <i class="glyphicon glyphicon-chevron-left"></i>
        </a>
      {% else %}
        {% set prev_page = url_for(request.endpoint, start=(page-2)*results, results=results, filters=feed_params['filters']) %}
        {% if request.endpoint == 'tag.render_tag_pages' %}
          {% set prev_page = prev_page + "&tag=" + full_tag %}
        {% endif %}
        <a href="{{ prev_page }}" class="btn btn-default">
          <i class="glyphicon glyphicon-chevron-left"></i>
        </a>
      {% endif %}
      {% if page == 1 %}
        {% set page_nums = [page, page + 1, page + 2] %}
      {% else %}
        {% set page_nums = [page - 1, page, page + 1] %}
      {% endif %}
      {% for page_num in page_nums %}
        {% set href = url_for(request.endpoint, start=(page_num - 1) * results, results=results, filters=feed_params["filters"]) %}
          {% if request.endpoint == 'tag.render_tag_pages' %}
            {% set href = href + "&tag=" + full_tag %}
          {% endif %}
        {% if page == page_num %}
          <a href= "{{ href }}" class="btn btn-default page-btn active-page"> {{ page_num }} </a>
        {% else %}
          <a href = "{{ href }}" class="btn btn-default page-btn"> {{ page_num }} </a>
        {% endif %}
      {% endfor %}
      {% set next_page = url_for(request.endpoint, start=page*results, results=results, filters=feed_params['filters']) %}
        {% if request.endpoint == 'tag.render_tag_pages' %}
          {% set next_page = next_page + "&tag=" + full_tag %}
        {% endif %}
        <a href="{{ next_page }}" class="btn btn-default">
          <i class="glyphicon glyphicon-chevron-right"></i>
        </a>
    </div>
  </div>
  {% endif %}

  </div>

  <div class="col-12">
    {% block inner_content %}
    {% endblock %}
  </div>
{% endblock %}

{% block scripts %}
{{ super() }}
<script type="text/javascript">

    $( document ).ready(function() {
     $.ajaxSetup({
      cache: true
    });

    $('[data-toggle="tooltip"]').tooltip();

  });
</script>
{% endblock %}


