{% extends "deliver_grant_funding/grant_base.html" %}
{% from "deliver_grant_funding/macros/deliver_grant_funding_collection_breadcrumb.html" import deliver_grant_funding_collection_breadcrumb %}
{% from "govuk_frontend_jinja/components/notification-banner/macro.html" import govukNotificationBanner %}
{% from "deliver_grant_funding/macros/dependency_banner.html" import dependency_banner %}
{% from "deliver_grant_funding/macros/dependency_banner.html" import section_component_dependency_banner %}
{% from "deliver_grant_funding/macros/question_table.html" import question_table %}

{% set type_constants = db_form.collection.type.constants %}
{% set page_title = db_form.title %}
{% set active_item_identifier = type_constants.active_nav %}
{% set can_edit = authorisation_helper.can_edit_collection(current_user, db_form.collection_id) %}

{% set form_has_questions = db_form.cached_questions | length > 0 %}
{% set collection_has_data_sources = db_form.collection.data_sources | length > 0 %}
{% set preview_blocked_by_data_sources = collection_has_data_sources and form_has_questions %}
{% set can_preview = form_has_questions and not collection_has_data_sources and db_form.collection.status != enum.collection_status.CLOSED %}

{% block beforeContent %}
  {{ deliver_grant_funding_collection_breadcrumb(grant=grant, collection=db_form.collection, this_page_breadcrumb_item={"text":db_form.title}) }}
{% 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.SUBMISSION_TESTING_COMPLETE.value]) %}
        {% if flashes %}
          {{ govukNotificationBanner(params={"titleText": "Preview complete", "text": flashes[0]}) }}
        {% endif %}
        {% set errors = None %}
      {% endwith %}
      {% with flashes = get_flashed_messages(category_filter=[enum.flash_message_type.SECTION_COMPONENT_DEPENDENCY_ERROR.value]) %}
        {% if flashes %}
          {{ section_component_dependency_banner(flashes[0], interpolate=interpolate) }}
        {% endif %}
      {% endwith %}
      {% if delete_form %}
        {% set banner_html %}
          <p class="govuk-body govuk-!-font-weight-bold">Are you sure you want to delete this section?</p>
          <form method="post" novalidate>
            {{ delete_form.csrf_token }}
            <div class="govuk-button-group govuk-!-margin-bottom-0">
              {{ delete_form.confirm_deletion(params={"text": "Yes, delete this section", "classes": "govuk-button--warning govuk-!-margin-bottom-0"}) }}
              <a class="govuk-link govuk-link--no-visited-state govuk-!-margin-bottom-0" href="{{ url_for('deliver_grant_funding.list_section_questions', grant_id=grant.id, form_id=db_form.id) }}">Cancel</a>
            </div>
          </form>
        {% endset %}

        {{ govukNotificationBanner(params={"role": "alert", "titleText": "Important", "classes": "", "html": banner_html}) }}
      {% endif %}

      {% with flashes = get_flashed_messages(category_filter=[enum.flash_message_type.DEPENDENCY_ORDER_ERROR.value]) %}
        {% if flashes %}
          {{ dependency_banner(flashes[0], interpolate=interpolate) }}
        {% endif %}
      {% endwith %}

      <h1 class="govuk-heading-l">{{ db_form.title }}</h1>
      {% if preview_blocked_by_data_sources and db_form.collection.status != enum.collection_status.CLOSED %}
        <p class="govuk-body">You cannot preview sections in this {{ type_constants.singular }} because it requires uploaded data.</p>
      {% endif %}
    </div>

    <div class="govuk-grid-column-one-third govuk-!-text-align-right">
      {% if can_preview %}
        <form method="post" novalidate>
          {{ form.csrf_token }}
          {{ form.submit(params={"text": "Preview section", "classes": "govuk-button--secondary" }) }}
        </form>
      {% endif %}
    </div>
  </div>
  <div class="govuk-grid-row">
    <div class="govuk-grid-column-full">
      <h2 class="govuk-heading-m govuk-!-margin-top-5">Questions</h2>
      {% if not db_form.cached_all_components %}
        <p class="govuk-body">This section has no questions.</p>
      {% else %}
        {{ question_table(db_form, interpolate, role_can_edit=can_edit, enum=enum) }}
      {% endif %}

      {% if can_edit %}
        <div class="govuk-button-group">
          <a class="govuk-button govuk-button--secondary" href="{{ url_for('deliver_grant_funding.choose_question_type', grant_id=grant.id, form_id=db_form.id) }}"
            >{{ "Add another question" if db_form.cached_questions else "Add a question" }}</a
          >
          <a class="govuk-button govuk-button--secondary" href="{{ url_for('deliver_grant_funding.add_question_group_name', grant_id=grant.id, form_id=db_form.id) }}">{{ "Add a question group" }}</a>
        </div>
      {% endif %}
    </div>
    {% if can_edit %}
      <div class="govuk-grid-column-full">
        <h2 class="govuk-heading-m govuk-!-margin-top-5">Manage section</h2>

        <ul class="govuk-list govuk-list--spaced">
          <li><a class="govuk-link govuk-link--no-visited-state" href="{{ url_for("deliver_grant_funding.change_form_name", grant_id=grant.id, form_id=db_form.id) }}">Change section name</a></li>
          <li><a class="govuk-link govuk-link--no-visited-state" href="?delete">Delete section</a></li>
        </ul>
      </div>
    {% endif %}
  </div>
{% endblock content %}
