{% set oStateFenced = constant('AppBundle\\Enum\\OperationPEEState::FENCED') %}

{% spaceless %}
    {% for o in operations %}
        {# User has (or not) an application for this operation #}
        {% set demandePee = (1 == o.details.demandepee|length) ? o.details.demandepee|first : false %}

        <div class="listpee-op">
            <h4 class="title">{{ o.details.company ~ ' ' ~ o.details.site }}</h4>

            <table class="listpee-op-details">
                {# DemandePEE state #}
                <tr>
                    <th class="listpee-op-label">
                        {% if false != demandePee %}
                            {% if
                                constant('AppBundle\\Enum\\DemandePEEState::REGISTERED') == demandePee.state
                                or constant('AppBundle\\Enum\\DemandePEEState::PREREGISTERED') == demandePee.state
                            %}
                                {{ include('Front/_component/icon.html.twig', {id:'calendar--calendar-ok'}) }}
                            {% elseif constant('AppBundle\\Enum\\DemandePEEState::WAITING') == demandePee.state %}
                                {{ include('Front/_component/icon.html.twig', {id:'calendar--calendar-later'}) }}
                            {% elseif constant('AppBundle\\Enum\\DemandePEEState::WITHDRAWAL') == demandePee.state %}
                                {{ include('Front/_component/icon.html.twig', {id:'calendar--calendar-ko'}) }}
                            {% endif %}
                        {% else %}
                            {% if oStateFenced == o.details.state %}
                                {{ include('Front/_component/icon.html.twig', {id:'calendar--calendar-ko'}) }}
                            {% elseif registeredQty >= max_pee_applications_per_user %}
                                {{ include('Front/_component/icon.html.twig', {id:'calendar--calendar-limit-reached'}) }}
                            {% else %}
                                {% if o.details.urlExternalRegistration is null %}
                                    <input
                                        class="ope"
                                        id="ope-{{ o.details.id }}"
                                        name="operation[]"
                                        type="checkbox"
                                        value="{{ o.details.id }}"
                                    />
                                    {{
                                        include('Front/_component/icon.html.twig', {
                                            id:'checkbox--checkbox-unchecked',
                                            class: 'icon-unchecked',
                                        })
                                    }}
                                    {{
                                        include('Front/_component/icon.html.twig', {
                                            id:'checkbox--checkbox-checked',
                                            class: 'icon-checked',
                                        })
                                    }}
                                {% else %}
                                    {{ include('Front/_component/icon.html.twig', {id:'calendar--calendar-later'}) }}
                                {% endif %}
                            {% endif %}
                        {% endif %}
                    </th>
                    <td class="listpee-op-value">
                        {% if false != demandePee %}
                            {{ demandePee.state }}
                        {% else %}
                            {% if oStateFenced == o.details.state %}
                                Les inscriptions sont closes
                            {% elseif registeredQty >= max_pee_applications_per_user %}
                                Limite dʼinscriptions atteinte
                            {% else %}
                                {% if o.details.urlExternalRegistration is null %}
                                    <label for="ope-{{ o.details.id }}">Je mʼy inscris{% if o.details.nbPeople - o.placesTaken <= 0 %} en liste dʼattente{% endif %}</label>
                                {% else %}
                                    <a href="{% if o.details.urlExternalRegistration matches '/^(?!(https?|ftp):\\/\\/)/' %}http://{% endif %}{{ o.details.urlExternalRegistration }}" target="_blank" title="Ouvre la page dans une nouvelle fenêtre">Je mʼinscris dʼabord auprès du rectorat</a>
                                {% endif %}
                            {% endif %}
                        {% endif %}
                    </td>
                </tr>

                {# DemandePEE special type CERPEP #}
                <tr>
                    <th class="listpee-op-label">
                        {% set cerpepStillAvailable -%}
                            {%- if false == demandePee or demandePee.state not in [constant('AppBundle\\Enum\\DemandePEEState::WITHDRAWAL'), constant('AppBundle\\Enum\\DemandePEEState::REGISTERED')] -%}
                                {{ true }}
                            {%- else -%}
                                {{ false }}
                            {%- endif -%}
                        {%- endset %}

                        <input
                            {{ false != demandePee and constant('AppBundle\\Enum\\DemandePEEType::CERPEP') == demandePee.type ? ' checked="checked"' : '' }}
                            class="ope-cerpep"
                            {{ cerpepStillAvailable ? '' : ' disabled' }}
                            id="ope-cerpep-{{ o.details.id }}"
                            name="operation_cerpep[]"
                            type="checkbox"
                            value="{{ o.details.id }}"
                        />
                        {{
                            include('Front/_component/icon.html.twig', {
                                id:'checkbox--checkbox-unchecked',
                                class: 'icon-unchecked',
                            })
                        }}
                        {{
                            include('Front/_component/icon.html.twig', {
                                id:'checkbox--checkbox-checked',
                                class: 'icon-checked',
                            })
                        }}
                    </th>
                    <td class="listpee-op-value">
                        <label for="ope-cerpep-{{ o.details.id }}">
                            {{ constant('AppBundle\\Enum\\DemandePEEType::CERPEP')|transconst('AppBundle\\Enum\\DemandePEEType')|raw }}
                        </label>
                        <button
                                class="js-simple-tooltip tooltip-info"
                                data-simpletooltip-text="Cochez cette case si vous êtes un candidat du CEFPEP  (Centre d’Études et de Formation en Partenariat avec les Entreprises et les Professions). Vous recevrez ainsi une invitation du CEFPEP  vous permettant d'obtenir un ordre de mission auprès de votre chef d'établissement ainsi qu'une attestation de formation à l'issue du stage. Cette option n'est possible que si vous n'êtes pas déjà inscrit via le PAF de votre académie."
                                type="button"
                            >
                            {{ include('Front/_component/icon.html.twig', {id: 'info', title: 'Aide'}) }}
                        </button>
                    </td>
                </tr>

                {# Operation visit date #}
                <tr>
                    <th class="listpee-op-label">Le</th>
                    <td class="listpee-op-value">
                        {% if o.details.dateVisite is null %}
                            {{ 'sentence.date.operationPEE.missing'|trans({}, 'sentence') }}
                        {% else %}
                            <strong>{{ o.details.dateVisite|date("d/m/Y") }}</strong> de <strong>{{ o.details.startTime|date('H\\hi') }}</strong> à <strong>{{ o.details.endTime|date('H\\hi') }}</strong>
                        {% endif %}
                    </td>
                </tr>

                {# Operation company name #}
                {% if o.details.company is not empty %}
                    <tr>
                        <th class="listpee-op-label">chez</th>
                        <td class="listpee-op-value"><strong>{{ o.details.company }}</strong></td>
                    </tr>
                {% endif %}

                {# Operation city #}
                {% if o.details.city is not empty %}
                    <tr>
                        <th class="listpee-op-label">à</th>
                        <td class="listpee-op-value"><strong>{{ o.details.city }}</strong></td>
                    </tr>
                {% endif %}

                {# Operation academy #}
                {% if o.details.academy is not empty %}
                    <tr>
                        <th class="listpee-op-label"><abbr title="Académie">ac.</abbr></th>
                        <td class="listpee-op-value"><strong>{{ o.details.academy }}</strong></td>
                    </tr>
                {% endif %}

                {# Subscription deadline / Number of seats left #}
                {% if false == demandePee and oStateFenced != o.details.state %}
                    <tr>
                        <td class="listpee-op-value" colspan="2">Fin des inscriptions le {{ o.details.unpublish|date("d/m/Y") }}.</td>
                    </tr>

                    <tr>
                        <td class="listpee-op-value" colspan="2">
                            {% if o.details.urlExternalRegistration is null %}
                                {% if o.details.nbPeople - o.placesTaken > 0 %}
                                    Il reste {{ o.details.nbPeople - o.placesTaken }} place{% if o.details.nbPeople - o.placesTaken >1 %}s{% endif %}.
                                {% else %}
                                    La visite est complète, mais vous pouvez tout de même vous inscrire en liste dʼattente.
                                {% endif %}
                            {% else %}
                                Les inscriptions auprès de CGénial ouvriront lorsque celles du rectorat seront fermées, dans la limite des places disponibles.
                            {% endif %}
                        </td>
                    </tr>

                    <tr>
                        <td class="listpee-op-value" colspan="2">
                            <a href="{{ path('operation_pee_download', {id: o.details.id}) }}">
                                {{ 'action.download.pdf'|trans({}, 'action') }}
                            </a>
                        </td>
                    </tr>
                {% endif %}
            </table>
        </div>
    {% else %}
        <p>Aucune visite nʼest disponible{% if academy_name is not null %} pour lʼacadémie de {{ academy_name }}{% endif %}.</p>
    {% endfor %}
{% endspaceless %}