{#
    Template to render a person
    OPTIONS
    * render        string ['raw'|'label'|'bloc']
    * addAltNames   bool
    * addLink       bool
    * addEntity     bool
    * addId         bool
    * addInfo       bool
    * addAge        bool
    * addCenter     bool
    * hLevel        integer
    * addDeath      bool
    * addAgeBadge   bool
    * suffixText    bool
    * address_multiline     bool
    * customButtons [
        'before'    Twig\Markup, (injected with macro)
        'replace'   Twig\Markup,
        'after'     Twig\Markup
    ]
    * customArea [
        'beforeLabel'   Twig\Markup,
        'afterLabel'    Twig\Markup,
    ]
#}

{% macro raw(person, options) %}
    <span class="firstname">{{ person.firstName }}</span>
    <span class="lastname">
        {{ person.lastName }}
        {%- if options['addDeath'] -%}
            {% if person.deathdate is not null %} (‡){% endif %}
        {% endif %}
    </span>
    {%- if options['addAltNames'] -%}
        <span class="altnames">
        {%- for n in person.altNames -%}
            <span class="altname altname-{{ n.key }}">
                {{- n.label -}}
            </span>
        {%- endfor -%}
        </span>
    {%- endif -%}
    {%- if options['addAgeBadge'] -%}
        {% if person.age is not null and person.deathDate is null %}
        <span>({{- 'years_old'|trans({ 'age': person.age }) -}})</span>
        {% endif %}
    {% endif %}
    {%- if options['suffixText'] is defined -%}
        {% for o in options['suffixText'] %}
            <span>{{ o }}</span>
        {% endfor %}
    {% endif %}
{% endmacro raw %}

{% macro label(person, options) %}
    <div class="entity-label">
        <div class="denomination {{ 'h' ~ options['hLevel'] }}">
            {%- if options['addLink'] and is_granted('CHILL_PERSON_SEE', person) -%}
                <a href="{{ chill_path_add_return_path('chill_person_view', { 'person_id': person.id }) }}">
            {%- endif -%}

                {% if options['customArea']['beforeLabel'] is defined %}
                    {{ options['customArea']['beforeLabel'] }}
                {% endif %}

                {{ _self.raw(person, options) }}

                {% if options['customArea']['afterLabel'] is defined %}
                    {{ options['customArea']['afterLabel'] }}
                {% endif %}

            {%- if options['addLink'] and is_granted('CHILL_PERSON_SEE', person) -%}
                </a>
            {%- endif -%}

            {%- if options['addEntity'] -%}
                <span class="badge rounded-pill bg-secondary">{{ 'Person'|trans }}</span>
            {%- endif -%}
        </div>
        {%- if options['addInfo'] -%}
            <p class="moreinfo">
                {% if person.gender is not null %}{{ person.gender.icon|chill_entity_render_box }} {% endif %}
            {%- if person.deathdate is not null -%}
                {%- if person.birthdate is not null -%}
                    {# must be on one line to avoid spaces with dash #}
                    <time datetime="{{ person.birthdate|date('Y-m-d') }}" title="{{ 'birthdate'|trans|e('html_attr') }}">{{ person.birthdate|format_date("medium") }}</time>&#x2013;
                {%- else -%}
                    <span>{{ 'Date of death'|trans }}: </span>
                {%- endif -%}
                {#- must be on one line to avoid spaces with dash -#}
                <time datetime="{{ person.deathdate|date('Y-m-d') }}" title="{{ 'deathdate'|trans }}">{{ person.deathdate|format_date("medium") }}</time>
                {%- if options['addAge'] -%}
                    <span class="age">&nbsp;{{ 'years_old'|trans({ 'age': person.age }) }}</span>
                {%- endif -%}
{#                {%- if options['addId'] -%}#}
{#                    {%- set personId = person|chill_person_id_render_text %}#}
{#                    <span class="id-number" title="{{ 'Person'|trans ~ ' ' ~ personId }}">#}
{#                    ({{ personId }})#}
{#                </span>#}
{#                {%- endif -%}#}
            {%- elseif person.birthdate is not null -%}
                <time datetime="{{ person.birthdate|date('Y-m-d') }}" title="{{ 'Birthdate'|trans }}">
                {{ 'Born the date'|trans({'gender': person.gender ? person.gender.genderTranslation.value : 'neutral',
                    'birthdate': person.birthdate|format_date("medium") }) }}
                </time>
                {%- if options['addAge'] -%}
                    <span class="age">{{- 'years_old'|trans({ 'age': person.age }) -}}</span>
                {%- endif -%}
            {%- endif -%}
            {%- if options['addId'] -%}
                {%- set personId = person|chill_person_id_render_text %}
                <span class="id-number same-size" title="{{ 'Person'|trans ~ ' ' ~ personId }}">
                    ({{ personId }})
                </span>
            {%- endif -%}
            </p>
        {%- endif -%}
    {#- tricks to remove easily whitespace after template -#}
    {%- if true -%}</div>{%- endif -%}
{% endmacro label %}


{%- if render == 'raw' -%}
    <span class="entity-raw">
        {{ _self.raw(person, options) }}
    </span>
{%- endif -%}

{%- if render == 'label' -%}
    {{ _self.label(person, options) }}
{%- endif -%}

{%- if render == 'bloc' -%}
<div class="item-row entity-bloc">
    <div class="item-col">
        {{ _self.label(person, options) }}
    </div>
    <div class="item-col">
        <ul class="list-content fa-ul">
            {% if person.getCurrentPersonAddress is not null %}
                {% set multiline = (options['address_multiline']) ? true : false %}
                {{ person.getCurrentPersonAddress|chill_entity_render_box({
                    'render': 'list',
                    'with_picto': true,
                    'multiline': multiline,
                    'with_valid_from': false
                }) }}
            {% endif %}
            {% if person.phonenumber is not null %}
                <li>
                    <i class="fa fa-li fa-phone"></i>
                    <a href="{{ 'tel:' ~ person.phonenumber|phone_number_format('E164') }}">
                        {{ person.phonenumber|chill_format_phonenumber }}
                    </a>
                </li>
            {% endif %}
            {% if person.mobilenumber is not null %}
                <li>
                    <i class="fa fa-li fa-mobile"></i><a href="{{ 'tel:' ~ person.mobilenumber|phone_number_format('E164') }}">
                        {{ person.mobilenumber|chill_format_phonenumber }}
                    </a>
                </li>
            {% endif %}
            {% if person.phonenumber is null and person.mobilenumber is null %}
                <li>
                    <i class="fa fa-li fa-phone"></i>
                    <span class="chill-no-data-statement">{{ 'No data given'|trans }}</span>
                </li>
            {% endif %}
            {% if person.email is not empty %}
                <li>
                    <i class="fa fa-li fa-envelope-o"></i><a href="{{ 'mailto:' ~ person.email }}">
                        {{ person.email }}
                    </a>
                </li>
            {% else %}
                <li>
                    <i class="fa fa-li fa-envelope-o"></i>
                    <span class="chill-no-data-statement">{{ 'No data given'|trans }}</span>
                </li>
            {% endif %}
            {% if options['addCenter'] and person|chill_resolve_center|length > 0 %}
                <li>
                    <i class="fa fa-li fa-long-arrow-right"></i>
                    {% for c in person|chill_resolve_center %}
                        {{  c.name|upper }}{% if not loop.last %}, {% endif %}
                    {% endfor %}
                </li>
            {% endif %}
        </ul>
        <ul class="record_actions">
            {% if options['customButtons']['before'] is defined %}
                {{ options['customButtons']['before'] }}
            {% endif %}

            {%- if options['customButtons']['replace'] is defined -%}
                {{ options['customButtons']['replace'] }}
            {%- elseif is_granted('CHILL_PERSON_SEE', person) and options['addLink'] -%}
                <li>
                    <a class="btn btn-sm btn-show" title="{{ 'Show person'|trans }}"
                    href="{{ path('chill_person_view', { person_id: person.id }) }}"></a>
                </li>
            {%- else -%}
            {%- endif -%}

            {% if options['customButtons']['after'] is defined %}
                {{ options['customButtons']['after'] }}
            {% endif %}
        </ul>
    </div>
</div>
{%- endif -%}
