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

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

{% block content %}
    <div class="container">
        <h1>
            {% if not is_overview|default(false) %}
                {{ m.button(m.icon('chevron-left'), url('/news'), null, 'sm') }}
            {% endif %}
            {{ block('title') }}
            {%- if has_permission_to('admin_news') and is_overview|default(false) -%}
                {{ m.button(m.icon('plus-lg'), url('/admin/news', only_meetings ? {'meeting': 1} : {}), 'secondary') }}
            {%- 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">
                        <ul class="pagination justify-content-center">
                            {% for p in range(1, pages) %}
                                <li class="page-item{% if p == page %} active{% endif %}">
                                    <a class="page-link" href="{{ url( only_meetings ? 'meetings' : '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="card {% if news.is_highlighted %}bg-warning{% elseif news.is_meeting %}bg-info{% elseif theme.type =='light' %}bg-light{% else %}bg-dark{% endif %} mb-4">
        {% if is_overview|default(false) %}
        <div class="card-header {% if news.is_meeting and theme.type == 'dark' %}text-white{% endif %}">
                <a href="{{ url('/news/' ~ news.id) }}" class="text-inherit">
                    {% if news.is_pinned %}{{ m.icon('pin-angle') }}{% endif %}
                    {% if news.is_meeting %}{{ __('news.is_meeting') }}{% endif %}
                    {{ news.title }}
                </a>
            </div>
        {% endif %}

        <div class="card-body bg-body">
            {{ news.text(not is_overview)|markdown }}
            {% if is_overview and news.text != news.text(false) %}
                {{ m.button(__('news.read_more'), url('/news/' ~ news.id), null, 'sm', null, null, 'chevron-double-right') }}
            {% endif %}
        </div>

        <div class="card-footer text-nowrap {% if theme.type =='light' %}bg-light{% else %}bg-dark{% endif %} text-muted">
            <div class="d-flex flex-column flex-md-row align-items-md-center gap-3">
                {% if news.updated_at != news.created_at and not is_overview %}
                    <div>
                        {{ m.icon('clock') }} {{ news.updated_at.format(__('general.datetime')) }}
                        {{ __('news.updated') }}
                    </div>
                {% endif %}

                <div>
                    {{ m.icon('clock') }} {{ news.created_at.format(__('general.datetime')) }}
                </div>

                <div class="text-truncate">
                    {{ m.user(news.user) }}
                </div>

                {% if show_comments_link|default(false) %}
                    <div>
                        <a href="{{ url('/news/' ~ news.id) }}" class="me-1">
                            {{ m.icon('chat-left-text') }} {{ __('news.comments') }}
                        </a>
                        <span class="badge bg-primary">{{ news.comments.count() }}</span>
                        {{ m.icon('chevron-double-right', 'primary') }}
                    </div>
                {% endif %}

                {% if has_permission_to('admin_news') %}
                    <div class="d-flex ms-auto">
                        {{ m.button(m.icon('pencil'), url('/admin/news/' ~ news.id), 'secondary', 'sm') }}

                        <form class="ps-1"
                            action="{{ url('/admin/news/' ~ news.id) }}" enctype="multipart/form-data"
                            method="post">
                            {{ csrf() }}
                            {{ f.delete(null, {
                                'size': 'sm',
                                'confirm_title': __('news.delete.title', [news.title[:40]|e]),
                                'confirm_button_text': __('form.delete')
                            }) }}
                        </form>
                    </div>
                {% endif %}
            </div>
        </div>
    </div>
{% endmacro %}