{% load static %}
{% load cat_extra %}
{% load utils_extra %}
{% load event_extra %}
{% load tag_extra %}


{% if user.is_authenticated %}

{% if event.other_versions %}
    {% with poss_dup=event.get_other_versions %}
        {% if poss_dup|length > 0 and event.masked %}
        <div class="message warning">
            {% if event.other_versions.representative  %}
                {% if event.pure_import %}Cette version de l'événement est fidèle à la source, mais l'événement existe en{% else %}
                Cette version de l'événement existe en
                {% endif %} 
                     <a href="{{ event.other_versions.get_absolute_url }}">en plusieurs versions</a>.<br>
                {% if event == event.other_versions.get_one_event %}
                Tu consultes la version mise en avant.
                {% else %}
                Tu peux consulter <a href="{{ event.other_versions.get_one_event.get_absolute_url  }}">la version mise en avant</a>.
                {% endif%}
            {% else %}
                Cet événement existe <a href="{{ event.other_versions.get_absolute_url }}">en plusieurs versions</a>, et aucune n'a encore été choisie pour être mise en avant.
            {% endif %}
        </div>
        {% endif %}
    {% endwith %}
{% endif %}
{% else %}
{% if event.other_versions.representative and event.masked %}
<div class="message warning">
    Cette version de l'événement n'est pas dans sa forme la plus avantageuse.
    L'équipe de modération a préparé pour toi une <a href="{{ event.other_versions.representative.get_absolute_url }}">version à jour</a>.
</div>
{% endif %}
{% endif %}

<article>
    <header>
        {% include "agenda_culturel/ephemeris-inc.html" with event=event filter=filter %}
    <div class="details-entete">
        {{ event.category | small_cat_recurrent:event.has_recurrences }}
        {% if h %}<h{{ h }}>{% else %}<h1>{% endif %}{{ event|picto_status }} {% if permalink %}<a href="{{ event.get_absolute_url }}">{% endif %}{{ event.title }}{% if permalink %}</a>{% endif %} {{ event|picto_visibility:user.is_authenticated }}{% if h %}</h{{ h }}>{% else %}</h1>{% endif %}
        <p>
            {% picto_from_name "calendar" %}
            {% if event.end_day and event.end_day != event.start_day %}du{% else %}le{% endif %}
            {{ event.start_day|date|frdate }} 
            {% if event.start_time %} {% if not event.end_day or event.end_day == event.start_day %}{% if event.end_time %}de{% else %}à{% endif %}{% endif %}
                {{ event.start_time }}
            {% endif %}
            {% if event.end_day and event.end_day != event.start_day %}
                au {% if event.end_day and event.end_day != event.start_day %}{{ event.end_day|date|frdate }}{% endif %}
            {% endif %}
            {% if event.end_time %} {% if not event.end_day|date|frdate or event.end_day == event.start_day %}jusqu'à{% endif %} {{ event.end_time }}{% endif %}
        </p>
        
            
            {% if event.exact_location %}
                <p>{% picto_from_name "map-pin" %}
                <a href="{{ event.exact_location.get_absolute_url }}">{{ event.exact_location.name }}, {{ event.exact_location.city }}</a></p>
            {% else %}
                {% if perms.agenda_culturel.change_event and perms.agenda_culturel.change_place %}
                <p>{% picto_from_name "map-pin" %}
                <a href="{% url 'add_place_to_event' event.pk %}" class="missing-data">{% if event.location %}{{ event.location }}{% else %}sans lieu{% endif %}</a></p>
                {% else %}
                {% if event.location %}<p>{% picto_from_name "map-pin" %} {{ event.location }}</p>{% endif %}
                {% endif %}
            {% endif %}
        </p>

        {% with event.get_shown_organisers as organisers %}
        {% if organisers and organisers|length > 0 %}
        <p>{% picto_from_name "users" %} organisé par
                {% for o in organisers %}
                    <a href="{{ o.get_absolute_url }}">{{ o }}</a>{% if not forloop.last %}, {% endif %}
                {% endfor %}
        </p>
        {% endif %}
        {% endwith %}

        {% if user.is_authenticated %}
        {% if event.other_versions %}
            {% with poss_dup=event.get_other_versions %}
                {% if poss_dup|length > 0 and event.other_versions.representative and not event.masked %}
                <p class="remarque">Cette version est celle mise en avant, cependant il en existe <a href="{{ event.other_versions.get_absolute_url }}">en plusieurs versions</a>.</p>
                {% endif %}
            {% endwith %}
        {% endif %}
        {% endif %}

        {% if perms.agenda_culturel.change_message %}
        {% if event.message_set.all.count > 0 %}
        <p class="remarque">Cet événement a été l'objet {% if event.message_set.all.count == 1 %}d'un message{% else %}de messages{% endif %}
            {% for cm in event.message_set.all %}
            <a href="{{ cm.get_absolute_url }}">le {{ cm.date.date }} à {{ cm.date.time }}</a>{% if not forloop.last %}, {% endif %}
            {% endfor %}
            </p>
        {% endif %}
        {% endif %}
    </div>
    </header>
    <div class="event-body">


    {% if event.has_image_url %}
    <article class='illustration'>
        <img src="{{ event.get_image_url }}" alt="{{ event.image_alt }}" />
    </article>
    {% endif %}

    {{ event.description |linebreaks | robust_urlize }}
    </div>
    
    <footer>
        <div class="infos">

        <p>
                {% for tag in event.sorted_tags %}
                <a href="{% url 'view_tag' tag|prepare_tag %}" role="button" class="small-cat">{{ tag|tw_highlight }}</a>
                {% endfor %}
            </p>

            
        {% include "agenda_culturel/event-sources-inc.html" %}

        {% if event.exact_location and event.location %}
            <p class="footer">
                {% picto_from_name "map-pin" %}
                Adresse indiquée par l'organisateur&nbsp;: {{ event.location }}
            </p>
        {% endif %}
        {% if event.has_recurrences %}
        <p class="footer">
        {% picto_from_name "repeat" %}
        <!-- TODO: see https://forge.chapril.org/jmtrivial/agenda_culturel/issues/65 -->
        {% for r in event.recurrences.rrules %}
        {{ r.to_text }}{% if not forloop.first %}, {% endif %}{% endfor %}, depuis le {{ event.recurrences.dtstart.date }}
        {% if event.recurrences.exdates|length > 0 %}, sauf 
            le{{ recurrences.exdates|length|pluralize }}
            {% for e in event.recurrences.exdates %}{% if not forloop.first %}{% if forloop.last %} et {% else %}, {% endif %}{% endif %}
            {{ e.date }}{% endfor %}
        {% endif %}
        </p>
        {% endif %}
        
        {% include "agenda_culturel/event-date-info-inc.html" %}
        </div>
        {% if not nobuttons %}
        <div class="buttons">
            {% if onlyedit %}
                {% if event.pure_import %}
                {% with event.get_local_version as local %}
                    {% if local %}
                    <a href="{{ local.get_absolute_url }}" role="button">voir la version locale {% picto_from_name "eye" %}</a>
                    {% else %}
                    <a href="{% url 'clone_edit' event.id %}" role="button">modifier {% picto_from_name "edit-3" %}</a>
                    {% endif %}
                {% endwith %}
                {% else %}
                <a href="{% url 'edit_event' event.id %}" role="button">modifier {% picto_from_name "edit-3" %}</a>
                {% endif %}
        
            {% else %}
                <a href="{% url 'export_event_ical' event.start_day.year event.start_day.month event.start_day.day event.id %}" role="button">exporter ical {% picto_from_name "calendar" %}</a>
                {% if perms.agenda_culturel.change_event and not noedit %}
                    {% include "agenda_culturel/edit-buttons-inc.html" with event=event details=1 %}
                {% endif %}
            {% endif %}
        </div>
        {% endif %}
        </footer>
</article>

