{% extends "deliver_grant_funding/grant_base.html" %}
{% from "govuk_frontend_jinja/components/back-link/macro.html" import govukBackLink %}
{% from "govuk_frontend_jinja/components/table/macro.html" import govukTable %}

{% set type_constants = collection.type.constants %}
{% set active_item_identifier = type_constants.active_nav %}

{% set page_title = "Format " ~ session_data.name ~ " data" %}

{% block beforeContent %}
  {% if session_data.has_missing_data %}
    {% set back_link = url_for("deliver_grant_funding.data_set_missing_data", grant_id=grant.id, collection_type=collection.type, collection_id=collection.id, data_source_id=session_data.data_source_id if session_data.is_replace == True else None) %}
  {% elif session_data.has_grant_recipient_mismatches %}
    {% set back_link = url_for("deliver_grant_funding.confirm_data_set_grant_recipients", grant_id=grant.id, collection_type=collection.type, collection_id=collection.id, data_source_id=session_data.data_source_id if session_data.is_replace == True else None) %}
  {% else %}
    {% if session_data.is_replace %}
      {% set back_link = url_for("deliver_grant_funding.replace_data_set", grant_id=grant.id, collection_type=collection.type, collection_id=collection.id, data_source_id=session_data.data_source_id) %}
    {% else %}
      {% set back_link = url_for("deliver_grant_funding.upload_data_set", grant_id=grant.id, collection_type=collection.type, collection_id=collection.id) %}
    {% endif %}
  {% endif %}
  {{ govukBackLink({ "text": "Back", "href": back_link }) }}
{% endblock beforeContent %}

{% block content %}
  <div class="govuk-grid-row">
    <div class="govuk-grid-column-two-thirds">
      <h1 class="govuk-heading-l">
        <span class="govuk-caption-l">{{ collection.name }}</span>
        Format {{ session_data.name }} data
      </h1>
      <p class="govuk-body">Select the data type for each column in the data set.</p>
    </div>
    <div class="govuk-grid-column-full">
      <form method="POST" novalidate>
        {{ form.csrf_token }}

        {% set rows = [] %}
        {% for column in form.data_columns %}
          {% set idx = loop.index0 %}
          {% set preview_html = session_data.preview_data[column] | join('<br>') %}
          {% set select_html %}
            {{ form.columns[idx].form.column_type(params={"label": {"html": "Select data type <span class='govuk-visually-hidden'>for " ~ column ~ "</span>" } }) }}
          {% endset %}
          {%
            do rows.append([
              {"text": column},
              {"html": preview_html},
              {"html": select_html}
            ])
          %}
        {% endfor %}

        {{
          govukTable({
            "head": [
              {"text": "Column name", "classes": "govuk-!-width-one-quarter"},
              {"text": "Example data", "classes": "govuk-!-width-one-half"},
              {"text": "Formatting options"}
            ],
            "rows": rows
          })
        }}

        <div class="govuk-button-group">
          {{ form.submit }}
          <a class="govuk-link govuk-link--no-visited-state" href="{{ url_for('deliver_grant_funding.list_collection_data_sets', grant_id=grant.id, collection_type=collection.type, collection_id=collection.id) }}">Cancel</a>
        </div>
      </form>
    </div>
  </div>
{% endblock content %}
