{% extends "deliver_grant_funding/grant_base.html" %}
{% from "govuk_frontend_jinja/components/summary-list/macro.html" import govukSummaryList %}
{% from "govuk_frontend_jinja/components/notification-banner/macro.html" import govukNotificationBanner %}
{% from "govuk_frontend_jinja/components/back-link/macro.html" import govukBackLink %}

{% set type_constants = collection.type.constants %}
{% set page_title = type_constants.singular | uppercase_first ~ " settings - " ~ collection.name %}
{% set active_item_identifier = type_constants.active_nav %}
{% set can_edit = authorisation_helper.can_edit_collection(current_user, collection.id) %}

{% block beforeContent %}
  {{
    govukBackLink({
      "text": "Back",
      "href": url_for(type_constants.list_endpoint, grant_id=grant.id),
    })
  }}
{% endblock beforeContent %}

{% block content %}
  <div class="govuk-grid-row">
    <div class="govuk-grid-column-full">
      {% if delete_form %}
        {% set banner_html %}
          <p class="govuk-body govuk-!-font-weight-bold">Are you sure you want to delete "{{ collection.name }}"?</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 " ~ type_constants.singular, "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.collection_settings', grant_id=grant.id, collection_type=collection.type, collection_id=collection.id) }}"
                >Cancel</a
              >
            </div>
          </form>
        {% endset %}
        {{ govukNotificationBanner(params={"role": "alert", "titleText": "Important", "html": banner_html}) }}
      {% endif %}

      <span class="govuk-caption-l">{{ collection.name }}</span>
      <h1 class="govuk-heading-l">{{ type_constants.singular | uppercase_first }} settings</h1>

      {% set rows = [] %}
      {%
        do rows.append({
          "key": {"text": type_constants.singular | uppercase_first ~ " name"},
          "value": {"text": collection.name},
          "actions": {
            "items": [{
              "href": url_for("deliver_grant_funding.change_collection_name", grant_id=grant.id, collection_type=collection.type, collection_id=collection.id),
              "classes": "govuk-link--no-visited-state",
              "text": "Change",
              "visuallyHiddenText": type_constants.singular ~ " name"
            }]
          } if can_edit else {}
        })
      %}
      {%
        do rows.append({
          "key": {"text": "Requires certification"},
          "value": {"text": "Yes" if collection.requires_certification else "No"},
          "actions": {
            "items": [{
              "href": url_for("deliver_grant_funding.collection_configure_certification", grant_id=grant.id, collection_type=collection.type, collection_id=collection.id),
              "classes": "govuk-link--no-visited-state",
              "text": "Change",
              "visuallyHiddenText": "certification setting"
            }]
          } if can_edit else {}
        })
      %}
      {%
        do rows.append({
          "key": {"text": "Allow reopening submissions"},
          "value": {"text": "Yes" if collection.allow_submission_reopening else "No"},
          "actions": {
            "items": [{
              "href": url_for("deliver_grant_funding.collection_configure_reopening", grant_id=grant.id, collection_type=collection.type, collection_id=collection.id),
              "classes": "govuk-link--no-visited-state",
              "text": "Change",
              "visuallyHiddenText": "reopening submissions setting"
            }]
          } if can_edit else {}
        })
      %}
      {%
        do rows.append({
          "key": {"text": "Allow multiple submissions"},
          "value": {"text": "Yes" if collection.allow_multiple_submissions else "No"},
          "actions": {
            "items": [{
              "href": url_for("deliver_grant_funding.collection_multiple_submissions", grant_id=grant.id, collection_type=collection.type, collection_id=collection.id),
              "classes": "govuk-link--no-visited-state",
              "text": "Change",
              "visuallyHiddenText": "multiple submissions setting"
            }]
          } if can_edit else {}
        })
      %}

      {% if collection.allow_multiple_submissions %}
        {%
          do rows.append({
            "key": {"text": "How submissions are named"},
            "value": {"text": "Grant policy team will provide a list of names" if collection.multiple_submissions_are_managed_by_service else "Users will create and name each submission"},
            "actions": {
              "items": [{
                "href": url_for("deliver_grant_funding.collection_multiple_submissions_naming", grant_id=grant.id, collection_type=collection.type, collection_id=collection.id),
                "classes": "govuk-link--no-visited-state",
                "text": "Change",
                "visuallyHiddenText": "multiple submissions naming setting"
              }]
            } if can_edit else {}
          })
        %}
        {% set submission_name_value_html %}
          {{ collection.submission_name_question.text if collection.submission_name_question else "Not set" }}
          {% if collection.multiple_submissions_are_managed_by_service %}
            <p class="govuk-body govuk-!-margin-top-2 govuk-hint">Contact the Platform team to set up submission names</p>
          {% endif %}
        {% endset %}
        {%
          do rows.append({
            "key": {"text": "Question for submission name"},
            "value": {"html": submission_name_value_html},
            "actions": {
              "items": [{
                "href": url_for("deliver_grant_funding.collection_multiple_submissions_settings", grant_id=grant.id, collection_type=collection.type, collection_id=collection.id),
                "classes": "govuk-link--no-visited-state",
                "text": "Change",
                "visuallyHiddenText": "multiple submissions name"
              }]
            } if can_edit else {}
          })
        %}
        {% if not collection.multiple_submissions_are_managed_by_service %}
          {%
            do rows.append({
              "key": {"text": "Guidance for multiple submissions (optional)"},
              "value": {"html": collection.submission_guidance | replace("\n", "<br>" | safe) if collection.submission_guidance else "Not set"},
              "actions": {
                "items": [{
                  "href": url_for("deliver_grant_funding.collection_multiple_submissions_settings", grant_id=grant.id, collection_type=collection.type, collection_id=collection.id),
                  "classes": "govuk-link--no-visited-state",
                  "text": "Change",
                  "visuallyHiddenText": "multiple submissions guidance"
                }]
              } if can_edit else {}
            })
          %}
        {% endif %}
      {% endif %}

      {{ govukSummaryList({"rows": rows}) }}

      {% if can_edit and not collection.live_submissions %}
        <p class="govuk-body govuk-!-margin-top-6">
          <a class="govuk-link app-link--destructive" href="{{ url_for('deliver_grant_funding.collection_settings', grant_id=grant.id, collection_type=collection.type, collection_id=collection.id, delete='') }}">
            Delete {{ type_constants.singular }}
          </a>
        </p>
      {% endif %}
    </div>
  </div>
{% endblock content %}
