{% extends '@ChillPerson/AccompanyingCourse/layout.html.twig' %}

{% block title %}
    {{ 'Resume Accompanying Course'|trans }}
{% endblock %}

{% macro insert_onthefly(type, entity, parent = null) %}
    {% include '@ChillMain/OnTheFly/_insert_vue_onthefly.html.twig' with {
        action: 'show', displayBadge: true,
        targetEntity: { name: type, id: entity.id },
        buttonText: entity|chill_entity_render_string,
        isDead: entity.deathdate is defined and entity.deathdate is not null,
        parent: parent
    } %}
{% endmacro %}

{% block js %}
    {{ parent() }}
    {{ encore_entry_script_tags('page_accompanying_course_index_person_locate') }}
    {{ encore_entry_script_tags('page_accompanying_course_index_masonry') }}
    {{ encore_entry_script_tags('mod_notification_toggle_read_status') }}
{% endblock %}

{% block css %}
    {{ parent() }}
    {{ encore_entry_link_tags('mod_notification_toggle_read_status') }}
{% endblock %}

{% block content %}
<div class="accompanyingcourse-resume">
    <div id="dashboards" class="row g-3" data-masonry='{"percentPosition": true }'>

        {% if 'CLOSED' == accompanyingCourse.step %}
            <div class="mbloc col col-sm-6 col-lg-4">
                <div class="warnings">
                    <div class="alert alert-danger">
                        <h2>{{ 'This course is closed'|trans }}</h2>
                        {% if accompanyingCourse.closingMotive is not same as null %}
                            <p>
                                {{ 'Closing motive'|trans }} : {{ accompanyingCourse.closingMotive.name|localize_translatable_string }}
                            </p>
                        {% endif %}
                    </div>
                </div>
            </div>
        {% endif %}

        {% if 'DRAFT' == accompanyingCourse.step %}
            <div class="mbloc col col-sm-6 col-lg-4">
                <div class="warnings">
                    {% include '@ChillPerson/AccompanyingCourse/_still_draft.html.twig' %}
                </div>
            </div>
        {% endif %}

        {% if 'DRAFT' != accompanyingCourse.step %}
            {% if withoutHousehold|length > 0 %}
                <div class="mbloc col col-sm-6 col-lg-4">
                    <div class="warnings">
                        {% include '@ChillPerson/AccompanyingCourse/_join_household.html.twig' %}
                    </div>
                </div>
            {% endif %}
        {% endif %}

        {% if accompanyingCourse.locationStatus == 'address'
            or accompanyingCourse.locationStatus == 'none' %}
            <div class="mbloc col col-sm-6 col-lg-4">
                <div class="warnings">
                    {% include '@ChillPerson/AccompanyingCourse/_warning_address.html.twig' %}
                </div>
            </div>
        {% endif %}

        {% if accompanyingCourse.locationStatus != 'none' %}
        <div class="mbloc col col-sm-6 col-lg-4">
            <div class="location">
                {% if accompanyingCourse.locationStatus == 'person' %}
                    <h2>{{ 'This course is located by'|trans }}</h2>
                    <h4>{{ accompanyingCourse.personLocation|chill_entity_render_string }}</h4>
                {%  elseif accompanyingCourse.locationStatus == 'address' %}
                    <h4>{{ 'This course has a temporarily location'|trans }}</h4>
                {% endif %}
                {{ accompanyingCourse.location|chill_entity_render_box }}
            </div>
        </div>
        {% endif %}

        {% if accompanyingCourse.pinnedComment is not empty %}
            <div class="mbloc col col-sm-6 col-lg-8">
                <div class="comment">
                    <h4 class="item-key visually-hidden">{{ 'Pinned comment'|trans }}</h4>
                    <blockquote class="chill-user-quote">
                        <i class="fa fa-flag float-end text-chill-gray" title="{{ 'pinned'|trans }}"></i>
                        {{ accompanyingCourse.pinnedComment.content|u.truncate(250, '…', false)|chill_markdown_to_html }}
                        {% if accompanyingCourse.pinnedComment.content|length > 250 %}
                            <a href="{{ chill_path_add_return_path('chill_person_accompanying_period_comment_list', {'accompanying_period_id': accompanyingCourse.id}) }}">{{ 'Read more'|trans }}</a>
                        {% endif %}
                        <div class="metadata">
                            {{ 'Last updated by'| trans }}
                            <span class="user">
                            {{ accompanyingCourse.pinnedComment.updatedBy|chill_entity_render_box({'at_date': accompanyingCourse.pinnedComment.updatedAt}) }}
                        </span>
                            {{ 'on'|trans ~ ' ' }}
                            <span class="date">
                            {{ accompanyingCourse.pinnedComment.updatedAt|format_datetime("medium", "short") }}
                        </span>
                        </div>
                    </blockquote>
                </div>
            </div>
        {% endif %}

        {% if accompanyingCourse.getCurrentParticipations is not empty %}
            <div class="mbloc col col-sm-6 col-lg-4">
                <div class="persons">
                    <h4 class="item-key">{{ 'Persons associated'|trans }}</h4>
                    {% for r in accompanyingCourse.getCurrentParticipations %}
                        {{ _self.insert_onthefly('person', r.person) }}
                    {% endfor %}
                </div>
            </div>
        {% endif %}

        {% if accompanyingCourse.resources is not empty %}
            <div class="mbloc col col-sm-6 col-lg-4">
                <div class="resources">
                    <h4 class="item-key">{{ 'Resources'|trans }}</h4>

                    {% for r in accompanyingCourse.resources %}
                        {% set parent = {
                            'type': 'accompanying_period_resource',
                            'id': r.id,
                            'comment': r.comment,
                            'parent': {
                                'type': 'accompanying_period',
                                'id': accompanyingCourse.id
                            }
                        } %}
                        {% if r.person is not null %}
                            {{ _self.insert_onthefly('person', r.person, parent) }}
                        {% elseif r.thirdParty is not null %}
                            {{ _self.insert_onthefly('thirdparty', r.thirdParty, parent) }}
                        {% endif %}
                    {% endfor %}
                </div>
            </div>
        {% endif %}

        {% if accompanyingCourse.scopes is not empty %}
            <div class="mbloc col col-sm-6 col-lg-4">
                <div class="scopes">
                    <h4 class="item-key">{{ 'Concerned scopes'|trans }}</h4>
                    <div>
                        {% for s in accompanyingCourse.scopes  %}
                            <span>{{ s.name|localize_translatable_string|upper }}</span>{% if not loop.last %}, {% endif %}
                        {% endfor %}
                    </div>
                </div>
            </div>
        {% endif %}

        {% if accompanyingCourse.requestorPerson is not null or accompanyingCourse.requestorThirdParty is not null %}
            <div class="mbloc col col-sm-6 col-lg-4">
                <div class="requestor">
                    {% if accompanyingCourse.requestorPerson is not null %}
                        <h4 class="item-key">{{ 'Requestor'|trans }}</h4>
                        {% if accompanyingCourse.requestorAnonymous %}
                            <div class="confidential toggle-close-twig"><p>{{ _self.insert_onthefly('person', accompanyingCourse.requestorPerson) }}</p></div>
                        {% else %}
                            {{ _self.insert_onthefly('person', accompanyingCourse.requestorPerson) }}
                        {% endif %}
                    {% elseif accompanyingCourse.requestorThirdParty is not null %}
                        <h4 class="item-key">{{ 'Requestor'|trans }}</h4>
                        {% if accompanyingCourse.requestorAnonymous %}
                            <div class="confidential toggle-close-twig"><p>{{ _self.insert_onthefly('thirdparty', accompanyingCourse.requestorThirdParty) }}</p></div>
                        {% else %}
                            {{ _self.insert_onthefly('thirdparty', accompanyingCourse.requestorThirdParty) }}
                        {% endif %}
                    {% endif %}
                </div>
            </div>
        {% endif %}

        {% if accompanyingCourse.origin is not empty %}
            <div class="mbloc col col-sm-6 col-lg-4">
                <div class="origin">
                    <h4 class="item-key">{{ 'Origin'|trans }}</h4>
                    {{ accompanyingCourse.origin.label|localize_translatable_string|capitalize }}
                </div>
            </div>
        {% endif %}

        {% if accompanyingCourse.administrativeLocation is not null %}
            <div class="mbloc col col-sm-6 col-lg-4">
                <div class="administrative-location">
                    <h4 class="item-key">{{ 'accompanying_course.administrative_location'|trans }}</h4>
                    {{ accompanyingCourse.administrativeLocation.name }} ({{ accompanyingCourse.administrativeLocation.locationType.title|localize_translatable_string }})
                </div>
            </div>
        {% endif %}

        {% if accompanyingCourse.step != 'DRAFT' %}
            <div class="mbloc col col-sm-6 col-lg-8 col-xxl-4">
                <div class="notification-counter">
                    <h4 class="item-key">{{ 'notification.Notifications'|trans }}</h4>
                    {% set notif_counter = chill_count_notifications('Chill\\PersonBundle\\Entity\\AccompanyingPeriod', accompanyingCourse.id) %}
                    {% if notif_counter.total > 0 %}
                        <div class="my-2">
                            <a href="#notification-list">
                                {{ chill_counter_notifications('Chill\\PersonBundle\\Entity\\AccompanyingPeriod', accompanyingCourse.id) }}
                            </a>
                        </div>
                    {% endif %}
                    <div class="d-grid gap-2 {% if accompanyingCourse.hasUser and accompanyingCourse.user is not same as(app.user) %}btn-group{% endif %}" {% if accompanyingCourse.hasUser and accompanyingCourse.user is not same as(app.user) %}role="group"{% endif %}>
                        {% if accompanyingCourse.hasUser and accompanyingCourse.user is not same as(app.user) %}
                        <button id="btnGroupNotifyButtons" type="button" class="btn btn-notify dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
                            {{ 'notification.Notify'|trans }}
                        </button>
                        <ul class="dropdown-menu" aria-labelledby="btnGroupNotifyButtons">
                            <li>
                                <a class="dropdown-item" href="{{ chill_path_add_return_path('chill_main_notification_create', {'entityClass': 'Chill\\PersonBundle\\Entity\\AccompanyingPeriod', 'entityId': accompanyingCourse.id, 'tos': [accompanyingCourse.user.id]}) }}">
                                    {{ 'notification.Notify referrer'|trans }}
                                </a>
                            </li>
                            <li>
                                <a class="dropdown-item" href="{{ chill_path_add_return_path('chill_main_notification_create', {'entityClass': 'Chill\\PersonBundle\\Entity\\AccompanyingPeriod', 'entityId': accompanyingCourse.id}) }}">
                                    {{ 'notification.Notify any'|trans }}
                                </a>
                            </li>
                        </ul>
                        {% else %}
                            <a class="btn btn-notify" href="{{ chill_path_add_return_path('chill_main_notification_create', {'entityClass': 'Chill\\PersonBundle\\Entity\\AccompanyingPeriod', 'entityId': accompanyingCourse.id}) }}">
                                {{ 'notification.Notify'|trans }}
                            </a>
                        {% endif %}
                    </div>
                </div>
            </div>
        {% endif %}

        {% if counters.activities > 0 %}
            <div class="mbloc col col-sm-6 col-lg-4">
                <div class="count-activities">
                    <div class="count-item">{{ counters.activities }}</div>
                    <div class="count-item-label">
                        {% if counters.activities == 1 %}
                            {{ 'Activity'|trans }}
                        {% else %}
                            {{ 'Activities'|trans }}
                        {% endif %}
                    </div>
                </div>
            </div>
        {% endif %}

        {% if counters.works > 0 %}
            <div class="mbloc col col-sm-6 col-lg-4">
                <div class="count-works">
                    <div class="count-item">{{ counters.openWorks }} / {{ counters.works }}</div>
                    <div class="count-item-label">{{ 'accompanying_course_work.On-going works over total'|trans }}</div>
                </div>
            </div>
        {% endif %}

    </div>

    <div class="social-actions my-4">
        <h2 class="mb-3 visually-hidden">{{ 'Last social actions'|trans }}</h2>
        {% include '@ChillPerson/AccompanyingCourseWork/list_recent_by_accompanying_period.html.twig' with {'buttonText': false } %}
    </div>

    {% block contentActivity %}
    <div class="activities my-4">
        {% set person = null %}

        {% set person_id = null %}
        {% if person %}
            {% set person_id = person.id %}
        {% endif %}

        {% set accompanying_course_id = null %}
        {% if accompanyingCourse %}
            {% set accompanying_course_id = accompanyingCourse.id %}
        {% endif %}

        <h2 class="mb-3 visually-hidden">{{ 'Last activities' |trans }}</h2>
        {% include '@ChillActivity/Activity/list_recent.html.twig' with { 'context': 'accompanyingCourse', 'no_action': true } %}
    </div>
    {% endblock %}

    <div class="notification notification-list">
        {% set notifications = chill_list_notifications('Chill\\PersonBundle\\Entity\\AccompanyingPeriod', accompanyingCourse.id) %}
        {% if notifications is not empty %}
            {{ notifications|raw }}
        {% endif %}
    </div>
</div>
{% endblock %}

{% block block_post_menu %}
<div class="post-menu pt-4"></div>
{% endblock %}
