{% extends "access_grant_funding/base.html" %}
{% from "govuk_frontend_jinja/components/back-link/macro.html" import govukBackLink %}
{% from "common/macros/submissions.html" import submission_answers with context %}
{% from "common/macros/status.html" import submissionStatusTag with context %}
{% from "govuk_frontend_jinja/components/inset-text/macro.html" import govukInsetText %}

{% set constants = submission.collection.type.constants %}
{% set page_title = ("Review " ~ constants.singular ~ ": " ~ submission.long_collection_name) if submission.is_awaiting_sign_off else submission.long_collection_name %}

{% set mainClasses = "app-full-screen-with-section-nav" %}

{% set active_item_identifier = constants.active_nav %}
{% block beforeContent %}
  {{ super() }}

  {{
    govukBackLink({
      "href": (
        url_for("access_grant_funding.list_collections", organisation_id=grant_recipient.organisation.id, grant_id=grant_recipient.grant.id)
        if not submission.collection.allow_multiple_submissions else
        url_for("access_grant_funding.list_collection_submissions", organisation_id=grant_recipient.organisation.id, grant_id=grant_recipient.grant.id, collection_id=submission.collection_id)
      ),
      "text": "Back to " ~ constants.plural
    })
  }}
{% endblock beforeContent %}

{% set should_certify = authorisation_helper.is_access_grant_certifier(submission.grant_recipient, current_user) and submission.is_awaiting_sign_off and not submission.in_immutable_state %}
{% block content %}
  <div class="govuk-grid-row">
    <div class="govuk-grid-column-one-quarter-from-desktop app-section-nav-container">
      <nav aria-label="{{ constants.singular | uppercase_first }} sections">
        <ul class="govuk-list app-section-nav-list">
          <li class="app-section-nav-list__item app-section-nav-list__item--active"><a class="govuk-link govuk-link--no-visited-state" href="#submission">{{ page_title }}</a></li>

          {% for form in submission.get_ordered_visible_forms() %}
            <li class="app-section-nav-list__item"><a class="govuk-link govuk-link--no-visited-state" href="#form-{{ form.id }}">{{ form.title }}</a></li>
          {% endfor %}

          {% if should_certify %}
            <li class="app-section-nav-list__item"><a class="govuk-link govuk-link--no-visited-state" href="#sign-off-and-submit-{{ constants.singular }}">Sign off and submit {{ constants.singular }}</a></li>
          {% endif %}
        </ul>
      </nav>

      <a
        class="govuk-button govuk-button--secondary"
        download
        href="{{ url_for('access_grant_funding.export_submission_pdf', organisation_id=grant_recipient.organisation.id, grant_id=grant_recipient.grant.id, collection_type=submission.collection.type, submission_id=submission.submission.id) }}">
        Download {{ constants.singular }} PDF
      </a>
    </div>
    <div class="govuk-grid-column-three-quarters-from-desktop app-section-content-container">
      {# exception for xl heading where we'd anticipate having many subheadings and levels of heirarchy on this page #}
      <h1 id="submission" class="govuk-heading-xl">{{ page_title }}</h1>

      {% if submission.is_submitted %}
        {# if certification wasn't required then show who submitted directly #}
        {% set submitter = submission.sent_for_certification_by or submission.submitted_by %}
        <dl class="app-metadata">
          <dt class="app-metadata__key">Organisation:</dt>
          <dd class="app-metadata__value" data-testid="submission-status">{{ submission.submission.grant_recipient.organisation.name }}</dd>
        </dl>
        <dl class="app-metadata">
          <dt class="app-metadata__key">Submitted by:</dt>
          <dd class="app-metadata__value" data-testid="submission-status">{{ submitter.name }}</dd>
        </dl>
        {% if submission.certified_by %}
          <dl class="app-metadata">
            <dt class="app-metadata__key">Certifier:</dt>
            <dd class="app-metadata__value" data-testid="submission-status">{{ submission.certified_by.name }}</dd>
          </dl>
        {% endif %}
        <dl class="app-metadata govuk-!-margin-bottom-9">
          <dt class="app-metadata__key">Date submitted:</dt>
          <dd class="app-metadata__value" data-testid="submission-status">{{ format_date(submission.submitted_at_utc) }}</dd>
        </dl>
      {% elif submission.is_awaiting_sign_off %}
        <dl class="app-metadata">
          <dt class="app-metadata__key">Submission deadline:</dt>
          <dd class="app-metadata__value" data-testid="submission-status">{{ format_date(submission.collection.submission_period_end_date) if submission.collection.submission_period_end_date else "Dates to be confirmed" }}</dd>
        </dl>
        <dl class="app-metadata">
          <dt class="app-metadata__key">Submitted by:</dt>
          <dd class="app-metadata__value" data-testid="submission-status">{{ submission.sent_for_certification_by.name }}</dd>
        </dl>
        <dl class="app-metadata">
          <dt class="app-metadata__key">Date submitted for sign off:</dt>
          <dd class="app-metadata__value" data-testid="submission-status">{{ format_date(submission.sent_for_certification_at_utc) }}</dd>
        </dl>
        <dl class="app-metadata govuk-!-margin-bottom-9">
          <dt class="app-metadata__key">Status:</dt>
          <dd class="app-metadata__value" data-testid="submission-status">{{ submissionStatusTag(submission.status, submission.submission.is_overdue) }}</dd>
        </dl>
      {% else %}
        {# we should only show this page for a non-submitted collection if it is locked #}
        {# and we're showing the users incomplete answers #}
        {{ govukInsetText({"text": "You cannot edit or submit this " ~ constants.singular ~ "."} ) }}

        <dl class="app-metadata">
          <dt class="app-metadata__key">Submission deadline:</dt>
          <dd class="app-metadata__value" data-testid="submission-status">{{ format_date(submission.collection.submission_period_end_date) if submission.collection.submission_period_end_date else "Dates to be confirmed" }}</dd>
        </dl>
        <dl class="app-metadata">
          <dt class="app-metadata__key">Status:</dt>
          <dd class="app-metadata__value" data-testid="submission-status">{{ submissionStatusTag(submission.status, submission.submission.is_overdue) }}</dd>
        </dl>
      {% endif %}

      {{ submission_answers(submission, should_show_answers_diff=submission.in_answers_locked_state) }}

      {% if should_certify %}
        <h2 id="sign-off-and-submit-{{ constants.singular }}" class="govuk-heading-m govuk-!-margin-top-9">Sign off and submit {{ constants.singular }}</h2>
        <p class="govuk-body">
          Sign off and submit the {{ constants.singular }} to the {{ submission.collection.grant.organisation.name }} by
          {{ format_date_short(submission.collection.submission_period_end_date) if submission.collection.submission_period_end_date else "(Dates to be confirmed)" }}.
        </p>

        <p class="govuk-body">If you need to decline the {{ constants.singular }}, provide a reason so {{ submission.sent_for_certification_by.name }} can update and resubmit the {{ constants.singular }}.</p>

        <form method="POST" novalidate>
          <div class="govuk-button-group">
            {{ form.csrf_token }}
            {{ form.submit(params={"text": "Continue to sign off and submit" }) }}
            <a
              class="govuk-button--secondary govuk-button"
              href="{{ url_for("access_grant_funding.decline_submission", organisation_id=grant_recipient.organisation_id, grant_id=grant_recipient.grant_id, collection_type=submission.collection.type, submission_id=submission.id) }}">
              Decline sign off
            </a>
          </div>
        </form>
      {% endif %}
    </div>
  </div>
{% endblock %}
