{% extends default_layout is defined ? default_layout : "AyalineCollectiviteBundle::pagelayout.html.twig" %}

{% block content %}
    <div class="introduction">
        <div class="container">
            <h1>{{ ez_content_name(content) }}</h1>
            {{ ez_render_field(content, 'chapo') }}
        </div>
    </div>

    <div class="container">
        <div class="row content-row">

            {% set dateStart = app.request.query.has('date_start') ? app.request.query.get('date_start') : '' %}
            {% set dateEnd = app.request.query.has('date_end') ? app.request.query.get('date_end') : '' %}
            {% set query = app.request.query.has('query') ? app.request.query.get('query') : '' %}
            {% set tags = app.request.query.has('tags') ? app.request.query.get('tags') : restreindreThematiques %}
            {% set communes = app.request.query.has('communes') ? app.request.query.get('communes') : 0 %}
            {% set optionsDate = app.request.query.has('optionsDate') ? app.request.query.get('optionsDate') : '' %}
            {% set optionsDateRetourEnImage = app.request.query.has('optionsDateRetourEnImage') ? app.request.query.get('optionsDateRetourEnImage') : '' %}

            <section class="col-md-8">
                {% if not ez_is_field_empty( content, "description" ) %}
                    <article>
                        {{ ez_render_field(content, "description") }}
                    </article>
                {% endif %}
                {# pour carto #}
                {% set contentRoot = load_content_by_location_id(location.id) %}
                {% set params = [] %}

                {% if not retourEnImage and  permanentEvents|length %}
                    <div class="bloc-events permanent-events">
                        <h2>Événements permanents
                            - {{ permanentEvents|length }} {% if permanentEvents|length > 1 %}événements{% else %}événement{% endif %}</h2>

                        <div class="flexslider permanent-events-slider">
                            <div class="flex-viewport">
                                <ul class="slides">
                                    {% for locationId,permanentEvent in permanentEvents %}
                                        {# pour carto #}
                                        {% set image = "" %}
                                        <li class="fitted">
                                            <div class="img-wrap">
                                                {% set url = path( 'ez_urlalias', {'locationId': locationId, 'uri': app.request.requesturi } ) %}
                                                {% if permanentEvent.getFieldValue( 'vignette' ).destinationContentId is defined and permanentEvent.getFieldValue( 'vignette' ).destinationContentId %}
                                                    {% set imageContent = load_content_by_content_id(permanentEvent.getFieldValue('vignette').destinationContentId) %}
                                                    {% if imageContent is not null %}
                                                        {#pour carto#}
                                                        {% set image = ez_render_field( imageContent, "image", { 'template': 'AyalineEzAgendaCollectivitesBundle:fields:event_ezimage.html.twig', 'parameters': { 'alias': 'line_event', 'linkHref': url } } ) %}
                                                        {{ image |raw }}
                                                    {% endif %}

                                                {% endif %}
                                            </div>
                                            <span>{{ ez_render_field(permanentEvent, 'thematiques') }}</span>

                                            <h3>{{ ez_render_field(permanentEvent, 'titre') }}</h3>

                                            {% if content.fields.mode_escale is defined and not ez_is_field_empty(content, 'mode_escale') %}
                                                <ul class="event-infos">
                                                    {% if permanentEvent.fields.horaire is defined and not ez_is_field_empty(permanentEvent, 'horaire')  %}
                                                        <li class="time-info">
                                                            <span class="icon-time"></span>
                                                            <span class="title-time">{{ ez_render_field(permanentEvent, 'horaire') }}</span>
                                                        </li>
                                                    {% endif %}
                                                    {% if permanentEvent.fields.abonnement is defined and not ez_is_field_empty(permanentEvent, 'abonnement') %}
                                                        <li class="tarif-info">
                                                            <span class="icon-abonnement"></span>
                                                            <span class="title-tarif">Tarif réduit</span>
                                                        </li>
                                                    {% endif %}
                                                </ul>
                                            {% endif %}
                                        </li>
                                            {# pour carto #}
                                            {% set coordonneesEvent = get_coordonnees_event(permanentEvent.id) %}
                                            {% set lieuContent = coordonneesEvent.LieuCoordonnes %}

                                            {% if contentRoot and contentRoot.fields['afficher_carte'] is defined and not ez_is_field_empty(contentRoot, 'afficher_carte') %}
                                                {% if lieuContent and not ez_is_field_empty(lieuContent, 'geolocalisation')  %}
                                                    {% set date = "" %}
                                                    {% if permanentEvent.fields.date_debut is defined and not ez_is_field_empty( permanentEvent, "date_debut" )
                                                    and permanentEvent.fields.date_fin is defined and not ez_is_field_empty( permanentEvent, "date_fin" )
                                                    and permanentEvent.getFieldValue('date_debut').date != permanentEvent.getFieldValue('date_fin').date %}
                                                        {% set date = date ~ "Du " %}
                                                    {% endif %}
                                                    {% if permanentEvent.fields.date_debut is defined and not ez_is_field_empty( permanentEvent, "date_debut" ) %}
                                                        {% set date = date ~ ez_field_value(permanentEvent, "date_debut")|localizeddate('none', 'none', app.request.locale, null, "dd MMMM yyyy") %}
                                                        {% if permanentEvent.fields.date_fin is defined and not ez_is_field_empty( permanentEvent, "date_fin" )  and permanentEvent.getFieldValue('date_debut').date != permanentEvent.getFieldValue('date_fin').date %}
                                                            {% set date = date ~ " au " ~ ez_field_value(permanentEvent, "date_fin")|localizeddate('none', 'none', app.request.locale, null, "dd MMMM yyyy") %}
                                                        {% endif %}
                                                    {% endif %}

                                                    {% set latitude = lieuContent.getFieldValue('geolocalisation').latitude %}
                                                    {% set longitude = lieuContent.getFieldValue('geolocalisation').longitude %}


                                                {% set detailCarte = {
                                                "url": path( 'ez_urlalias', {'locationId': locationId } ),
                                                "image": image,
                                                "titre": ez_render_field(permanentEvent, "titre"),
                                                "info1": ez_render_field(permanentEvent, "thematiques"),
                                                "info2": date,
                                                "info3": {},
                                                "latLngs": {
                                                'latitude': latitude,
                                                'longitude': longitude
                                                }
                                                }
                                                %}
                                                {% set params = params|merge([detailCarte]) %}
                                            {% endif %}
                                        {% endif %}
                                    {% endfor %}
                                </ul>
                            </div>

                            <ul class="flex-direction-nav">
                                <li><a class="flex-prev flex-disabled" href="#" tabindex="-1">prev</a></li>
                                <li><a class="flex-next" href="#" tabindex="-1">next</a></li>
                            </ul>
                        </div>
                    </div>
                {% endif %}
                {% if eventsList|length %}
                    <div class="bloc-events">
                        {% for day,events in eventsList %}
                            <h2>
                                <span>
                                    {{ day|localizeddate('full', 'none', app.request.locale, null)|capitalize }}
                                </span>
                                - {{ events|length }} {% if events|length > 1 %}événements{% else %}événement{% endif %}
                            </h2>
                            <div class="flexslider events-slider">
                                <div class="flex-viewport">
                                    <ul class="slides">
                                        {% for locationId,event in events %}
                                            {% set image = "" %}
                                            <li class="fitted">
                                                {% set url = path( 'ez_urlalias', {'locationId': locationId, 'uri': app.request.requesturi } ) %}
                                                <a href="{{ url }}"></a>
                                                {% if event.getFieldValue( 'vignette' ).destinationContentId is defined and event.getFieldValue( 'vignette' ).destinationContentId %}
                                                    <div class="img-wrap">
                                                        {% set imageContent = load_content_by_content_id(event.getFieldValue('vignette').destinationContentId) %}
                                                        {% if imageContent is not null %}
                                                            {#pour carto#}
                                                            {% set image = ez_render_field( imageContent, "image", {'parameters': { 'alias': 'line_event'} } ) %}
                                                            {{ image |raw }}
                                                        {% endif %}

                                                    </div>
                                                {% endif %}
                                                <span>{{ ez_render_field(event, 'thematiques') }}</span>

                                                <h3>{{ ez_render_field(event, 'titre') }}</h3>

                                                {% if content.fields.mode_escale is defined and not ez_is_field_empty(content, 'mode_escale') %}
                                                    <ul class="event-infos">
                                                        {% if event.fields.horaire is defined and not ez_is_field_empty(event, 'horaire')  %}
                                                            <li class="time-info">
                                                                <span class="icon-time"></span>
                                                                <span class="title-time">{{ ez_render_field(event, 'horaire') }}</span>
                                                            </li>
                                                        {% endif %}
                                                        {% if event.fields.abonnement is defined and not ez_is_field_empty(event, 'abonnement') %}
                                                            <li class="tarif-info">
                                                                <span class="icon-abonnement"></span>
                                                                <span class="title-tarif">Tarif réduit</span>
                                                            </li>
                                                        {% endif %}
                                                    </ul>
                                                {% endif %}
                                            </li>

                                                {# pour carto #}
                                                {% set coordonneesEvent = get_coordonnees_event(event.id) %}
                                                {% set lieuContent = coordonneesEvent.LieuCoordonnes %}
                                                {% if contentRoot and contentRoot.fields['afficher_carte'] is defined and not ez_is_field_empty(contentRoot, 'afficher_carte') %}
                                                    {% if lieuContent and not ez_is_field_empty(lieuContent, 'geolocalisation')  %}
                                                        {% set date = "" %}
                                                        {% if event.fields.date_debut is defined and not ez_is_field_empty( event, "date_debut" )
                                                        and event.fields.date_fin is defined and not ez_is_field_empty( event, "date_fin" )
                                                        and event.getFieldValue('date_debut').date != event.getFieldValue('date_fin').date %}
                                                            {% set date = date ~ "Du " %}
                                                        {% endif %}
                                                        {% if event.fields.date_debut is defined and not ez_is_field_empty( event, "date_debut" ) %}
                                                            {% set date = date ~ ez_field_value(event, "date_debut")|localizeddate('none', 'none', app.request.locale, null, "dd MMMM yyyy") %}
                                                            {% if event.fields.date_fin is defined and not ez_is_field_empty( event, "date_fin" )  and event.getFieldValue('date_debut').date != event.getFieldValue('date_fin').date %}
                                                                {% set date = date ~ " au " ~ ez_field_value(event, "date_fin")|localizeddate('none', 'none', app.request.locale, null, "dd MMMM yyyy") %}
                                                            {% endif %}
                                                        {% endif %}

                                                        {% set latitude = lieuContent.getFieldValue('geolocalisation').latitude %}
                                                        {% set longitude = lieuContent.getFieldValue('geolocalisation').longitude %}

                                                    {% set detailCarte = {
                                                    "url": path( 'ez_urlalias', {'locationId': locationId } ),
                                                    "image": image,
                                                    "titre": ez_render_field(event, "titre"),
                                                    "info1": ez_render_field(event, "thematiques"),
                                                    "info2": date,
                                                    "info3": {},
                                                    "latLngs": {
                                                    'latitude': latitude,
                                                    'longitude': longitude
                                                    }
                                                    }
                                                    %}
                                                    {% set params = params|merge([detailCarte]) %}
                                                {% endif %}
                                            {% endif %}
                                        {% endfor %}
                                    </ul>
                                </div>

                                <ul class="flex-direction-nav">
                                    <li><a class="flex-prev flex-disabled" href="#" tabindex="-1"></a></li>
                                    <li><a class="flex-next" href="#"></a></li>
                                </ul>
                            </div>
                        {% endfor %}
                    </div>
                {% endif %}
                {% if (permanentEvents|length == 0 and eventsList|length == 0) or (retourEnImage and eventsList|length == 0) %}
                    <div class="ezxmltext-field"><p>Aucun événement ne correspond à votre recherche.</p></div>
                {% else %}
                    {# pour carto #}
                    {% if contentRoot and contentRoot.fields['afficher_carte'] is defined and not ez_is_field_empty(contentRoot, 'afficher_carte') and params|length %}
                        {{ render( controller(
                        "AyalineEzCartographieCollectivitesBundle:Cartographie:map",
                        {
                            'params': params,
                            'withPopin': true
                        }
                        )
                        ) }}
                    {% endif %}
                {% endif %}

            </section>

            {% block right_column %}
                <aside class="col-md-4">
                    <div class="bloc-type-2 pink-bg bloc-red actu-form contextualite">
                        <h3>
                            {% if not ez_is_field_empty( content, "titre_bloc_recherche" ) %}
                                {{ ez_render_field(content, 'titre_bloc_recherche') }}
                            {% else %}
                                Rechercher
                            {% endif %}
                        </h3>

                        <h5>Date</h5>

                        <div id="datetimepicker" class="inline-calendar"></div>
                        <form role="form" method="get"
                              action="{{ path( 'ez_urlalias', {'locationId': location.id } ) }}#content">
                            <div class="form-group">
                                <div class="radio-group">
                                    {% set checked = false %}
                                    {% if dateStart == '' and dateEnd == '' %}{% set checked = true %}{% endif %}
                                    {% if retourEnImage %}
                                        <div class="radio">
                                            <input type="radio" name="optionsDateRetourEnImage" id="optionsRadiosRetourEnImage1" value="4"
                                                   {% if optionsDateRetourEnImage == "4" %}checked="checked"{% endif %}>
                                            <label for="optionsRadiosRetourEnImage1"> Le week-end dernier </label>
                                        </div>
                                        <div class="radio">
                                            <input type="radio" name="optionsDateRetourEnImage" id="optionsRadiosRetourEnImage2" value="5"
                                                   {% if optionsDateRetourEnImage == "5" %}checked="checked"{% endif %}>
                                            <label for="optionsRadiosRetourEnImage2"> Il y a 7 jours </label>
                                        </div>
                                        <div class="radio">
                                            <input type="radio" name="optionsDateRetourEnImage" id="optionsRadiosRetourEnImage3" value="6"
                                                   {% if checked or optionsDateRetourEnImage == "6" %}checked="checked"{% endif %}>
                                            <label for="optionsRadiosRetourEnImage3"> Il y a un mois </label>
                                        </div>
                                    {% else %}
                                        <div class="radio">
                                            <input type="radio" name="optionsDate" id="optionsRadios0" value="0"
                                                   {% if checked or optionsDate == "0" %}checked="checked"{% endif %}>
                                            <label for="optionsRadios0"> À venir </label>
                                        </div>
                                        <div class="radio">
                                            <input type="radio" name="optionsDate" id="optionsRadios1" value="1"
                                                   {% if optionsDate == "1" %}checked="checked"{% endif %}>
                                            <label for="optionsRadios1"> Ce week-end </label>
                                        </div>
                                        <div class="radio">
                                            <input type="radio" name="optionsDate" id="optionsRadios2" value="2"
                                                   {% if optionsDate == "2" %}checked="checked"{% endif %}>
                                            <label for="optionsRadios2"> Dans les 7 prochains jours </label>
                                        </div>
                                        <div class="radio">
                                            <input type="radio" name="optionsDate" id="optionsRadios3" value="3"
                                                   {% if optionsDate == "3" %}checked="checked"{% endif %}>
                                            <label for="optionsRadios3"> Ce mois-ci </label>
                                        </div>
                                    {% endif %}

                                </div>
                                <div class="calendar-group">
                                    <div class="input-group date" id="datetimepicker1" data-date-format="DD/MM/YYYY">
                                        <label for="du">A partir du</label>
                                        <input type="text" class="form-control" id="du" name="date_start"
                                               value="{{ dateStart }}">
                                        <span class="input-group-addon">
                                            <span class="picto-calendar"></span>
                                        </span>
                                    </div>

                                    <div class="input-group date" id="datetimepicker2" data-date-format="DD/MM/YYYY">
                                        <label for="au">Jusqu’au</label>
                                        <input type="text" class="form-control" id="au" name="date_end"
                                               value="{{ dateEnd }}">
                                        <span class="input-group-addon">
                                            <span class="picto-calendar"></span>
                                        </span>
                                    </div>
                                </div>
                            </div>

                            {% if not ez_is_field_empty( content, "activer_thematiques" ) %}
                                <div class="form-group">
                                    {% set parentTags = content.getFieldValue("thematique_racine").tags.0 is defined ? content.getFieldValue("thematique_racine").tags.0 : 0 %}
                                    {% set restreindreTags = content.fields.restreindre_thematiques is defined and content.getFieldValue("restreindre_thematiques").tags  ? content.getFieldValue("restreindre_thematiques").tags : [] %}

                                    {{ render( controller( "AyalineEzAgendaCollectivitesBundle:Agenda:tagList", { 'parentTags': parentTags, 'paramTags': (app.request.query.has('tags') ? app.request.query.get('tags') : 0), 'restreindre_thematiques': restreindreTags, 'getSousTag': true } ) ) }}
                                </div>
                            {% endif %}

                            {% if not ez_is_field_empty( content, "activer_gestion_des_communes" ) %}
                                <div class="form-group">
                                    {% set parentTags = content.getFieldValue("commune_racine").tags.0 is defined ? content.getFieldValue("commune_racine").tags.0 : 0 %}
                                    {{ render( controller( "AyalineEzAgendaCollectivitesBundle:Agenda:tagList" , { 'parentTags': parentTags, 'paramTags': communes, 'template': "AyalineEzAgendaCollectivitesBundle::parts/tag_list_commune.html.twig" }) ) }}
                                </div>
                            {% endif %}

                            {% if not ez_is_field_empty( content, "activer_recherche_mots_cles" ) %}
                                <div class="form-group search-group">
                                    <h5><label for="mot-cle">Mots-clés</label></h5>
                                    <input type="text" class="form-control" id="mot-cle" name="query"
                                           value="{{ query }}">
                                </div>
                            {% endif %}

                            <button type="submit" class="btn btn-red">Rechercher</button>

                        </form>
                    </div>

                    {# accèss rapide #}
                    {{ render_esi (controller("AyalineCollectiviteBundle:Home:getBlocHome", {'type': 'acces_rapide_racine', 'template': 'AyalineCollectiviteBundle:parts:quick_access_content.html.twig'})) }}
                </aside>
            {% endblock %}
        </div>
    </div>

{% endblock %}