{% extends "scrutiny/base.html" %}
{% load tags %}

{% block appbar %}
  <h2>{{ project.title }} — {{ platform.title }}</h2>
{% endblock %}

{% block content %}

  <div id="history-summary">
    {% for day in history %}
      <div data-issues-url-content="{% url 'issues-snippet' %}">
        {% if day.modules %}
          {% if day.day == 'future' %}
            <h3>À venir</h3>
            {% if user.is_authenticated %}
              <a href="{% url 'project-summary-history-future' slug=object.slug %}">Exporter</a>
              -
              <a href="{% url 'project-issues-metadata-stats-future' slug=object.slug %}">Statistiques</a>
            {% endif %}
          {% else %}
            <h3>{{ day.day|date:"d/m/Y" }}</h3>
            {% if user.is_authenticated %}
              <a href="{% url 'project-summary-history-day' slug=object.slug year=day.day.year month=day.day.month day=day.day.day %}">Exporter</a>
              -
              <a href="{% url 'project-issues-metadata-stats-day' slug=object.slug year=day.day.year month=day.day.month day=day.day.day %}">Statistiques</a>
            {% endif %}
          {% endif %}
          <p>
            {% for module in day.modules.values %}
              <span data-module-name="{{ module.name }}"
                    data-previous-version="{{ module.previous_version }}"
                    data-current-version="{{ module.current_version }}"
              >{{ module.name }} ({{module.previous_version}} → {{module.current_version}})
              </span>
              {% if not forloop.last %}, {%endif %}
            {% endfor %}
            <table class="main pk-compact-table issues loading">
              <tbody>
                <tr><td>(récupération des tickets en cours)</td></tr>
              </tbody>
            </table>
        {% endif %}
      </div>
    {% endfor %}

  </div>

{% endblock %}


{% block page-end %}
  <script>
    $(function() {
      $('[data-issues-url-content]').each(function(idx, elem) {
        var table_issues = $(elem).find('table.issues');
        var url = $(elem).data('issues-url-content');
        var modules = Object();
        $(elem).find('[data-module-name]').each(function(idx2, elem2) {
          var mod = Object();
          mod.name = $(elem2).data('module-name');
          mod.previous_version = $(elem2).data('previous-version');
          mod.current_version = $(elem2).data('current-version');
          modules[mod.name] = mod;
        });
        $.ajax({
          url: url,
          type: 'POST',
          contentType: 'application/json',
          data: JSON.stringify(modules),
          dataType: 'html',
          success: function(data) {
            $(table_issues).replaceWith(data);
          }
        });
      });
      $(document).on('gadjo:dialog-done', function(ev, data) {
        var $target = $(ev.target);
        var $modules = $target.parents('tr').find('td').last();
        var $html = $(data.content)
        $html.find('td').last().replaceWith($modules);
        $target.parents('tr').replaceWith($html);
      });
    });
  </script>
{% endblock %}
