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

{% block javascripts %}
    <script>
        //<![CDATA[
        $(function() {
            $('.fancybox-mail').fancybox({
                autoSize: false,
                height: 500,
                width: 700,
                padding: 0,
                openEffect: 'elastic',
                closeEffect: 'elastic',
                beforeClose: function () {
                    {# Get the iframe URL #}
                    var iframeUrl = this.content[0].contentWindow.location.href;

                    {# Find a matching date in the URL (format: xx/xx/xxxx, where x is a number between 0 and 9) #}
                    var dateSending = iframeUrl.match(/datesending=(\d{2}\/\d{2}\/\d{4})$/);

                    {#
                        If there is one match, then update the date in the list of operations
                        Comparing to 2 is necessary because match is used without the /g flag, and then behave like the exec method:
                        "If the regular expression does not include the g flag, returns the same result as RegExp.exec()."
                        @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match#Description
                        "The returned array has the matched text as the first item, and then one item for each capturing parenthesis that matched containing the text that was captured."
                        @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec#Return_value
                    #}
                    if (null !== dateSending && 2 === dateSending.length) {
                        this.element
                            .closest('.operation')
                            .find('.operation-date')
                            .html(dateSending[1]);
                    }
                }
            });
        });
        //]]>
    </script>
{% endblock javascripts %}

{% trans_default_domain 'form' %}

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

    {# Formulaire de recherche #}
    <form action="{{ path('admin_operation_itdlc') }}" class="search-admin" method="get">
        <fieldset>
            <div class="search-fields">
                <p>
                    <label for="academy">{{ 'form.academy.label'|trans }}</label>
                    <select id="form_search_academy" name="academy">
                        <option value="">- Toutes -</option>
                        {% for a in academies %}
                            <option value="{{ a }}"{% if academy == a %} selected="selected"{% endif %}>{{ a }}</option>
                        {% endfor %}
                    </select>
                </p>
            </div>
        </fieldset>
        <p>
            <input class="btn" type="submit" value="{{ 'action.search'|trans({}, 'action') }}" />
        </p>
    </form>

    <table class="tableBo">
        <thead>
            <tr>
                <td>Date de la rencontre</td>
                <td>Ville établissement</td>
                <td>{{ 'form.engineer.label'|transchoice(1) }}</td>
                <td>{{ 'form.teacher.label'|transchoice(1) }}</td>
                <td>{{ 'form.state.label'|trans }}</td>
                <td>{{ 'form.email.label'|trans }}</td>
                <td colspan="3">{{ 'word.actions'|trans({}, 'word') }}</td>
            </tr>
        </thead>
        {% for o in ope %}
            {% set oDate = include('Admin/Ope/ITDLC/_component/availability.html.twig', {availabilities: o.availability}) %}
            <tr class="delete-element-container operation">
                <td>
                    <a href="{{ path('admin_operation_itdlc_edit', {id:o.id }) }}">
                        {{- oDate -}}
                    </a>
                </td>
                <td>
                    <a href="{{ path('admin_operation_itdlc_edit', {id:o.id}) }}">
                        {{- o.prof.establishmentCity -}}
                    </a>
                </td>
                <td>
                    <a href="{{ path('admin_operation_itdlc_edit', {id:o.id}) }}">
                        {%- if o.inge is not null %}{{ o.inge.fullname }}{% endif -%}
                    </a>
                </td>
                <td>
                    <a href="{{ path('admin_operation_itdlc_edit', {id:o.id}) }}">
                        {{- o.prof.fullname -}}
                    </a>
                </td>
                <td>
                    <a href="{{ path('admin_operation_itdlc_edit', {id:o.id}) }}">
                        {{- o.state|transconst('AppBundle\\Enum\\OperationITDLCState') -}}
                    </a>
                </td>
                <td class="operation-date">
                    {{- o.dateMail is not null ? o.dateMail|date('d/m/Y') : '-' -}}
                </td>
                <td class="action">
                    <a
                        class="edit"
                        href="{{ path('admin_operation_itdlc_edit', {id:o.id}) }}"
                        title="Modifier: {{ o.prof.establishmentCity }}, {{ oDate|lower }}"
                    >
                        <span>Modifier</span>
                    </a>
                </td>
                <td class="action">
                    {% if o.inge is not null and o.prof is not null %}
                        <a
                            class="send fancybox-mail fancybox.iframe"
                            href="{{ path('admin_operation_itdlc_confirm', {id:o.id, 'iframe':true}) }}"
                            title="{{ 'action.confirm'|trans({'%name%': o.prof.establishmentCity ~ ', ' ~ oDate|lower}, 'action') }}"
                        >
                            <span><img src="{{ asset('admin/images/screen/send.png') }}" alt="{{ 'action.send.label'|trans({}, 'action') }}"/></span>
                        </a>
                    {% endif %}
                </td>
                <td class="action">
                    <a class="delete-element" href="{{ path('admin_operation_itdlc_delete', {id:o.id}) }}">
                        <span>{{ 'action.delete.label'|trans({}, 'action') }}</span>
                    </a>
                </td>
            </tr>
        {% endfor %}
    </table>

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