{% macro renderPager(pager, aggregate) %}
    {{
        pagerfanta(
            pager,
            'twitter_bootstrap_translated',
            {
                'proximity' : 1,
                'routeName': 'claro_announcements_list',
                'routeParams': {'aggregateId': aggregate.getId()}
            }
        )
    }}
{% endmacro %}

{% from _self import renderPager %}

{% extends "ClarolineCoreBundle:Workspace:layout.html.twig" %}

{% block javascripts %}
    {{ parent() }}
    <script
        src='{{ asset('bundles/clarolineannouncement/js/announcements_list.js') }}'
        type="text/javascript"
    >
    </script>
{% endblock %}

{% block section_content %}

<div class="panel-heading">
    <h3 class="panel-title">
        {{ 'announcements_list'|trans({}, 'announcement') }}
    </h3>
</div>
<div class="panel-body">
    <h4>{{ 'resource'|trans({}, 'platform') }} : {{ _resource.getResourceNode().getName() }}</h4>
    <br>

    {% if is_granted('EDIT', resourceCollection) %}
    <div>
        <a href="{{ path('claro_announcement_create_form', {'aggregateId': _resource.getId()}) }}">
            <button class="btn btn-default create-announcement-button">
                {{ 'create_new_announcement'|trans({}, 'announcement') }}
            </button>
        </a>
    </div>
    {% endif %}

    <br>

    <div class="margin">
        {% set nbPages = announcements.getNbPages() %}

        {% if nbPages > 1 %}
            {{ renderPager(announcements, _resource) }}
        {% endif %}

        {% for announcement in announcements %}
        <div class="panel
            {% if announcement.isVisible() %}
                panel-primary
            {% else %}
                panel-danger
            {% endif %}"
         >
            <div class="pull-right">
                {% if is_granted('EDIT', resourceCollection) %}
                    <a href="{{ path('claro_announcement_edit_form', {'announcementId': announcement.getId()}) }}">
                        <button type="button" class="btn btn-default">
                            <i class="fa fa-pencil-square-o"></i>
                        </button>
                    </a>
                    <button
                        type="button"
                        class="announcement-delete-button btn btn-default"
                        btn-announcement-id="{{ announcement.getId() }}"
                    >
                        &times;
                    </button>
                {% endif %}
            </div>

            {% set title = announcement.getTitle() %}

            {% if title is not null %}
            <div class="panel-heading">
                <b>{{ title }}</b>
            </div>
            {% endif %}

            <div class="panel-body">
                {% set announcer = announcement.getAnnouncer() %}

                {% if announcer is not null %}
                    <b>{{ announcer }} :</b>
                    <div class="well well-sm">
                {% endif %}

                {{ announcement.getContent()|raw }}

                {% if announcer is not null %}
                    </div>
                {% endif %}
            </div>

            <div class="panel-footer">
                {% if (is_granted('EDIT', resourceCollection) or is_granted('DELETE', resourceCollection)) %}
                <div>
                    <small>
                        {% set creator = announcement.getCreator() %}
                        {{ 'created_on'|trans({}, 'announcement') }}
                        {{ announcement.getCreationDate().format('d-m-Y') }}
                        {{ 'at'|trans({}, 'announcement') }}
                        {{ announcement.getCreationDate().format('H:i:s') }},
                        {{ 'by'|trans({}, 'announcement') }}
                        {{ creator.getFirstName() }}
                        {{ creator.getLastName() }}
                    </small>
                </div>
                {% endif %}

                {% set publicationDate = announcement.getPublicationDate() %}
                {% set visibleFrom = announcement.getVisibleFrom() %}
                {% set visibleUntil = announcement.getVisibleUntil() %}

                {% if publicationDate is not null %}
                    <div>
                        <small>
                            {{ 'published_on'|trans({}, 'announcement') }}
                            {{ publicationDate.format('d-m-Y') }}
                            {% if (is_granted('EDIT', resourceCollection) or is_granted('DELETE', resourceCollection)) %}
                            <span>
                                <b>
                                [
                                {% if visibleFrom is not null %}
                                    {{ visibleFrom.format('d-m-Y') }}
                                {% else %}
                                    ?
                                {% endif %}
                                &rarr;
                                {% if visibleUntil is not null %}
                                    {{ visibleUntil.format('d-m-Y') }}
                                {% else %}
                                    ?
                                {% endif %}
                                ]
                                </b>
                            </span>
                            {% endif %}
                        </small>
                    </div>
                {% endif %}
            </div>
        </div>
        {% endfor %}

        {% if nbPages > 1 %}
            {{ renderPager(announcements, _resource) }}
        {% endif %}
    </div>
</div>


<div id="delete-announcement-validation-box" class="modal fade">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">×</button>
                <h3>{{ 'announcement_delete_confirm_title'|trans({}, 'announcement') }}</h3>
            </div>
            <div class="modal-body">
                <p>{{ 'announcement_delete_confirm_message'|trans({}, 'announcement') }}</p>
            </div>
            <div class="modal-footer">
                <input type="button" id="delete-confirm-ok" class="btn btn-primary" value="{{ 'ok'|trans({}, 'platform') }}"/>
                <input type="button" class="btn btn-default" data-dismiss="modal" value="{{ 'cancel'|trans({}, 'platform') }}"/>
            </div>
        </div>
    </div>
</div>

{% endblock %}