{% if person is defined %}
    {% set contextEntity = { 'type': 'person', 'entity': person } %}{% endif %}
{% if household is defined %}
    {% set contextEntity = { 'type': 'household', 'entity': household } %}{% endif %}

{% macro recordAction(period, contextEntity) %}
    {# TODO if enable_accompanying_course_with_multiple_persons is true ... #}
    <li>
        <a href="{{ path('chill_person_accompanying_course_index', { 'accompanying_period_id': period.id }) }}"
           class="btn btn-show" title="{{ 'See accompanying period'|trans }}">{# {{ 'See this period'|trans }} #}</a>
    </li>
    {% if period.step == 'DRAFT' and contextEntity.type == 'person' %}
        {% set person = contextEntity.entity %}
        <li>
            <a href="{{ path('chill_person_accompanying_course_delete', { 'accompanying_period_id': period.id, 'person_id' : person.id }) }}"
               class="btn btn-delete" title="{{ 'Delete accompanying period'|trans }}">{# {{ 'Delete this period'|trans }} #}</a>
        </li>
    {% endif %}

    {# DISABLED if new accompanying course, this is not necessary
    {% if person is defined %}
        <li>
            <a  href="{{ path('chill_person_accompanying_period_update', {'person_id' : person.id, 'period_id' : period.id } ) }}"
                class="btn btn-update" title="{{ 'Edit accompanying period'|trans }}"></a>
        </li>
        {% if period.isOpen == true %}
            <li>
                <a href="{{ path('chill_person_accompanying_period_close', {'person_id' : person.id}) }}"
                   class="btn btn-update change-icon">
                    <i class="fa fa-fw fa-lock" aria-hidden="true"></i>
                    {{'Close accompanying period'|trans }}
                </a>
            </li>
        {% endif %}
        {% if period.canBeReOpened(person) == true %}
            <li>
                <a href="{{ path('chill_person_accompanying_period_re_open', {'person_id' : person.id, 'period_id' : period.id } ) }}"
                   class="btn btn-create change-icon">
                    <i class="fa fa-fw fa-unlock" aria-hidden="true"></i>
                    {{'Re-open accompanying period'|trans }}
                </a>
            </li>
        {% endif %}
    {% elseif household is defined %}
        TODO buttons specific for household ?
    {% endif %}
    #}
{% endmacro %}

{% block content %}

    {# WARNING: this list is rendered in either person context or houshold context #}

    {%- set acps = [] %}
    {%- set acpsClosed = [] %}
    {% for acp in accompanying_periods %}
        {% if acp.step == 'CLOSED' or (person is defined and acp.requestorPerson is not same as(person) and acp.openParticipationContainsPerson(person) is null ) %}
            {%- set acpsClosed = acpsClosed|merge([acp]) %}
        {% else %}
            {%- set acps = acps|merge([acp]) %}
        {% endif %}
    {% endfor %}

    <div class="flex-table accompanyingcourse-list">
        {% for period in acps %}
            {% include '@ChillPerson/AccompanyingPeriod/_list_item.html.twig' with {
                'recordAction': _self.recordAction(period, contextEntity)
            } %}
        {% else %}
            <p class="chill-no-data-statement">{{ 'Any accompanying periods are open'|trans }}</p>
        {% endfor %}
    </div>

    {% if acpsClosed|length > 0 %}
        {% if person is defined %}
            {% set identifier = person.id %}
        {% else  %}
            {% set identifier = household.id %}
        {% endif %}

        <div class="accordion" id="member_{{ identifier }}">
            <div class="accordion-item">
                <h2 class="accordion-header" id="heading_{{ identifier }}">
                    <button
                        class="accordion-button collapsed"
                        type="button"
                        data-bs-toggle="collapse"
                        data-bs-target="#collapse_{{ identifier }}"
                        aria-expanded="false"
                        aria-controls="collapse_{{ identifier }}">
                        <span class="folded">{{ 'periods.show closed periods'|trans({ 'nb_items': acpsClosed|length }) }}</span>
                        <span class="unfolded text-secondary">{{ 'periods.hide closed periods'|trans({ 'nb_items': acpsClosed|length }) }}</span>
                    </button>
                </h2>

                <div id="collapse_{{ identifier }}"
                     class="accordion-collapse collapse"
                     aria-labelledby="heading_{{ identifier }}"
                     data-bs-parent="#nonCurrent">

                    <div class="flex-table accompanyingcourse-list">
                        {% for period in acpsClosed %}
                            {% include '@ChillPerson/AccompanyingPeriod/_list_item.html.twig' with {
                                'recordAction': _self.recordAction(period, contextEntity)
                            } %}
                        {% endfor %}
                    </div>

                </div>
            </div>
        </div>
    {% endif %}
{% endblock content %}
