{% from "deliver_grant_funding/macros/deliver_grant_funding_collection_breadcrumb.html" import deliver_grant_funding_collection_breadcrumb %}
{% from "common/partials/mhclg-test-banner.html" import mhclgInlinePageTestDataBanner %}
{% from "common/macros/status.html" import submissionStatusTag, assessmentStatusTag with context %}
{% from "common/macros/submissions.html" import submission_answers with context %}
{% from "govuk_frontend_jinja/components/notification-banner/macro.html" import govukNotificationBanner %}
{% from "govuk_frontend_jinja/components/button/macro.html" import govukButton %}
{% extends "deliver_grant_funding/grant_base.html" %}

{% set page_title = "Submission - " ~ helper.long_collection_name %}
{% set type_constants = helper.submission.collection.type.constants %}
{% set active_item_identifier = type_constants.active_nav %}

{% block beforeContent %}
  {% if helper.is_test %}
    {{ mhclgInlinePageTestDataBanner("Test submission") }}
  {% endif %}

  {{ deliver_grant_funding_collection_breadcrumb(grant=grant, collection=helper.submission.collection, immediate_parent_breadcrumb_item={"text": "Submissions", "href": url_for("deliver_grant_funding.list_submissions", grant_id=grant.id, collection_type=helper.submission.collection.type, collection_id=helper.submission.collection.id, submission_mode=helper.submission.mode)}, this_page_breadcrumb_item={"text": helper.submission.grant_recipient.organisation.name if helper.submission.grant_recipient else helper.submission.created_by.email }) }}
{% 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_REOPENED]) %}
        {% if flashes %}
          {% set submission_reopened_html %}
            <h3 class="govuk-notification-banner__heading">Submission reopened and email sent to {{ helper.submission.grant_recipient.organisation.name if helper.submission.grant_recipient else helper.submission.created_by.email }}</h3>
            <p class="govuk-body">Email sent to {{ helper.submission.grant_recipient.organisation.name ~ " users" if helper.submission.grant_recipient else helper.submission.created_by.email }} with your reason for reopening.</p>
          {% endset %}
          {{
            govukNotificationBanner({
              "html": submission_reopened_html,
              "type":"success",
              "titleText": "Success"
            })
          }}
        {% endif %}
      {% endwith %}
    </div>
  </div>
  {% if delete_form %}
    {% set banner_html %}
      <p class="govuk-body govuk-!-font-weight-bold">Are you sure you want to reset this test submission?</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, reset this test submission", "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.view_submission', grant_id=grant.id, submission_id=helper.submission.id) }}">Cancel</a>
        </div>
      </form>
    {% endset %}

    {{ govukNotificationBanner(params={"role": "alert", "titleText": "Important", "classes": "", "html": banner_html}) }}
  {% endif %}
  <span class="govuk-caption-l">{{ helper.submission.grant_recipient.organisation.name if helper.submission.grant_recipient else helper.submission.created_by.email }}</span>
  <div class="app-aligned-header-tag govuk-!-margin-bottom-6">
    <h1 class="govuk-heading-l govuk-!-margin-bottom-0">Submission</h1>
    {% if helper.submission.is_assessed %}
      {{ assessmentStatusTag(helper.assessment_status) }}
    {% else %}
      {{ submissionStatusTag(helper.status, helper.submission.is_overdue) }}
    {% endif %}
  </div>
  {% if helper.can_be_reopened_by_user(current_user) %}
    <div class="govuk-body">
      {{
        govukButton(
        params={
          "text": "Reopen submission",
          "href": url_for('deliver_grant_funding.reopen_submission', grant_id=grant.id, submission_id=helper.submission.id),
          "classes": "govuk-button--secondary"
        }
        )
      }}
    </div>
  {% endif %}

  {{ submission_answers(helper, should_show_answers_diff=helper.is_submitted) }}

  <p class="govuk-body">
    <a
      class="govuk-link govuk-link--no-visited-state"
      href="{{ url_for('deliver_grant_funding.list_submissions', grant_id=grant.id, collection_type=helper.submission.collection.type, collection_id=helper.submission.collection.id, submission_mode=helper.submission.mode) }}">
      Back to submissions
    </a>
  </p>

  {% if helper.submission.mode == enum.submission_mode.TEST %}
    <div>
      <h2 class="govuk-heading-m govuk-!-margin-top-9">Manage test submission</h2>

      <ul class="govuk-list govuk-list--spaced">
        <li><a class="govuk-link govuk-link--no-visited-state" href="?delete">Reset this submission</a></li>
      </ul>
    </div>
  {% endif %}
{% endblock content %}
