{% extends '::layout.html.twig' %}

{% block title %}
    Home
{% endblock %}

{% block activeHome %}
    class="active"
{% endblock %}
{% block activeWhatsOpenData %}
    class=""
{% endblock %}
{% block activeLegalInformation %}
    class=""
{% endblock %}
{% block activeContact %}
    class=""
{% endblock %}

{% block content %}

    {% for label, flashes in app.session.flashbag.all %}
        {% for flash in flashes %}
            <div class="alert alert-success">
                {{ flash }}
            </div>
        {% endfor %}
    {% endfor %}

    <div class="row">
        <div class="col-sm-3">
            <div class="page-header">
                <h1>Categories</h1>
            </div>
            <div class="list-group" id="list_categories">

                {% set id_category = app.request.get('id_category') %}

                <a href="{{ path('home', {'id_category': '0'}) }}" class="list-group-item {% if (id_category == "") or (id_category == 0) %}active{% endif %}">ALL DATA</a>
                {% for category in categories %}
                    <a href="{{ path('home', {'id_category': category.id}) }}" class="list-group-item {% if id_category == category.id %}active{% endif %}">{{ category.nameEngCategory }}</a>
                {% endfor %}

            </div>
        </div><!-- /.col-sm-3 -->


        <div class="col-md-9">

            {% if not (id_category == 4) %}

            <div class="page-header">
                <h1>Data</h1>
            </div>

            {% if is_granted('ROLE_USER') %}
            <a style="text-decoration: none" href="{{ path('upload') }}"><button class="btn btn-lg btn-primary btn-block" type="button"> <i class="glyphicon glyphicon-upload"></i>  Upload Data</button></a>
            <div style="margin-bottom: 20px"></div>
            {% endif %}

            <div class="panel panel-default">
                <div class="panel-heading"><h4></h4>

                </div>
                <div class="panel-body">
                    <table class="table table-striped">
                        <thead>
                        <tr><th>#</th><th>Name</th></th><th>Type</th><th>Size</th><th>Date of upload</th><th>Popularity</th></tr>
                        </thead>
                        <tbody>

                            {% for file in data %}
                                {% if ((id_category == file.idCategory) or (id_category == "") or (id_category == 0)) %}

                                <tr>
                                    <td>{{ loop.index }}</td>
                                    <td><a href="{{ path('file', { 'id_file': file.id }) }}">{{ file.nameEng }}</a></td>
                                    <td>{{ file.type }}</td>
                                    <td>{% if (file.size < (1024*1024)) %} {{ (file.size / (1024)) | number_format(2) }}KB {% else %} {{ (file.size / (1024*1024)) | number_format(2) }}MB {% endif %}</td>
                                    <td>{{ file.dateTime | date('Y-m-d') }}</td>
                                    <td>{{ file.views }} {% if (file.views == 1) %}view{% else %}views{% endif %}</td>
                                </tr>

                                {% endif %}
                            {% endfor %}

                        </tbody>
                    </table>

                    {% if total_pages_data > 0 %}
                        <center>
                        <ul class="pagination pagination-sm">
                            {% for i in 1..total_pages_data %}
                                {% if loop.first %}
                            <li class="prev {% if current_page_data == 1 %} disabled{% endif %}"><a href="{{ path('home', {'page_data' : current_page_data - 1}) }}">&laquo;</a></li>
                                {% endif %}

                            <li {% if current_page_data == loop.index %} class="active"{% endif %}><a href="{{ path('home', {'page_data' : loop.index}) }}">{{ loop.index }}</a></li>

                                {% if loop.last %}
                            <li class="next{% if current_page_data == total_pages_data %} disabled{% endif %}"><a href="{{ path('home', {'page_data' : current_page_data + 1}) }}">&raquo;</a></li>
                                {% endif %}
                            {% endfor %}
                        </ul>
                        </center>
                    {% endif %}
                </div>
            </div>

            {% endif %}

            <div style="margin-bottom: 30px"></div>

            {% if ((id_category == "") or (id_category == 0) or (id_category == 4)) %}

            <div class="page-header">
                <h1>Events</h1>
            </div>

            {% if is_granted('ROLE_USER') %}
                <a style="text-decoration: none" href="{{ path('add_event') }}"><button class="btn btn-lg btn-primary btn-block" type="button"> <i class="glyphicon glyphicon-calendar"></i>  Add Event</button></a>
                <div style="margin-bottom: 20px"></div>
            {% endif %}

            <div class="panel panel-default">
                <div class="panel-heading"><h4></h4>
                </div>
                <div class="panel-body">
                    <table class="table table-striped">
                        <thead>
                        <tr><th>#</th><th>Title</th><th>Date</th></th><th>Place</th><th>Description</th><th>Popularity</th></tr>
                        </thead>
                        <tbody>

                        {% for event in events %}

                                <tr>
                                    <td>{{ loop.index }}</td>
                                    <td><a href="{{ path('event', { 'id_event': event.id }) }}">{{ event.titleEng }}</a></td>
                                    <td>{{ event.date }}</td>
                                    <td>{{ event.place }}</td>
                                    <td>{% if event|length > 100 %}...{% else %}{{ event.description }}{% endif %}</td>
                                    <td>{{ event.views }} {% if (event.views == 1) %}view{% else %}views{% endif %}</td>
                                </tr>

                        {% endfor %}

                        </tbody>
                    </table>

                    {% if total_pages_events > 0 %}
                        <center>
                            <ul class="pagination pagination-sm">
                                {% for i in 1..total_pages_events %}
                                    {% if loop.first %}
                                        <li class="prev {% if current_page_events == 1 %} disabled{% endif %}"><a href="{{ path('home', {'page_events' : current_page_events - 1}) }}">&laquo;</a></li>
                                    {% endif %}

                                    <li {% if current_page_events == loop.index %} class="active"{% endif %}><a href="{{ path('home', {'page_events' : loop.index}) }}">{{ loop.index }}</a></li>

                                    {% if loop.last %}
                                        <li class="next{% if current_page_events == total_pages_events %} disabled{% endif %}"><a href="{{ path('home', {'page_events' : current_page_events + 1}) }}">&raquo;</a></li>
                                    {% endif %}
                                {% endfor %}
                            </ul>
                        </center>
                    {% endif %}
                </div>
            </div>

            {% endif %}
        </div><!--/col-span-6-->

    </div>
{% endblock %}