{% extends "webpage/base.html" %}
{% load i18n %}
{% block title %} See all {{entity|title}}s {% endblock %}
{% block content %}
<script type="text/javascript">
    $(document).ready(function() {
        var fetchedParameter = "{{ table.page.number }}";
        $("#goto option").each(function() {
            if($(this).text() == fetchedParameter) {
                $(this).attr('selected', 'selected');
            }
        });
    });
</script>
<div class="container-fluid">
    <div class="row">
        <div class="col-md-4">
            <div class="card">
                <div class="card-body">
                    <div id="filter-container">
                        <h3>{{entity}}</h3>
                        {% load django_tables2 crispy_forms_tags %}

                    <hr/>
                        {% crispy filter.form filter.form.helper %}

                    </div>
                    {% block create_button %}
                    {% if user.is_authenticated %}
                        <div class="card">
                            <div class="card-header">
                                <h4>create new {{ entity }}</h4>
                            </div>
                            <div class="card-body">
                                {% crispy entity_create_stanbol  entity_create_stanbol.helper %}
                                <a class="btn btn-primary btn-small" href="{% url 'apis:apis_entities:generic_entities_create_view' entity %}">create new {{entity}}</a>
                            </div>
                        </div>
                    {% endif %}
                    {% endblock %}
                </div>
            </div>
        </div>

        <div class="col-md-8">
            <div class="card">
                <div class="card-body">
                    {% with table.paginator.count as total %}
                        <legend>Total results: {{ total }}</legend>
                    {% endwith %}
                    {% block table %}
                        <table{% if table.attrs %} {{ table.attrs.as_html }}{% endif %}>
                            {% block table.thead %}
                                {% if table.show_header %}
                                <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 %}">{{ column.header }}</a></th>
                                        {% else %}
                                            <th {{ column.attrs.th.as_html }}>{{ column.header }}</th>
                                        {% endif %}
                                    {% endfor %}
                                    </tr>
                                </thead>
                                {% endif %}
                            {% endblock table.thead %}
                        {% block table.tbody %}
                        <tbody>
                            {% for row in table.page.object_list|default:table.rows %} {# support pagination #}
                            {% block table.tbody.row %}
                            <tr {{ row.attrs.as_html }}>
                                {% for column, cell in row.items %}
                                    <td {{ column.attrs.td.as_html }}>{% if column.localize == None %}{{ cell }}{% else %}{% if column.localize %}{{ cell|localize }}{% else %}{{ cell|unlocalize }}{% endif %}{% endif %}</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 %}
                        {% if table.has_footer %}
                        <tfoot>
                            <tr>
                            {% for column in table.columns %}
                                <td>{{ column.footer }}</td>
                            {% endfor %}
                            </tr>
                        </tfoot>
                        {% endif %}
                        {% endblock table.tfoot %}
                    </table>
                    {% endblock table %}

                    {% block pagination.allpages %}
                        <div>
                            {% with table.page.object_list|length as count %}
                                <p>Page total: {{ count }}</p>
                            {% endwith %}
                        </div>
                        <ul class="pagination">
                            {% if table.paginator.page_range|length > 10 %}
                                {% if table.page.has_previous %}
                                    {% block pagination.previous.goto %}
                                        <li class="previous">
                                            <a style="margin-right: 10px;"  href="{% querystring table.prefixed_page_field=table.page.previous_page_number %}">{% trans "Previous" %}</a>
                                        </li>
                                    {% endblock pagination.previous.goto %}
                                {% endif %}
                            {% if table.page.has_previous or table.page.has_next %}
                                {% block pagination.cardinality %}
                                    {% blocktrans with table.page.number as current and table.paginator.num_pages as total %}
                                        <p>Page {{ current }} of {{ total }}</p>
                                    {% endblocktrans %}
                                {% endblock pagination.cardinality %}
                            {% endif %}
                            {% if table.page.has_next %}
                                {% block pagination.next.goto %}
                                    <li class="next">
                                        <a style="margin-left: 10px;" href="{% querystring table.prefixed_page_field=table.page.next_page_number %}" style="border-radius: 0px;">{% trans "Next" %}</a>
                                    </li>
                                {% endblock pagination.next.goto %}
                            {% endif %}
                        </ul>
                        <div class="form-group" style="width: 20%;">
                            <label for="goto">Go to page</label>
                            <select class="form-control" id="goto" onchange="javascript:location.href = this.value;">
                                {% for p in table.paginator.page_range %}
                                    <option value="{% querystring table.prefixed_page_field=p %}">{{ p }}</option>
                                {% endfor %}
                            </select>
                        </div>
                        {% else %}
                            {% if table.page.has_previous %}
                                {% block pagination.previous %}
                                    <li class="previous">
                                        <a style="margin-right: 10px;"  href="{% querystring table.prefixed_page_field=table.page.previous_page_number %}">{% trans "Previous" %}</a>
                                    </li>
                                {% endblock pagination.previous %}
                            {% endif %}
                            {% for p in table.paginator.page_range %}
                                {% if p == table.page.number %}
                                    <li>
                                        <a href="{% querystring table.prefixed_page_field=p %}" style="background-color:#007bff; border-color: #2e6da4; color: #ffffff; padding: 5px 10px 5px 10px;">{{ p }}</a>
                                    </li>
                                {% else %}
                                    <li>
                                        <a href="{% querystring table.prefixed_page_field=p %}" style="padding: 5px 10px 5px 10px;"> {{ p }}</a>
                                    </li>
                                {% endif %}
                            {% endfor %}
                            {% if table.page.has_next %}
                                {% block pagination.next %}
                                    <li class="next">
                                        <a style="margin-left: 10px;"  href="{% querystring table.prefixed_page_field=table.page.next_page_number %}">{% trans "Next" %}</a>
                                    </li>
                                {% endblock pagination.next %}
                            {% endif %}

                        </ul>
                        {% endif %}
                    {% endblock pagination.allpages %}
                    <div id="download_buttons">
                            {% for format in table.export_formats %}
                                <a href="{% querystring '_export'=format %}">
                                    download  <code>.{{ format }}</code>
                                </a>
                            {% endfor %}
                        </div>
                    </div>
                </div>
            </div>
    </div>
</div>

{% endblock %}
