{% extends 'layouts/base.html' %}

{% if parent_category != category %}
  {% set page_title = 'Category %s/%s'|format(parent_category.get_name(), category.get_name()) %}
{% else %}
  {% set page_title = 'Category %s'|format(category.get_name()) %}
{% endif %}

{% block breadcrumbs %}
<!--
<nav class="pt-3" aria-label="breadcrumb">
    <ol class="breadcrumb">
        <li class="breadcrumb-item">
            <a href="{{ url_for('portal.categories_index') }}">Software</a>
        </li>
        {% if parent_category != category %}
        <li class="breadcrumb-item">
            <a href="{{ url_for('portal.category_view', cat_id=parent_category.get_id(), page=1) }}">{{parent_category.get_name()}}</a>
        </li>
        {% endif %}
        <li class="breadcrumb-item active" aria-current="page">
            {{category.get_name()}}
        </li>
    </ol>
</nav>
-->
{% endblock %}

{% block body %}
<div class="container">

        <h1>{{category.get_name()}}</h1>


        {% if category.has_children() %}
        <div class="columns is-multiline is-mobile grid">
            {% for sc in category.get_children() %}
            <div class="column is-narrow">

                <a class="button" href="{{ url_for('portal.category_view', cat_id=parent_category.get_id(), subcat_id=sc.get_id(), page=1) }}">{{sc.get_name()}}</a>
            </div>
            {% endfor %}
        </div>
        {% endif %}



        {% if current_page == 1 and not software %}
        <h2>
          No software was found in this section!
        </h2>
        {% endif %}

        <div class="columns is-multiline is-mobile grid">

            {% for sw in software %}
            <div class="column is-one-fifth-desktop is-half-mobile">
                <a class="list-group-item" href="{{ url_for('software.details', cid=sw.cid) }}">
                <div class="box app-item is-horizontal-center">

                    <p>
                        <figure class="image has-image-centered is-64x64">
                        {% if sw.icon_name %}
                            <img src="{{ config.APPSTREAM_MEDIA_URL + '/' + sw.gcid + '/icons/64x64/' + sw.icon_name }}" alt="Icon">
                        {% else %}
                            <img src="data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9InllcyI/PjxzdmcgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB3aWR0aD0iNjQiIGhlaWdodD0iNjQiIHZpZXdCb3g9IjAgMCA2NCA2NCIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+PGRlZnMvPjxyZWN0IHdpZHRoPSI2NCIgaGVpZ2h0PSI2NCIgZmlsbD0iI0VFRUVFRSIvPjxnPjx0ZXh0IHg9IjEzLjQ2MDkzNzUiIHk9IjMyIiBzdHlsZT0iZmlsbDojQUFBQUFBO2ZvbnQtd2VpZ2h0OmJvbGQ7Zm9udC1mYW1pbHk6QXJpYWwsIEhlbHZldGljYSwgT3BlbiBTYW5zLCBzYW5zLXNlcmlmLCBtb25vc3BhY2U7Zm9udC1zaXplOjEwcHQ7ZG9taW5hbnQtYmFzZWxpbmU6Y2VudHJhbCI+NjR4NjQ8L3RleHQ+PC9nPjwvc3ZnPg==" alt="Icon">
                        {% endif %}
                        </figure>
                    <p>

                    <p class="has-text-centered is-size-6 is-size-7-mobile app-name">
                        {{sw.name}}
                    </p>
                    <p class="has-text-centered is-size-6 is-size-7-mobile badges">
                        {% if sw.is_free %}
                        <i class="fas fa-heart badge" title="Respects Freedom"></i>&nbsp;
                        {% endif %}
                        <!--
                        <i class="fas fa-shield-alt badge" title="Verifiable"></i>&nbsp;
                        <i class="fas fa-mobile badge" title="Mobile friendly"></i>&nbsp;
                        -->
                    </p>
                </div>
                </a>
            </div>
            {% endfor %}
        </div>

    {% if page_count > 1 %}
    <nav class="pagination is-centered" role="navigation" aria-label="pagination">
        <a class="pagination-previous" href="{{ url_for('portal.category_view', cat_id=parent_category.get_id(), subcat_id=subcat_id, page=current_page-1) if current_page > 1 else '#'}}">Previous</a>
        <a class="pagination-next" href="{{ url_for('portal.category_view', cat_id=parent_category.get_id(), subcat_id=subcat_id, page=current_page+1) if current_page < page_count else '#'}}">Next page</a>

        <ul class="pagination-list">

            <li>
                <a class="pagination-link {{'is-current' if 1 == current_page }}" href="{{ url_for('portal.category_view', cat_id=parent_category.get_id(), subcat_id=subcat_id, page=1) }}">1</a>
            </li>

            {% if page_count > 7 and current_page > 3 %}
            <li><span class="pagination-ellipsis">&hellip;</span></li>
            {% endif %}

            {% for i in range(2, page_count) %}
                {% if page_count < 8 or i < current_page + 2 and i > current_page - 2 or current_page < 4 and i < 6 or current_page > page_count - 3 and i > page_count - 5 %}

                <li>
                    <a class="pagination-link {{'is-current' if i == current_page }}" href="{{ url_for('portal.category_view', cat_id=parent_category.get_id(), subcat_id=subcat_id, page=i) }}">{{i}}</a>
                </li>

                {% endif %}
            {% endfor %}

            {% if page_count > 7 and current_page < page_count - 2 %}
            <li><span class="pagination-ellipsis">&hellip;</span></li>
            {% endif %}


            <li>
                <a class="pagination-link {{'is-current' if page_count == current_page }}" href="{{ url_for('portal.category_view', cat_id=parent_category.get_id(), subcat_id=subcat_id, page=page_count) }}">{{page_count}}</a>
            </li>

        </ul>
    </nav>
    {% endif %}

</div>


{% endblock %}
