{% 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 types = app.request.query.has('types') ? app.request.query.get('types') : 0 %}
            {% 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">
                <div class="bloc-events">
                    {% 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-event permanent-events">
                            <h2>
                                {% if content.fields.intitule_evenement_permanent is defined and not ez_is_field_empty(content, 'intitule_evenement_permanent') %}
                                    {{ ez_render_field(content, 'intitule_evenement_permanent') }}
                                {% else %}
                                    Événements permanents
                                {% endif %}
                                - {{ 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">
                                                {% set url = path( 'ez_urlalias', {'locationId': locationId, 'uri': app.request.requesturi } ) %}

                                                <div class="img-wrap">
                                                    {% if permanentEvent.getFieldValue( 'vignette' ).destinationContentId is defined and permanentEvent.getFieldValue( 'vignette' ).destinationContentId %}
                                                        {{ ez_render_field( permanentEvent, "vignette", { 'template': 'AyalineEzAgendaCollectivitesBundle:fields:event_ezobjectrelation.html.twig', 'parameters': { 'alias': 'line_event', 'linkHref': url } } ) }}
                                                        {# pour carto #}
                                                        {% set image = ez_render_field( permanentEvent, "vignette", { 'template': 'AyalineEzAgendaCollectivitesBundle:fields:event_ezobjectrelation.html.twig', 'parameters': { 'alias': 'line_event', 'linkHref': url } } ) %}
                                                    {% endif %}
                                                </div>
                                                <span>{{ ez_render_field(permanentEvent, 'thematiques') }}</span>
                                                <a href="{{ url }}" title="{{ ez_render_field(permanentEvent, 'titre') }}">
                                                    <h3>{{ ez_render_field(permanentEvent, 'titre') }}</h3>
                                                </a>

                                                <div class="time-wrapper">

                                                    <time datetime="{{ ez_field_value(permanentEvent, "date_debut")|localizeddate('none', 'none', app.request.locale, null, "dd/MM/yyyy") }}">
                                                        {% 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 %}
                                                            Du
                                                        {% endif %}
                                                        {% if permanentEvent.fields.date_debut is defined and not ez_is_field_empty( permanentEvent, "date_debut" ) %}
                                                        {% 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 dateDebut = ez_field_value(permanentEvent, "date_debut")|localizeddate('none', 'none', app.request.locale, null, "dd LLLL yyyy") %}
                                                        {% else %}
                                                            {% set dateDebut = ez_field_value(permanentEvent, "date_debut")|localizeddate('none', 'none', app.request.locale, null, "dd LLLL yyyy")|capitalize %}
                                                        {% endif %}
                                                        {{ dateDebut }}</time>
                                                    {% 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%}
                                                        <time datetime="{{ ez_field_value(permanentEvent, "date_fin")|localizeddate('none', 'none', app.request.locale, null, "dd/MM/yyyy") }}">
                                                            au {{ ez_field_value(permanentEvent, "date_fin")|localizeddate('none', 'none', app.request.locale, null, "dd LLLL yyyy") }}
                                                        </time>
                                                    {% endif %}
                                                    {% endif %}

                                                </div>

                                                {% block horaire_abonnement_permanent %}{% endblock %}
                                            </li>
                                            {# pour carto #}
                                            {% if contentRoot and contentRoot.fields['afficher_carte'] is defined and not ez_is_field_empty(contentRoot, 'afficher_carte') %}
                                                {% if not ez_is_field_empty(permanentEvent, 'geolocalisation') or not ez_is_field_empty(permanentEvent, 'localiser_lieu')  %}
                                                    {% 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 %}

                                                    {% if not ez_is_field_empty(permanentEvent, 'geolocalisation') %}
                                                        {% set latitude = permanentEvent.getFieldValue('geolocalisation').latitude %}
                                                        {% set longitude = permanentEvent.getFieldValue('geolocalisation').longitude %}
                                                    {% elseif permanentEvent.getFieldValue('localiser_lieu').destinationContentId %}
                                                        {% set lieu = load_content_by_content_id(permanentEvent.getFieldValue('localiser_lieu').destinationContentId) %}
                                                        {% set latitude = lieu.getFieldValue('geolocalisation').latitude %}
                                                        {% set longitude = lieu.getFieldValue('geolocalisation').longitude %}
                                                    {% endif %}

                                                    {% 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 %}
                            {% for day,events in eventsList %}
                                <div class="bloc-event">
                                    <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) %}
                                                            {{ ez_render_field( imageContent, "image", {'parameters': { 'alias': 'line_event'} } ) }}
                                                            {% set image = ez_render_field( imageContent, "image", {'parameters': { 'alias': 'line_event'} } ) %}
                                                        </div>
                                                    {% endif %}
                                                    <span>{{ ez_render_field(event, 'thematiques') }}</span>

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

                                                    {% block horaire_abonnement %}{% endblock %}
                                                </li>

                                                {# pour carto #}
                                                {% if contentRoot and contentRoot.fields['afficher_carte'] is defined and not ez_is_field_empty(contentRoot, 'afficher_carte') %}
                                                    {% if not ez_is_field_empty(event, 'geolocalisation') or not ez_is_field_empty(event, 'localiser_lieu')  %}
                                                        {% 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 %}

                                                        {% if not ez_is_field_empty(event, 'geolocalisation') %}
                                                            {% set latitude = event.getFieldValue('geolocalisation').latitude %}
                                                            {% set longitude = event.getFieldValue('geolocalisation').longitude %}
                                                        {% elseif event.getFieldValue('localiser_lieu').destinationContentId %}
                                                            {% set lieu = load_content_by_content_id(event.getFieldValue('localiser_lieu').destinationContentId) %}
                                                            {% set latitude = lieu.getFieldValue('geolocalisation').latitude %}
                                                            {% set longitude = lieu.getFieldValue('geolocalisation').longitude %}
                                                        {% endif %}

                                                        {% 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>
                                </div>
                        {% endfor %}
                    {% 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 %}
                </div>
            </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>
                        {% if content.fields.activer_calendrier is defined and not ez_is_field_empty( content, 'activer_calendrier' ) %}
                            <div id="datetimepicker" class="inline-calendar"></div>
                        {% endif %}

                        <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_type" ) %}
                                <div class="form-group">
                                    {% set parentTags = content.getFieldValue("type_racine").tags.0 is defined ? content.getFieldValue("type_racine").tags.0 : 0 %}
                                    {{ render( controller( "AyalineEzAgendaCollectivitesBundle:Agenda:tagList" , { 'parentTags': parentTags, 'paramTags': types, 'template': "AyalineEzAgendaCollectivitesBundle::parts/tag_list_type.html.twig" }) ) }}
                                </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 %}