{% extends "deliver_grant_funding/grant_base.html" %}
{% from "govuk_frontend_jinja/components/button/macro.html" import govukButton %}
{% from "govuk_frontend_jinja/components/notification-banner/macro.html" import govukNotificationBanner %}
{% from "common/macros/status.html" import grantStatusTag with context %}
{% from "common/macros/status.html" import grantStatusDescription with context %}
{% from "deliver_grant_funding/macros/collection_card.html" import collection_card with context %}

{% set page_title = "Forms" %}
{% set active_item_identifier = "pre_award" %}
{% set grant_admin = authorisation_helper.is_deliver_grant_admin(grant.id, current_user) %}

{% block content %}
  <div class="govuk-grid-row">
    <div class="govuk-grid-column-two-thirds">
      {# TODO: FSPT-549 - show flash messages consistently everywhere with a standard interface #}
      {% with flashes = get_flashed_messages(with_categories=true) %}
        {% for category, message in flashes %}
          {% if category == enum.flash_message_type.COLLECTION_CREATED %}
            {% set banner_html %}
              <h3 class="govuk-notification-banner__heading">{{ (message.name ~ " " ~ message.singular ~ " created") | uppercase_first }}</h3>
              <p class="govuk-body">View your <a class="govuk-notification-banner__link" href="{{ message.url }}">{{ message.name }} {{ message.singular }}</a>.</p>
            {% endset %}
            {{ govukNotificationBanner({ "html": banner_html, "type": "success" }) }}
          {% else %}
            {{ govukNotificationBanner({ "text": message, "type": category }) }}
          {% endif %}
        {% endfor %}
      {% endwith %}

      {% if delete_form %}
        {% set banner_html %}
          <p class="govuk-body govuk-!-font-weight-bold">Are you sure you want to delete the form ‘{{ delete_pre_award_form.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 form", "classes": "govuk-button--secondary govuk-!-margin-bottom-0"}) }}
              <a class="govuk-link govuk-link--no-visited-state govuk-!-margin-bottom-0" href="{{ url_for('deliver_grant_funding.list_pre_award_forms', grant_id=grant.id) }}">Cancel</a>
            </div>
          </form>
        {% endset %}

        {{ govukNotificationBanner(params={"role": "alert", "titleText": "Important", "html": banner_html}) }}
      {% endif %}
    </div>
  </div>
  <div class="govuk-grid-row">
    <div class="govuk-grid-column-two-thirds">
      <h1 class="govuk-heading-l">
        <span class="govuk-caption-l">{{ grant.name }}</span>
        Forms
      </h1>

      <p class="govuk-body"><strong>Grant status</strong>: {{ grantStatusTag(grant.status) }}</p>
      <p class="govuk-body">{{ grantStatusDescription(grant, enum.collection_type_enum.APPLICATION) }}</p>
    </div>
  </div>
  <div class="govuk-grid-row">
    <div class="govuk-grid-column-full">
      {% if not grant.pre_award_forms %}
        <p class="govuk-body">There are no pre-award forms for {{ grant.name }}.</p>
      {% else %}
        {% for form in grant.pre_award_forms %}
          {{ collection_card(form) }}
        {% endfor %}
      {% endif %}

      {% if grant_admin %}
        {{
          govukButton({
            "text": "Create a form" if not grant.pre_award_forms else "Create another form",
            "classes": "" if not grant.pre_award_forms else "govuk-button--secondary",
            "href": url_for('deliver_grant_funding.choose_collection_creation_method', grant_id=grant.id, collection_type=enum.collection_type_enum.APPLICATION)
          })
        }}
      {% endif %}
    </div>
  </div>
{% endblock content %}
