{% extends '@ChillMain/layout.html.twig' %}

{% block title 'period_by_user_list.Period by user'|trans %}

{% block js %}
    {{ encore_entry_script_tags('mod_set_referrer') }}
    {{ encore_entry_script_tags('mod_pickentity_type') }}
    {{ encore_entry_script_tags('mod_pick_postal_code') }}
{% endblock %}

{% block css %}
    {{ encore_entry_link_tags('mod_set_referrer') }}
    {{ encore_entry_link_tags('mod_pickentity_type') }}
    {{ encore_entry_link_tags('mod_pick_postal_code') }}
{% endblock %}

{% macro period_meta(period) %}
    {% if is_granted('CHILL_PERSON_ACCOMPANYING_PERIOD_UPDATE', period) %}
        <div class="item-col item-meta">
            {% set job_id = null %}
            {% if period.job is defined and period.job is not null %}
                {% set job_id = period.job.id %}
            {% endif %}
            <span
                data-set-referrer-app="data-set-referrer-app"
                data-set-referrer-accompanying-period-id="{{ period.id }}"
                data-set-referrer-job-id="{{ job_id }}"
            ></span>
        </div>
    {% endif %}
{% endmacro %}


{% macro period_actions(period) %}
    {% if is_granted('CHILL_PERSON_ACCOMPANYING_PERIOD_UPDATE', period) %}
    <li>
        <a href="{{ chill_path_add_return_path('chill_person_accompanying_course_edit', {'accompanying_period_id': period.id}) }}" class="btn btn-edit"></a>
    </li>
    {% endif %}
{% endmacro %}

{% import _self as m %}

{% block content %}
    <div class="row col-10">
        <h1>{{ block('title') }}</h1>

        <div class="row filter-box">
            <div class="col-md-6">
                {{ form_start(form) }}
                {{ form_label(form.user ) }}
                {{ form_widget(form.user, {'attr': {'class': 'select2'}}) }}
                {{ form_label(form.postal_code) }}
                {{ form_widget(form.postal_code) }}
                <ul class="record_actions">
                    <li>
                        <button type="submit" class="btn btn-misc">
                            <i class="fa fa-filter"></i> {{ 'Filter'|trans }}
                        </button>
                    </li>
                </ul>
                {{ form_end(form) }}
            </div>

            <div class="col-md-6">
                {% if userFrom is not null %}
                    <div>
                        {{ form_start(assignForm) }}
                        {{ form_label(assignForm.userTo ) }}
                        {{ form_widget(assignForm.userTo, {'attr': {'class': 'select2'}}) }}
                    </div>
                    <ul class="record_actions">
                        <li>
                            <button type="submit" class="btn btn-update change-icon">
                                {{ 'reassign.Reassign'|trans }}
                            </button>
                        </li>
                    </ul>
                    {{ form_end(assignForm) }}
                {% else %}
                    <div class="alert alert-info">{{ 'reassign.List periods to be able to reassign them'|trans }}</div>
                {% endif %}

            </div>
        </div>

        {% if userFrom is not null %}
            <p><span class="badge rounded-pill bg-primary">{{ paginator.totalItems }}</span> parcours à réassigner (calculé ce jour à {{ null|format_time('medium') }})</p>
        {% endif %}

        <div class="flex-table">
            {% for period in periods %}
                {% include '@ChillPerson/AccompanyingPeriod/_list_item.html.twig' with {'period': period,
                    'recordAction': m.period_actions(period), 'itemMeta': m.period_meta(period), 'show_address': true } %}
            {% else %}
                {% if userFrom is same as(null) %}
                    <p class="chill-no-data-statement">{{ 'period_by_user_list.Pick a user'|trans }}</p>
                {% else %}
                    <p class="chill-no-data-statement">{{ 'period_by_user_list.Any course or no authorization to see them'|trans }}</p>
                {% endif %}
            {% endfor %}
        </div>

        {% if paginator is defined %}
            {{ chill_pagination(paginator) }}
        {% endif %}
    </div>

{% endblock %}
