{% from "common/macros/status.html" import submissionStatusTag with context %}
{% from "common/macros/collections.html" import collection_tasklist with context %}
{% from "govuk_frontend_jinja/components/back-link/macro.html" import govukBackLink %}
{% from "govuk_frontend_jinja/components/error-summary/macro.html" import govukErrorSummary %}
{% extends "access_grant_funding/base.html" %}

{# todo: move the suffix to the base template and call super #}
{% set page_title = runner.submission.long_collection_name ~ " - " ~ grant_recipient.grant.name %}

{% set form = runner.tasklist_form %}
{% set constants = runner.submission.collection.type.constants %}
{% set active_item_identifier = constants.active_nav %}

{% block beforeContent %}
  {{ super() }}
  {% if runner.submission.collection.allow_multiple_submissions %}
    {{
      govukBackLink({
        "href": url_for("access_grant_funding.list_collection_submissions", organisation_id=grant_recipient.organisation.id, grant_id=grant_recipient.grant.id, collection_id=runner.submission.collection_id),
        "text": "Back"
      })
    }}
  {% else %}
    {{
      govukBackLink({
        "href": url_for("access_grant_funding.list_collections", organisation_id=grant_recipient.organisation.id, grant_id=grant_recipient.grant.id),
        "text": "Back"
      })
    }}
  {% endif %}
{% 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_VALIDATION_ERROR]) %}
        {% if flashes %}
          {% set error_list = [{"html": flashes[0], "href": "#submit"}] %}
          {{
            govukErrorSummary({
              "titleText": "There is a problem",
              "errorList": error_list
            })
          }}
        {% endif %}
      {% endwith %}

      <h1 class="govuk-heading-l">{{ runner.submission.long_collection_name }}</h1>

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

  {% if runner.submission.collection.requires_certification %}
    {% set before_submission %}
      <h2 class="govuk-heading-m">Submit your {{ constants.singular }} to your certifier</h2>
      <p class="govuk-body">You can submit the {{ constants.singular }} for sign off once it's complete.</p>
      <p class="govuk-body">
        We'll send the {{ constants.singular }} to {{ grant_recipient.certifier_names }}. View who has access to the {{ constants.singular }} and their permissions on the
        <a class="govuk-link" href="{{ url_for('access_grant_funding.list_grant_team', organisation_id=grant_recipient.organisation.id, grant_id=grant_recipient.grant.id) }}">Team page</a>.
      </p>
      <p class="govuk-body">You cannot make changes while your {{ constants.singular }} is awaiting sign off.</p>
    {% endset %}
    {% set submit_text = "Submit " ~ constants.singular ~ " to certifier" %}
  {% else %}
    {% set before_submission %}
      <h2 class="govuk-heading-m">Submit your {{ constants.singular }}</h2>
      <p class="govuk-body">Once you've completed your {{ constants.singular }}, you can submit it to the {{ runner.submission.collection.grant.organisation.name }}.</p>
    {% endset %}
    {% set submit_text="Continue to submit" %}
  {% endif %}

  {{ collection_tasklist(runner, submit_text, before_submission=before_submission) }}
{% endblock content %}
