{% extends 'Admin/layout.html.twig' %}

{% trans_default_domain 'form' %}

{% set demandePEEStateRegistered = constant('AppBundle\\Enum\\DemandePEEState::REGISTERED') %}
{% set demandePEEStatePreregistered = constant('AppBundle\\Enum\\DemandePEEState::PREREGISTERED') %}
{% set demandePEEStateWaiting = constant('AppBundle\\Enum\\DemandePEEState::WAITING') %}

{% block body %}
    {{ knp_menu_render('actions_opes_pee') }}

    {# Formulaire de recherche #}
    <form action="{{ path('admin_operation_pee') }}" class="search-admin" method="get">
        <fieldset>
            <div class="search-fields">
                <p>
                    <label for="form_search_date">{{ 'form.date.visit.label'|trans }}</label>
                    <input class="date" id="form_search_date" name="date" type="text" value="{{ date ? date|date('d/m/Y') : '' }}" />
                </p>
                <p>
                    <label for="form_search_company">Entreprise et site</label>
                    <input id="form_search_company" type="text" name="company" value="{{ company }}" />
                </p>
                <p>
                    <label for="form_search_state">{{ 'form.state.label'|trans }}</label>
                    <select id="form_search_state" name="state">
                        <option value=""{% if state_selected is empty %} selected="selected"{% endif %}>- Tous -</option>
                        {% for key, value in states %}
                            <option value="{{ key }}"{% if key == state_selected %} selected="selected"{% endif %}>{{ value }}</option>
                        {% endfor %}
                    </select>
                </p>
                <p>
                    <label for="form_search_academy">{{ 'form.academy.label'|trans }}</label>
                    <select id="form_search_academy" name="academy">
                        <option value=""{% if academy_selected is empty %} selected="selected"{% endif %}>- Toutes -</option>
                        {% for academy in academies %}
                            <option value="{{ academy }}"{% if academy == academy_selected %} selected="selected"{% endif %}>{{ academy }}</option>
                        {% endfor %}
                    </select>
                </p>
            </div>
        </fieldset>
        <p>
            <input class="btn" type="submit" value="{{ 'action.search'|trans({}, 'action') }}" />
        </p>
    </form>

    {# Liste de résultats #}
    <table class="tableBo">
        <thead>
            <tr>
                <td>
                    {% set sort_classname_additional = null %}
                    {% set sort_title = null %}
                    {% set sort_url = null %}

                    {% if sortby=='date' and sortdirection=='asc' %}
                        {% set sort_classname_additional='sort-asc' %}
                        {% set sort_title = 'Trier par ordre descendant' %}
                        {% set sort_url = path('admin_operation_pee', {type:'all', page:pageactuelle, academy:academy_selected, company:company, state:state_selected, sortby:'date', sortdirection:'desc' }) %}
                    {% elseif sortby=='date' and sortdirection=='desc' %}
                        {% set sort_classname_additional='sort-desc' %}
                        {% set sort_title = 'Trier par ordre ascendant' %}
                        {% set sort_url = path('admin_operation_pee', {type:'all', page:pageactuelle, academy:academy_selected, company:company, state:state_selected, sortby:'date', sortdirection:'asc' }) %}
                    {% else %}
                        {% set sort_classname_additional='sort-none' %}
                        {% set sort_title = 'Trier par ordre descendant' %}
                        {% set sort_url = path('admin_operation_pee', {type:'all', page:pageactuelle, academy:academy_selected, company:company, state:state_selected, sortby:'date', sortdirection:'desc' }) %}
                    {% endif %}

                    <a class="sort {{ sort_classname_additional }}" title="{{ sort_title }}" href="{{ sort_url }}">{{ 'form.date.visit.label'|trans }}</a>
                </td>
                <td>
                    {% if sortby=='entreprise' and sortdirection=='asc' %}
                        {% set sort_classname_additional='sort-asc' %}
                        {% set sort_title = 'Trier par ordre descendant' %}
                        {% set sort_url = path('admin_operation_pee', {type:'all', page:pageactuelle, academy:academy_selected, company:company, state:state_selected, sortby:'entreprise', sortdirection:'desc' }) %}
                    {% elseif sortby=='entreprise' and sortdirection=='desc' %}
                        {% set sort_classname_additional='sort-desc' %}
                        {% set sort_title = 'Trier par ordre ascendant' %}
                        {% set sort_url = path('admin_operation_pee', {type:'all', page:pageactuelle, academy:academy_selected, company:company, state:state_selected, sortby:'entreprise', sortdirection:'asc' }) %}
                    {% else %}
                        {% set sort_classname_additional='sort-none' %}
                        {% set sort_title = 'Trier par ordre descendant' %}
                        {% set sort_url = path('admin_operation_pee', {type:'all', page:pageactuelle, academy:academy_selected, company:company, state:state_selected, sortby:'entreprise', sortdirection:'desc' }) %}
                    {% endif %}

                    <a class="sort {{ sort_classname_additional }}" title="{{ sort_title }}" href="{{ sort_url }}">Entreprise et site</a>
                </td>
                <td>{{ 'form.state.label'|trans }}</td>
                <td>Participants acceptés</td>
                <td>Inscrits</td>
                <td>Pièces d’identité</td>
                <td>Pré-inscrit</td>
                <td>Liste d'attente</td>
                <td colspan="4">{{ 'word.actions'|trans({}, 'word') }}</td>
            </tr>
        </thead>
        <tbody>
            {% for operation in operations %}
                {%-
                    set oDateVisite = operation.dateVisite is not null
                        ? operation.dateVisite|date('d/m/Y')
                        : 'sentence.date.operationPEE.missing'|trans({}, 'sentence')
                -%}
                <tr class="delete-element-container">
                    <td class="center">
                        <a href="{{ path('admin_operation_pee_edit', {id:operation.id }) }}">
                            {{- oDateVisite -}}
                        </a>
                    </td>
                    <td>
                        <a href="{{ path('admin_operation_pee_edit', {id:operation.id }) }}">
                            <strong>{{ operation.company }}</strong> {{ operation.site }}
                        </a>
                    </td>
                    <td class="center">
                        <a href="{{ path('admin_operation_pee_edit', {id:operation.id }) }}">
                            {{ operation.state|transconst('AppBundle\\Enum\\OperationPEEState') }}
                        </a>
                    </td>
                    <td class="center">{{ operation.nbpeople }}</td>
                    <td class="center">
                        {%  if  inscrits[loop.index0][demandePEEStateRegistered] is defined and inscrits[loop.index0][demandePEEStateRegistered] is not empty %}
                            <a href="{{ path('admin_operation_pee_export', {'operationPEE': operation.id, 'state': demandePEEStateRegistered}) }}">
                            {{ inscrits[loop.index0][demandePEEStateRegistered] }}
                            </a>
                        {%  else  %}
                            0
                        {% endif %}
                    </td>

                    <th>{%  if  inscrits[loop.index0][demandePEEStateRegistered] is defined and inscrits[loop.index0][demandePEEStateRegistered] is not empty %}<a href="{{ path('admin_operation_pee_export_pieces', {'id': operation.id}) }}" class="btn btn-export">Télécharger</a> {% endif %}</th>

                    <td class="center">
                        {%  if  inscrits[loop.index0][demandePEEStatePreregistered] is defined and inscrits[loop.index0][demandePEEStatePreregistered] is not empty %}
                            <a href="{{ path('admin_operation_pee_export', {'operationPEE': operation.id, 'state': demandePEEStatePreregistered}) }}">
                                {{ inscrits[loop.index0][demandePEEStatePreregistered] }}
                            </a>
                        {%  else  %}
                            0
                        {% endif %}
                    </td>
                    <td class="center">  {%  if  inscrits[loop.index0][demandePEEStateWaiting] is defined and inscrits[loop.index0][demandePEEStateWaiting] is not empty %}
                            <a href="{{ path('admin_operation_pee_export', {'operationPEE': operation.id, 'state': demandePEEStateWaiting}) }}">
                                {{ inscrits[loop.index0][demandePEEStateWaiting] }}
                            </a>
                        {%  else  %}
                            0
                        {% endif %}
                    </td>
                    <td class="action">
                        <a
                            class="edit"
                            href="{{ path('admin_operation_pee_edit', {id:operation.id}) }}"
                            title="Modifier: {{ operation.company }} {{ operation.site }}, {{ oDateVisite|lower }}"
                        >
                            <span>Modifier</span>
                        </a>
                    </td>
                    <td class="action">
                        <a
                            class="duplicate"
                            title="Dupliquer: {{ operation.company }} {{ operation.site }}, {{ oDateVisite|lower }}"
                            href="{{ path('admin_operation_pee_duplicate', {id:operation.id}) }}"
                        >
                            <span>Dupliquer</span>
                        </a>
                    </td>
                    <td class="action">
                        <input
                            {% if operation.isactive %} checked="checked"{% endif %}
                            class="input-active"
                            data-type="OperationPEE"
                            data-url="{{ path('element_actif') }}"
                            type="checkbox"
                            value="{{ operation.id }}"
                        />
                        <span class="checkbox" title="{% if operation.isactive %}Désactiver{% else %}Activer{% endif %}: {{ operation.company }} {{ operation.site }}, {{ oDateVisite|lower }}"></span>
                    </td>
                    <td class="action">
                        <a
                            class="delete-element"
                            title="Suprimer: {{ operation.company }} {{ operation.site }}, {{ oDateVisite|lower }}"
                            href="{{ path('admin_operation_pee_delete', {operationpee:operation.id }) }}"
                        >
                            <span>{{ 'action.delete.label'|trans({}, 'action') }}</span>
                        </a>
                    </td>
                </tr>
            {% endfor %}
        </tbody>
    </table>

    {{ include('Admin/Paginator/pagination.html.twig', {pages:nbPages}) }}
{% endblock %}
