{% load dashboard static thumbnail %}
{% load combo assets %}

{% if not in_dashboard %}
  <!-- Dashboard settings container -->
  <div class="dashboard-settings">
    <!-- Dashboard meta data container -->
    <div class="dashboard-meta">
      <h2>Délibérations communales</h2>
      {% if json.meta.description %}
        <div>{{json.meta.description}}</div>
      {% endif %}
    </div>

    <!-- Dashboard switch container -->
    <div class="dashboard-switch">
      {% with tile=cell|as_dashboard_cell:request.user %}
        <input type="checkbox" id="deliberations-communales-id"
          data-add-tile-url="{% url 'combo-dashboard-add-tile' cell_reference=cell.get_reference %}"
          {% if tile %}
            checked
            data-remove-tile-url="{% url 'combo-dashboard-remove-tile' cell_reference=tile.cell.get_reference %}"
          {% endif %}
        >
      {% endwith %}

      <!-- JavaScript for handling dashboard switch change -->
      <script>
        $('#deliberations-communales-id').on('change', function() {
          if ($(this).is(':checked')) {
            var $checkbox = $(this);
            $.ajax({
              url: $(this).data('add-tile-url'),
              success: function(data) {
                $checkbox.data('remove-tile-url', data.cell_data.remove_url);
                $('h1#logo').addClass('dashboard-change').on('animationend', function() { $('h1#logo').removeClass('dashboard-change')});
              }
            });
          } else {
            $.ajax({
              url: $(this).data('remove-tile-url'),
              success: function(data) {}
            });
          }
        });
      </script>
    </div>
  </div>
{% else %}
  <!-- Display Délibérations communales title -->
  <h2>Délibérations communales</h2>

  <!-- Iterate over json.data and display entries -->
  {% comment %} The endpoint used to fetch the data is set in the config.json, at the custom JSON cell definition (see "deliberations-communales" : "url" which is probably a HOBO var).{% endcomment %}
  {% for entry in json.data %}
    <div class="delib">
      <img src="{% asset_url "deliberation:placeholder" %}" alt="" height="160" width="160">
      <div>
        <h3><a href="{{ entry.PLONE_id }}">Séance du {{ entry.title }}</a></h3>
        <br>
        <p>
          {% if entry.review_state == 'decision' %}
            Décision
          {% elif entry.review_state == 'in_project' %}
            Projet de décision
          {% endif %}
        </p>
      </div>
    </div>
  {% empty %}
    <!-- Display message if there are no entries -->
    <p>Retrouvez ici les délibérations communales de votre administration.<br /> Il n'y a aucune publication actuellement.</p>
  {% endfor %}
  <!-- Display button for all deliberations if plone_deliberations_url is available -->
  {% if plone_deliberations_url %}
    <p>
      <a class="pk-button" href="{{ plone_deliberations_url }}">Consulter toutes les délibérations</a>
    </p>
  {% endif %}
{% endif %}
