{% macro button_person_before(person) %}
    {{ chill_menu('person_quick_menu', {
        'layout': '@ChillMain/Menu/quick_menu.html.twig',
        'args' : { 'person': person }
    }) }}
{% endmacro %}

{% macro button_person_after(person) %}
    {% set household = person.getCurrentHousehold %}
    {% if household is not null and is_granted('CHILL_PERSON_HOUSEHOLD_SEE', household) %}
        <li>
            <a href="{{ path('chill_person_household_summary', { 'household_id': household.id }) }}"
               class="btn btn-sm btn-chill-beige"
               title="{{ 'Show household'|trans }}"
            ><i class="fa fa-home"></i>
            </a>
        </li>
    {% endif %}
{% endmacro %}

{% macro accompanying_period(acp, person) %}
    {% set app = person.findParticipationForPeriod(acp) %}
        <div class="item-row separator">
            <div class="wrap-list periods-list">
                <div class="wl-row">
                    <div class="wl-col title">
                        <h3 class="courseid mb-2">
                                <i class="fa fa-random fa-fw"></i>
                                {{ 'Course number'|trans }} {{ acp.id }}
                        </h3>
                    </div>
                    <div class="wl-col list">
                        <div class="d-flex flex-column justify-content-center">
                            {% if app != null %}
                                {% if acp.closingDate != null %}
                                    {{ 'accompanying_period.dates_from_%opening_date%_to_%closing_date%'|trans({
                                        '%opening_date%': acp.openingDate|format_date('long'),
                                        '%closing_date%': acp.closingDate|format_date('long')}
                                    ) }}
                                {% else %}
                                    <div class="date">
                                        {{ 'Since %date%'|trans({'%date%': app.startDate|format_date('medium') }) }}
                                    </div>
                                {% endif %}
                            {% endif %}

                            {% set notif_counter = chill_count_notifications('Chill\\PersonBundle\\Entity\\AccompanyingPeriod', acp.id) %}
                            {% if notif_counter.total > 0 %}
                                {{ chill_counter_notifications('Chill\\PersonBundle\\Entity\\AccompanyingPeriod', acp.id) }}
                            {% endif %}
                        </div>
                        <div class="ms-auto">
                            {% if acp.requestoranonymous == false and acp.requestorPerson is same as(person) %}
                                <span class="as-requestor badge bg-info" title="{{ 'Requestor'|trans|e('html_attr') }}">
                                    {{ 'Requestor'|trans({'gender': person.gender ? person.gender.genderTranslation.value : 'neutral'}) }}
                                </span>
                            {% endif %}

                            {% if acp.emergency %}
                                <span class="badge rounded-pill bg-danger">{{- 'Emergency'|trans|upper -}}</span>
                            {% endif %}

                            {% if acp.confidential %}
                                <span class="badge rounded-pill bg-confidential">{{- 'Confidential'|trans|upper -}}</span>
                            {% endif %}

                            {% if acp.step == 'DRAFT' %}
                                <span class="badge bg-secondary" style="font-size: 85%;" title="{{ 'course.draft'|trans|e('html_attr') }}">{{ 'course.draft'|trans }}</span>
                            {% elseif acp.step == 'CLOSED' %}
                                <span class="badge bg-danger" style="font-size: 85%;" title="{{ 'course.closed'|trans|e('html_attr') }}">{{ 'course.closed'|trans }}</span>
                            {% elseif acp.step == 'CONFIRMED_INACTIVE_SHORT' %}
                                <span class="badge bg-chill-yellow text-primary" style="font-size: 85%;" title="{{ 'course.inactive_short'|trans|e('html_attr') }}">{{ 'course.inactive_short'|trans }}</span>
                            {% elseif acp.step == 'CONFIRMED_INACTIVE_LONG' %}
                                <span class="badge bg-danger" style="font-size: 85%;" title="{{ 'course.inactive_long'|trans|e('html_attr') }}">{{ 'course.inactive_long'|trans }}</span>
                            {% endif %}
                        </div>
                    </div>
                </div>

                {% if acp.step == 'CLOSED' and acp.closingMotive is not null %}
                    <div class="wl-row">
                        <div class="wl-col title">
                            <h3 class="closingMotive">{{ 'Closing motive'|trans }}</h3>
                        </div>
                        <div class="wl-col list">
                            <div>
                                {{ acp.closingMotive.name|localize_translatable_string }}
                            </div>
                        </div>
                    </div>
                {% endif %}


                {% if acp.user is not null %}
                    <div class="wl-row">
                        <div class="wl-col title">
                            <h3 class="referrer">{{ 'Referrer'|trans }}</h3>
                        </div>
                        <div class="wl-col list">
                            <div class="user">
                                <span class="badge-user">{{ acp.user|chill_entity_render_box }}</span>
                            </div>
                        </div>
                    </div>
                {% endif %}

                {% if acp.socialIssues|length > 0  %}
                    <div class="wl-row">
                        <div class="wl-col title">
                            <h3>{{ 'Social issues'|trans }}</h3>
                        </div>
                        <div class="wl-col list">
                            {% for issue in acp.socialIssues %}
                                {{ issue|chill_entity_render_box }}
                            {% endfor %}
                        </div>
                    </div>
                {% endif %}

                {% if acp.currentParticipations|length > 1 or (acp.currentParticipations|first).person is not same as person %}
                    <div class="wl-row">
                        <div class="wl-col title">
                            <h3 class="participants">
                                {{ 'accompanying_period.Participants_without_count'|trans({count: acp.currentParticipations|length}) }}
                            </h3>
                        </div>
                        <div class="wl-col list">
                            {% set participating = false %}
                            {% for part in acp.currentParticipations  %}
                                {% if part.person.id != person.id %}
                                    {% include '@ChillMain/OnTheFly/_insert_vue_onthefly.html.twig' with {
                                        targetEntity: { name: 'person', id: part.person.id },
                                        action: 'show',
                                        displayBadge: true,
                                        buttonText: part.person|chill_entity_render_string,
                                        isDead: part.person.deathdate is not null
                                    } %}
                                {% else %}
                                    {% set participating = true %}
                                {% endif %}
                            {% endfor %}
                            {% if participating %}
                                {{ 'person.And himself'|trans({'gender': person.gender ? person.gender.genderTranslation.value : 'neutral'}) }}
                            {% endif %}
                        </div>
                    </div>
                {% endif %}

                {% if acp.requestoranonymous == false %}
                    {% if (acp.requestorPerson is not null and acp.requestorPerson.id != person.id) or acp.requestorThirdParty is not null %}
                        <div class="wl-row">
                            <div class="wl-col title">
                                <h3>
                                    {% if acp.requestorPerson is not null %}
                                        {{ 'Requestor'|trans({'gender': acp.requestorPerson.gender ? person.gender.genderTranslation.value : 'neutral'}) }}
                                    {% else %}
                                        {{ 'Requestor'|trans({'gender': 'other'})}}
                                    {% endif %}
                                </h3>
                            </div>
                            <div class="wl-col list">
                                {% if acp.requestorThirdParty is not null %}
                                    {% include '@ChillMain/OnTheFly/_insert_vue_onthefly.html.twig' with {
                                        targetEntity: { name: 'thirdparty', id: acp.requestorThirdParty.id },
                                        action: 'show',
                                        displayBadge: true,
                                        buttonText: acp.requestorThirdParty|chill_entity_render_string
                                    } %}
                                {% else %}
                                    {% include '@ChillMain/OnTheFly/_insert_vue_onthefly.html.twig' with {
                                        targetEntity: { name: 'person', id: acp.requestorPerson.id },
                                        action: 'show',
                                        displayBadge: true,
                                        buttonText: acp.requestorPerson|chill_entity_render_string,
                                        isDead: acp.requestorPerson.deathdate is not null
                                    } %}
                                {% endif %}
                            </div>
                        </div>
                    {% endif %}
                {% endif %}

                {% set calendars = [] -%}
                {% for c in acp.nextCalendarsForPerson(person, 10) -%}
                    {% if is_granted('CHILL_CALENDAR_CALENDAR_SEE', c) -%}
                        {% set calendars = calendars|merge([c]) -%}
                    {% endif -%}
                {% endfor -%}
                {% if calendars|length > 0 %}
                    <div class="wl-row">
                        <div class="wl-col title">
                            <h3>{{ 'chill_calendar.Next calendars'|trans }}</h3>
                        </div>
                        <div class="wl-col list">
                            <div class="calendar-list">
                                <ul class="calendar-list">
                                    {% for c in calendars %}
                                        <li>
                                            {% if is_granted('CHILL_CALENDAR_CALENDAR_EDIT', c) %}
                                                <a href="{{ chill_path_add_return_path('chill_calendar_calendar_edit', {id: c.id}) }}">
                                                    <span class="badge bg-secondary">{{ c.startDate|format_datetime('long', 'short') }}</span>
                                                </a>
                                            {% else %}
                                                <span class="badge bg-secondary">{{ c.startDate|format_datetime('long', 'short') }}</span>
                                            {% endif %}
                                        </li>
                                    {% endfor %}
                                </ul>
                                {% if is_granted('CHILL_CALENDAR_CALENDAR_SEE', acp) %}
                                    <a href="{{ chill_path_add_return_path('chill_calendar_calendar_list_by_period', {'id': acp.id}) }}" class="calendar-list__global"><i class="fa fa-list"></i></a>
                                {% endif %}
                            </div>
                        </div>
                    </div>

                {% endif %}

                {% if acp.step == 'CLOSED' and acp.closingMotive is not null %}
                    <div class="wl-row">
                        <div class="wl-col title">
                            <h3 class="closingMotive">{{ 'Closing motive'|trans }}</h3>
                        </div>
                        <div class="wl-col list">
                            <div>
                                {{ acp.closingMotive.name|localize_translatable_string }}
                            </div>
                        </div>
                    </div>
                {% endif %}

                <ul class="record_actions">
                    {{ chill_menu('accompanying_course_quick_menu', {
                        'layout': '@ChillMain/Menu/quick_menu.html.twig',
                        'args' : { 'accompanying-course': acp }
                    }) }}

                    <li>
                        <a href="{{ path('chill_person_accompanying_course_index', { 'accompanying_period_id': acp.id }) }}"
                            class="btn btn-sm btn-primary"
                            title="{{ 'See accompanying period'|trans }}"
                            ><i class="fa fa-random fa-fw"></i>
                        </a>
                    </li>

                </ul>
            </div>
        </div>
{% endmacro %}

<div class="list-with-period">

    <h2>{{ title|default('Person search results')|trans }}</h2>

    <p>
        {{ 'total persons matching the search pattern'|trans({'total' : total}) }}&nbsp;:
        <a href="{{ path('chill_main_advanced_search', { "name": search_name, "q": pattern } ) }}" class="btn btn-sm">
            <i class="fa fa-search" aria-hidden="true"></i> {{ pattern }}
        </a>
    </p>

    <p>{{ 'Results %start%-%end% of %total%'|trans({ '%start%' : start, '%end%': start + persons|length, '%total%' : total } ) }}</p>

    <ul class="record_actions">
        {% if is_granted('CHILL_PERSON_CREATE') %}
            <li>
                <a href="{{ path('chill_person_new', { "_fragment": pattern }) }}" class="btn btn-create">
                    {{ 'Add a person'|trans }}
                </a>
            </li>
        {% endif %}
        {% if search_name != "person_similarity" %}
            <li>
                <a href="{{ path('chill_main_advanced_search', { "name": search_name, "q": pattern } ) }}" class="btn btn-action">
                    <i class="fa fa-fw fa-search" aria-hidden="true"></i>
                    {{ 'Advanced search'|trans }}
                </a>
            </li>
        {% endif %}
        {% if preview == true and persons | length < total %}
            <li>
                <a href="{{ path('chill_main_search', { "name": search_name|default('abcd'), "q" : pattern }) }}" class="btn btn-misc">
                    {{ 'See all results'|trans }}
                </a>
            </li>
        {% endif %}
    </ul>

    {% if persons|length > 0 %}
        <div class="flex-table">
            {% for person in persons %}
                <div class="item-bloc">

                    {{ person|chill_entity_render_box({
                        'render': 'bloc',
                        'addLink': true,
                        'addInfo': true,
                        'addAge': true,
                        'addAltNames': true,
                        'addCenter': true,
                        'address_multiline': false,
                        'customButtons': { 'after': _self.button_person_after(person), 'before': _self.button_person_before((person)) }
                    }) }}

                    {% set calendars = [] %}
                    {% for c in person.getNextCalendarsForPerson(10) %}
                        {% if is_granted('CHILL_CALENDAR_CALENDAR_SEE', c) %}
                            {% set calendars = calendars|merge([c]) %}
                        {% endif %}
                    {% endfor %}

                    {% if calendars|length > 0 %}
                        <div class="wrap-list periods-list">
                            <div class="wl-row">
                                <div class="wl-col title">
                                    <h3>{{ 'chill_calendar.Next calendars'|trans }}</h3>
                                </div>
                                <div class="wl-col list">
                                    <div class="calendar-list">
                                        <ul class="calendar-list">
                                            {% for c in calendars %}
                                                <li>
                                                    {% if is_granted('CHILL_CALENDAR_CALENDAR_EDIT', c) %}
                                                        <a href="{{ chill_path_add_return_path('chill_calendar_calendar_edit', { id: c.id }) }}">
                                                            <span class="badge bg-secondary">
                                                                {{ c.startDate|format_datetime('long', 'short') }}
                                                            </span>
                                                        </a>
                                                    {% else %}
                                                        <span class="badge bg-secondary">
                                                            {{ c.startDate|format_datetime('long', 'short') }}
                                                        </span>
                                                    {% endif %}
                                                </li>
                                            {% endfor %}
                                        </ul>
                                        {% if is_granted('CHILL_CALENDAR_CALENDAR_SEE', person) %}
                                            <a href="{{ chill_path_add_return_path('chill_calendar_calendar_list_by_person', {'id': person.id}) }}" class="calendar-list__global"><i class="fa fa-list"></i></a>
                                        {% endif %}
                                    </div>
                                </div>
                            </div>
                        </div>
                    {% endif %}

                    {#- 'acps' is for AcCompanyingPeriodS #}
                    {%- set acps = [] %}
                    {%- set acpsClosed = [] %}
                    {%- for acp in person.accompanyingPeriodInvolved %}
                        {%- if is_granted('CHILL_PERSON_ACCOMPANYING_PERIOD_SEE', acp) %}
                            {# filter for "current" periods: either the person is a requestor, or is member of the period and not closed #}
                            {% if acp.step == 'CLOSED' or (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 %}
                        {%- endif %}
                    {%- endfor %}
                    {# add as requestor #}

                    {% if acps|length > 0 %}
                        {% for acp in acps %}
                            {{ _self.accompanying_period(acp, person) }}
                        {% endfor %}
                    {% endif %}

                    {% if acpsClosed|length > 0 %}
                        <div class="accordion" id="member_{{ person.id }}">
                            <div class="accordion-item">
                                <h2 class="accordion-header" id="heading_{{ person.id }}">
                                    <button
                                        class="accordion-button collapsed"
                                        type="button"
                                        data-bs-toggle="collapse"
                                        data-bs-target="#collapse_{{ person.id }}"
                                        aria-expanded="false"
                                        aria-controls="collapse_{{ person.id }}">
                                        <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_{{ person.id }}"
                                    class="accordion-collapse collapse"
                                    aria-labelledby="heading_{{ person.id }}"
                                    data-bs-parent="#nonCurrent">
                                    {% for acp in acpsClosed %}
                                        {{ _self.accompanying_period(acp, person) }}
                                    {% endfor %}
                                </div>
                            </div>
                        </div>
                    {% endif %}
                    {% if person.signaturesPending|length > 0 %}
                    <div class="item-row separator">
                        <div class="wrap-list periods-list">
                            <div class="wl-row">
                                <div class="wl-col title">
                                    <h3>{{ 'workflow.pending_signatures'|trans({nb_signatures: person.signaturesPending|length}) }}</h3>
                                </div>
                                <div class="wl-col list">
                                    {% for signature in person.signaturesPending %}
                                        {% set entityWorkflow = signature.step.entityWorkflow %}
                                        {{ entityWorkflow|chill_entity_render_string }}
                                        <ul class="record_actions small slim">
                                            <li>
                                                <a href="{{ chill_path_force_return_path(path('chill_main_workflow_show', {id: entityWorkflow.id}), 'chill_main_workflow_signature_metadata', {signature_id: signature.id}) }}" class="btn btn-misc">
                                                    <i class="fa fa-pencil"></i>
                                                </a>
                                            </li>
                                            <li>
                                                <a href="{{ chill_path_add_return_path('chill_main_workflow_show', {id: entityWorkflow.id}) }}" class="btn btn-show"></a>
                                            </li>
                                        </ul>
                                    {% endfor %}
                                </div>
                            </div>
                        </div>
                    </div>
                    {% endif %}
                </div>
            {% endfor %}
        </div>
    {% endif %}

    {% if preview == false %}
        {{ chill_pagination(paginator) }}
    {% endif %}

</div>
