{% trans_default_domain 'form' %}

{# 
    Create a stringified JSON that contains the list of user coordinates (latitude, longitude).
    According to the usePersonalLocation variable, the coordinates are the one
    - from the user's establishment address (school or company)
    - from the personal address
#}
{% set fieldsNames = {
    'lat': usePersonalLocation ? 'personalLatitude' : 'latitude',
    'lng': usePersonalLocation ? 'personalLongitude' : 'longitude'
} %}
{% set coordinates = [] %}
{% for e in engineers %}
    {% set engineer = e|first %}
    {% set coordinates = coordinates|merge([{
        'latitude': attribute(engineer, fieldsNames.lat),
        'longitude': attribute(engineer, fieldsNames.lng)
    }]) %}
{% endfor %}
<table id="listusers" class="tableBo tablesorter" data-coordinates="{{ coordinates|json_encode }}">
    <thead>
        <tr>
            <th colspan="4">Intervenant</th>
            <th colspan="3">{{ 'form.company.label'|transchoice(1) }}</th>
            <th colspan="4">Trajet</th>
            {# <th colspan="1">Interventions</th> #}
            <th colspan="2">{{ 'word.actions'|trans({}, 'word') }}</th>
        </tr>
        <tr>
            <th>{{ 'form.email.label'|trans }}</th>
            <th>{{ 'form.firstname.label'|trans }}</th>
            <th>{{ 'form.lastname.label'|trans }}</th>
            <th>{{ 'form.phone.landline.label'|trans }}</th>
            <th>{{ 'form.name.label'|trans }}</th>
            <th>CP</th>
            <th>{{ 'form.city.label'|trans }}</th>
            <th>Km souhaité</th>
            <th>Temps souhaité</th>
            <th>Km réel</th>
            <th>Temps voiture</th>
            {# <th>Souhaitées/an</th> #}
            <th>{{ 'form.email.label'|trans }}</th>
            <th>{{ 'action.create.label'|trans({}, 'action') }}</th>
        </tr>
    </thead>
    <tbody>
        {% for engineer in engineers %}
            {% set e, isPreferred, isInterested = engineer|first, engineer.isPreferred, engineer.isInterested %}

            {% set now = "now"|date("Ymd") %}
            {% set mNow = "now"|date("m") %}
            {% if mNow <= 6 %}
                {% set startYear = "August 1st last year"|date("Ymd") %}
                {% set endYear = "July 31th"|date("Ymd") %}
            {% else %}
                {% set startYear = "August 1st"|date("Ymd") %}
                {% set endYear = "Next year July 31th"|date("Ymd") %}
            {% endif %}

            {% set futureOpeBooked = false %}
            {% set nbOpeBooked = 0 %}
            {% for ope in e.ingeitdlc %}
                {% if ope.created|date('Ymd') > startYear and ope.created|date('Ymd') < endYear and ope.type == constant('AppBundle\\Enum\\OperationITDLCType::OPERATION') and ( ope.state == constant('AppBundle\\Enum\\OperationITDLCState::ONGOING') or ope.state == constant('AppBundle\\Enum\\OperationITDLCState::NEW') or ope.state == constant('AppBundle\\Enum\\OperationITDLCState::CLOSED') ) %}
                    {% set nbOpeBooked = nbOpeBooked + 1 %}
                {% endif %}

                {% if ope.created|date('Ymd') > startYear and ope.created|date('Ymd') < endYear and ope.type == constant('AppBundle\\Enum\\OperationITDLCType::OPERATION') and ( ope.state == constant('AppBundle\\Enum\\OperationITDLCState::ONGOING') or ope.state == constant('AppBundle\\Enum\\OperationITDLCState::NEW')) %}
                    {% set futureOpeBooked = true %}
                {% endif %}
            {% endfor %}

            {% set tr_class %}
                {%- if isPreferred -%}
                    highlighted
                {%- else -%}
                    {{ loop.index is odd ? 'odd' : 'even' }}
                {%- endif -%}
            {% endset %}

            {% if futureOpeBooked %}
                {% set tr_class = tr_class ~ ' disabled' %}
            {% endif %}

            {% if isInterested %}
                {% set tr_class = tr_class ~ ' interested' %}
            {% endif %}

            <tr class="{{ tr_class }}" data-latitude="{{ e.latitude }}" data-longitude="{{ e.longitude }}">
                <td>{{ e.email }}</td>
                <td><a href="{{ path('admin_user_edit', {'user':e.id}) }}" target="_blank">{{ e.firstname }}</a></td>
                <td><a href="{{ path('admin_user_edit', {'user':e.id}) }}" target="_blank">{{ e.lastname }}</a></td>
                <td>{{ e.phone }}</td>
                <td>{{ e.establishment }}</td>
                <td>{{ e.establishmentzipcode }}</td>
                <td>{{ e.establishmentcity }}</td>
                <td class="skm" val="{{ e.transportkm ?? 45 }}">{{ e.transportkm ?? 45 }} km</td>
                <td class="stmp" val="{{ e.transporttime ?? 45 }}">{{ e.transporttime ?? 45 }} min</td>
                <td class="km"></td>
                <td class="tmp"></td>
                {# <td class="contrib" title="{{ nbOpeBooked }} programmées / {{ e.nbContribution }} souhaitées" {% if nbOpeBooked == 0 %}style="background: #d1ecc7; color: gray !important;"{% elseif nbOpeBooked >= e.nbContribution %}style="background: #edc6c6; color: gray !important;"{% endif %}>{{ e.nbContribution }}</td> #}
                <td class="action">
                    <a
                        class="send fancybox fancybox.iframe"
                        href="{{ path('admin_demande_itdlc_professeur_mail', {
                            'demandeItdlc':demande.id,
                            'engineer': e.id,
                            'iframe': true
                        })}}"
                    >
                        <span>
                            <img
                                alt="Envoyer un e-mail personnalisé à lʼingénieur {{ e.fullName }}"
                                src="{{ asset('admin/images/screen/send.png') }}"
                            />
                        </span>
                    </a>
                </td>
                <td class="action"{% if isInterested %} title="Cet Ingénieur a exprimé un intérêt pour cette intervention"{% endif %}>
                    <a href="{{ path('admin_demande_itdlc_generate_ope_inge', {type:'inge', demandeitdlc:demande.id, user:e.id}) }}">
                        {{ 'action.create.label'|trans({}, 'action') }}
                    </a>
                </td>
            </tr>
        {% endfor %}
    </tbody>
</table>
