{% extends 'layouts/app.twig' %}
{% import 'macros/base.twig' as m %}

{% set only_meetings = only_meetings|default(false) %}
{% block title %}{{ not only_meetings ? __('news.title') : __('news.title.meetings') }}{% endblock %}

{% block content %}
    <div class="container">
        <h1>
            {{ block('title') }}
            {%- if has_permission_to('admin_news') and is_overview|default(false) -%}
                {{ m.button(m.glyphicon('plus'), url('admin/news', only_meetings ? {'meeting': 1} : {})) }}
            {%- endif %}
        </h1>

        {% include 'layouts/parts/messages.twig' %}

        <div class="row">
            <div class="col-md-12">
                {% block news %}
                    {% for news in news %}
                        {{ _self.news(news, true, is_overview) }}
                    {% endfor %}
                {% endblock %}
            </div>

            {% block comments %}
            {% endblock %}

            {% block pagination %}
                {% if pages|default(0) > 1 %}
                    <div class="col-md-12 text-center">
                        <ul class="pagination">
                            {% for p in range(1, pages) %}
                                <li{% if p == page %} class="active"{% endif %}>
                                    <a href="{{ url('news', p == 1 ? {} : {'page': p}) }}">{{ p }}</a>
                                </li>
                            {% endfor %}
                        </ul>
                    </div>
                {% endif %}
            {% endblock %}

            {% block write_comment %}
            {% endblock %}
        </div>
    </div>
{% endblock %}

{% macro news(news, show_comments_link, is_overview) %}
    <div class="panel {% if not news.is_meeting %}panel-default{% else %}panel-info{% endif %}">
        {% if is_overview|default(false) %}
            <div class="panel-heading">
                <h3 class="panel-title">
                    <a href="{{ url('news/' ~ news.id) }}">
                        {% if news.is_pinned %}{{ m.glyphicon('pushpin') }}{% endif %}
                        {% if news.is_meeting %}{{ __('news.is_meeting') }}{% endif %}
                        {{ news.title }}
                    </a>
                </h3>
            </div>
        {% endif %}

        <div class="panel-body">
            {{ news.text|raw|markdown(false) }}
        </div>

        <div class="panel-footer text-muted">
            {{ m.glyphicon('time') }} {{ news.updated_at.format(__('Y-m-d H:i')) }}

            {% if news.updated_at != news.created_at and not is_overview %}
                &emsp;{{ __('news.updated') }}

                <br>
                {{ m.glyphicon('time') }} {{ news.created_at.format(__('Y-m-d H:i')) }}
            {% endif %}

            &emsp;{{ m.user(news.user) }}

            {% if show_comments_link|default(false) %}
                &ensp;
                <a href="{{ url('news/' ~ news.id) }}">
                    {{ m.glyphicon('comment') }} {{ __('news.comments') }} &raquo;
                </a>
                <span class="badge">{{ news.comments.count() }}</span>
            {% endif %}

            {% if has_permission_to('admin_news') %}
                <div class="pull-right">
                    {{ m.button(m.glyphicon('edit'), url('admin/news/' ~ news.id), null, 'xs') }}
                </div>
            {% endif %}
        </div>
    </div>
{% endmacro %}