{% load assets wcs %}

{% if not json_error %}
{% with parametres=cards|objects:"pn-v2-parametres-generaux"|get_full|first|get:"fields" %}
{% with cards|objects:"pn_preferencesusager"|filter_by_user:request.user|get_full|first as preference %}

<div class="group-title notifications--group-title" hidden {% if not preference %}data-user-with-no-pref{% endif %}><div><h2>{{ title|default:"Notifications" }}</h2></div></div>

{% for fiche in json.data %}

  {# Global notifications #}
  {% if fiche.fields.priseencompte == "Non" %}
  {% include "combo/publik-notifications/pn-single-notification.html" with refimgc="refimgdefaut" %}
  {% endif %}

  {# Custom notifications #}

  {% if preference.user.email == request.user.email %}  {# XXX: pourquoi ça ? #}
    {% with thememessage=fiche.fields.thematique preferencetheme=preference.fields.thematiques %}

    {# Metro notifications #}
    {% if thememessage in preferencetheme and fiche.fields.zone_diffusion_raw == "metro" and preference.fields.metro == True %}
    {% include "combo/publik-notifications/pn-single-notification.html" with refimgc="pn:thematique:"|add:fiche.fields.thematique_raw %}
    {% endif %}

    {% with villesmessage=fiche.fields.villes_raw preferenceville=preference.fields.villes_raw %}
      {% if thememessage in preferencetheme %}
        {% for ville in villesmessage %}
          {% if ville in preferenceville %}
            {% ifchanged %}
              {% include "combo/publik-notifications/pn-single-notification.html" with refimgc="pn:thematique:"|add:fiche.fields.thematique_raw %}
            {% endifchanged %}
          {% endif %}
        {% endfor %}
      {% endif %}
    {% endwith %}

  {% endwith %}{#  Fin with thememessage and preferencethemel #}
  {% endif %} {# Fin vérif email usager #}

{% endfor %} {# Fin de la boucle sur les fiches #}

<div hidden class="tile notifications--no-notification">
<p>{{ parametres|get:"message_defaut_si_vide" }}</p>
</div>

{% if parametres|get:"affiche_lien_inscription" == True %}
<p hidden class="notifications--customize-button"><a class="pk-button" href="{{ eservices_url }}{{ parametres|get:"uri_inscription" }}">Gérer mes notifications personnalisées</a></p>
{% endif %}

<script>
$(function() {
  {% if preference %}
  $('.notifications--group-title').attr('hidden', null);
  $('.notifications--customize-button').attr('hidden', null);
  {% endif %}
  if ($('.tile-notification').length == 0) {
    {% if preference %}
    $('.notifications--no-notification').attr('hidden', null);
    {% endif %}
  } else {
    $('.notifications--group-title').attr('hidden', null);
  }
});
</script>

{% endwith %} {# Fin with fiche préférence usager #}
{% endwith %}
{% endif %}
