{% extends "deliver_grant_funding/grant_base.html" %}
{% from "govuk_frontend_jinja/components/table/macro.html" import govukTable %}
{% from "govuk_frontend_jinja/components/notification-banner/macro.html" import govukNotificationBanner %}
{% from "deliver_grant_funding/macros/deliver_grant_funding_collection_breadcrumb.html" import deliver_grant_funding_collection_breadcrumb %}
{% from 'govuk_frontend_jinja/components/details/macro.html' import govukDetails %}
{% from "govuk_frontend_jinja/components/tag/macro.html" import govukTag %}

{% set type_constants = collection.type.constants %}
{% set page_title = "Data sets for " + collection.name %}
{% set active_item_identifier = type_constants.active_nav %}
{% set can_upload = authorisation_helper.can_edit_collection(current_user, collection.id) %}

{% block beforeContent %}
  {{ deliver_grant_funding_collection_breadcrumb(grant=grant, collection=collection, this_page_breadcrumb_item={"text": "Data sets"}) }}
{% endblock beforeContent %}

{% block content %}
  <div class="govuk-grid-row">
    <div class="govuk-grid-column-two-thirds">
      {% with flashes = get_flashed_messages(category_filter=[enum.flash_message_type.DATA_SOURCE_UPLOADED_SUCCESS,enum.flash_message_type.DATA_SOURCE_DELETED]) %}
        {% if flashes %}
          {% set message %}
            <h3 class="govuk-notification-banner__heading">Data set {{ "deleted" if "deleted" in flashes[0] else "uploaded" }}</h3>
            <p class="govuk-body">{{ flashes[0] }}</p>
          {% endset %}
          {{
            govukNotificationBanner({
              "html": message, "type": "success"
            })
          }}
        {% endif %}
      {% endwith %}

      <h1 class="govuk-heading-l">
        <span class="govuk-caption-l">{{ collection.name }}</span>
        Uploaded data sets
      </h1>
      <p class="govuk-body">Upload data to reference in guidance, questions, conditions and validation.</p>
      <p class="govuk-body">You can replace uploaded data sets to update missing or incorrect data.</p>

      {% set dataSetUploadInstructionsHtml %}
        <ol class="govuk-list govuk-list--number govuk-!-margin-bottom-0">
          <li>
            Check the <a class="govuk-link govuk-link--no-visited-state" href="{{ url_for('deliver_grant_funding.grant_details', grant_id=grant.id) }}">grant recipients</a> are set up to make sure they will be in the data set template.
          </li>
          <li>
            <a class="govuk-link govuk-link--no-visited-state" href="{{ url_for('deliver_grant_funding.download_grant_recipient_data_set_template', grant_id=grant.id, collection_type=collection.type, collection_id=collection.id) }}"
              >Download the data set template (CSV)</a
            >.
          </li>
          <li>Name each column to describe the data in it, for example, “Funding”.</li>
          <li>Upload the new data set by naming and formatting the data set.</li>
        </ol>
      {% endset %}
      {{
        govukDetails({
          "summaryText": "Uploading a data set",
          "html": dataSetUploadInstructionsHtml,
        })
      }}
    </div>
    <div class="govuk-grid-column-full">
      {% set rows = [] %}
      {% if data_sources %}

        {% for data_set in data_sources %}
          {% set data_set_name_html %}
            <a class="govuk-link govuk-link--no-visited-state" href="{{ url_for('deliver_grant_funding.view_data_source', grant_id=grant.id, collection_type=collection.type, collection_id=collection.id, data_source_id=data_set.id) }}"
              >{{ data_set.name }}</a
            >
            {% if data_set.has_missing_data %}
              {{ govukTag({"text": "Data missing", "classes": "govuk-tag--orange govuk-!-margin-left-1"}) }}
            {% endif %}
          {% endset %}
          {% set data_set_timestamp %}
            <time datetime="{{ iso_utc(data_set.updated_at_utc) }}">{{ format_datetime_short(data_set.updated_at_utc) }}</time>
          {% endset %}
          {% do rows.append([{"html": data_set_name_html}, {"html": data_set_timestamp}, {"text": data_set.uploaded_by_name}]) %}
        {% endfor %}
      {% else %}
        {% set no_data_sets_text %}
          <span>There are no data sets uploaded for this {{ type_constants.singular }}</span>
        {% endset %}
        {%
          do rows.append([
            {
              "html": no_data_sets_text,
              "colspan": 3
            }
          ])
        %}
      {% endif %}

      {{
        govukTable({
            "head": [{"text": "Data set"}, {"text": "Time and date"}, {"text": "Uploaded by"}],
            "rows": rows
        })
      }}

      {% if can_upload %}
        <form method="POST" novalidate>
          <div class="govuk-button-group">
            {{ form.csrf_token }}
            {{ form.submit(params={"text": "Upload new data set" }) }}
          </div>
        </form>
      {% endif %}
    </div>
  </div>
{% endblock content %}
