{% extends "deliver_grant_funding/grant_base.html" %}
{% from "govuk_frontend_jinja/components/back-link/macro.html" import govukBackLink %}
{% from "deliver_grant_funding/macros/select_context_source_header_caption.html" import context_source_heading_caption %}
{% from "deliver_grant_funding/macros/select_context_source_return_url.html" import context_source_return_url %}

{% set page_title = "Select column in " ~ data_set.name ~ " data set - " ~ db_form.title %}
{% set type_constants = db_form.collection.type.constants %}
{% set active_item_identifier = type_constants.active_nav %}

{# FIXME: handle back link generation in the view #}
{% set back_url = url_for("deliver_grant_funding.select_context_source_data_set", grant_id=grant.id, form_id=db_form.id, data_set=data_set.id) %}

{# Set heading caption and cancel button to for what initiated the `add data` flow #}
{% set heading_caption = context_source_heading_caption(add_context_data) %}
{% set cancel_url = context_source_return_url(add_context_data, grant, db_form, enum) %}

{% block beforeContent %}
  {{
    govukBackLink({
        "text": "Back",
        "href": back_url
    })
  }}
{% endblock beforeContent %}

{% block content %}
  <div class="govuk-grid-row">
    <div class="govuk-grid-column-two-thirds">
      {% if form.column.choices | length > 0 %}
        <span class="govuk-caption-l">Question {{ heading_caption }}</span>

        <form method="post" novalidate>
          {{ form.csrf_token }}
          {{ form.column(params={"fieldset": {"legend": {"text": form.column.label.text, "isPageHeading": true, "classes": "govuk-fieldset__legend--l"} } }) }}

          <span class="govuk-button-group">
            {{ form.submit(params={"text": "Select column"}) }}

            <a class="govuk-link govuk-link--no-visited-state" href="{{ cancel_url }}">Cancel</a>
          </span>
        </form>
      {% else %}
        <h1 class="govuk-heading-l">
          <span class="govuk-caption-l">Question {{ heading_caption }}</span>
          {{ form.column.label.text }}
        </h1>
        <p class="govuk-body">You cannot reference this data set because there are no columns with numbers.</p>
        <p class="govuk-body"><a class="govuk-link govuk-link--no-visited-state" href="{{ cancel_url }}">Cancel</a></p>
      {% endif %}
    </div>
  </div>
{% endblock content %}
