{% from 'attachments/_attachments.html' import render_attachments as _render_attachments %}

{% macro render_attachments(category) %}
    {%- set can_manage = category.can_manage(session.user) -%}
    {%- if category.attachment_count or can_manage %}
        <div class="title">
            <h2 class="icon-attachment">{% trans %}Materials{% endtrans %}</h2>
            {% if can_manage %}
                <div class="actions">
                    <button class="i-button text-color subtle icon-edit js-manage-attachments"
                            title="{% trans %}Manage materials{% endtrans %}"
                            data-title="{% trans title=category.title %}Manage category materials{% endtrans %}"
                            data-locator="{{ category.locator|tojson|forceescape }}"
                            data-attachment-editor
                            data-reload-on-change></button>
                </div>
            {% endif %}
        </div>
        {{ _render_attachments(category.attached_items, linked_object=category, management=false) }}
    {% endif -%}
{% endmacro %}

{% macro render_managers(managers) %}
    {%- if managers %}
        <div class="title">
            <h2 class="icon-medal">{% trans %}Managers{% endtrans %}</h2>
        </div>
        <ul id="manager-list">
            {% for manager in managers %}
                <li class="{{ 'icon-user' if manager.principal_type.name in ('user', 'email') else 'icon-users' }}">
                    {{ manager.name }}
                </li>
            {% endfor %}
        </ul>
    {% endif -%}
{% endmacro %}

{% macro render_news(news_list, tzinfo) %}
    {%- if news_list %}
        <div class="title">
            <h2 class="icon-bullhorn">{% trans %}News{% endtrans %}</h2>
            <a href="{{ url_for('news.display') }}" class="more-icon">{% trans %}more...{% endtrans %}</a>
        </div>
        <ul class="main-page-list">
            {% for news in news_list %}
                <li>
                    <a class="title" href="{{ news.url }}">
                        {{ news.title }}
                    </a>
                    <span class="timing">
                        {% trans date=news.created_dt|format_pretty_date(tzinfo=tzinfo, capitalize=false) -%}
                            Posted {{ date }}
                        {%- endtrans %}
                    </span>
                </li>
            {% endfor %}
        </ul>
    {% endif -%}
{% endmacro %}

{% macro render_upcoming_events(upcoming_events, tzinfo, happening_now) %}
    {%- if upcoming_events %}
        <div class="title">
            <h2 class="icon-alarm">{% trans %}Upcoming events{% endtrans %}</h2>
        </div>
        <ul class="main-page-list">
            {% for event in upcoming_events %}
                <li>
                    {% block upcoming_title scoped %}
                        <a href="{{ event.url }}" class="title" title="{{ event.title|striptags }}" >
                            {{ event.title|striptags }}
                        </a>
                    {% endblock %}
                    <span class="timing">
                        {% if happening_now(event) %}
                            {% trans time=event.end_dt|format_pretty_date(tzinfo=tzinfo, capitalize=false) -%}
                                Ongoing until {{ time }}
                            {%- endtrans %}
                        {% else %}
                            {% trans time=event.start_dt|format_pretty_date(tzinfo=tzinfo, capitalize=false) -%}
                                Starts {{ time }}
                            {%- endtrans %}
                        {% endif %}
                    </span>
                </li>
            {% endfor %}
        </ul>
    {% endif -%}
{% endmacro %}
