{% extends "access_grant_funding/base.html" %}
{% from "govuk_frontend_jinja/components/back-link/macro.html" import govukBackLink %}
{% from "govuk_frontend_jinja/components/table/macro.html" import govukTable %}
{% from "common/macros/status.html" import submissionStatusTag with context %}
{% from "govuk_frontend_jinja/components/button/macro.html" import govukButton %}

{% set page_title = collection.name %}
{% set active_item_identifier = collection.type.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),
      "text": "Back"
    })
  }}
{% endblock beforeContent %}

{% block content %}
  <div class="govuk-grid-row">
    <div class="govuk-grid-column-two-thirds">
      <h1 class="govuk-heading-l">{{ collection.name }}</h1>

      {% if collection.submission_guidance %}
        {{ collection.submission_guidance | govuk_markdown }}
      {% endif %}
    </div>
  </div>
  <div class="govuk-grid-row">
    <div class="govuk-grid-column-two-thirds">
      <dl class="app-metadata govuk-!-margin-bottom-6">
        <dt class="app-metadata__key">Submission deadline:</dt>
        <dd class="app-metadata__value" data-testid="submission-status">
          {% if collection.submission_period_end_date %}
            {{ format_date(collection.submission_period_end_date) }}
          {% else %}
            <span>(Dates to be confirmed)</span>
          {% endif %}
        </dd>
      </dl>
    </div>
  </div>
  <div class="govuk-grid-row">
    <div class="govuk-grid-column-full">
      {% if submission_helpers|length == 0 %}
        <p class="govuk-body govuk-!-margin-bottom-7">You have not started any {{ collection.type.constants.plural }} yet.</p>
      {% else %}
        {% set rows=[] %}
        {% for submission_helper in submission_helpers|sort(attribute="submission_name") %}
          {% set submission_status = submission_helper.status %}
          {% set submission_status_html %}
            {{ submissionStatusTag(submission_status, submission_helper.submission.is_overdue) }}
          {% endset %}
          {% set action_config = submission_helper.get_access_submission_action(collection, grant_recipient, submission_helper.submission) %}
          {% set action_html %}
            {% if action_config.href %}
              <a class="govuk-link govuk-link--no-visited-state" href="{{ action_config.href }}">
                {{ action_config.label }}
                <span class="govuk-visually-hidden"> for {{ submission_helper.submission_name }} </span>
              </a>
            {% elif action_config.label %}
              <span>{{ action_config.label }}</span>
            {% endif %}
          {% endset %}
          {%
            do rows.append([
              {"text": submission_helper.submission_name },
              {"html": submission_status_html},
              {"html": action_html}
            ])
          %}
        {% endfor %}
        {% set actions_html %}
          <span class="govuk-visually-hidden">Actions</span>
        {% endset %}
        {{
          govukTable(params={
                  "firstCellIsHeader": false,
                  "head":[
                    {"text": (collection.submission_name_question.name | uppercase_first )},
                    {"text": "Status"},
                    {"html": actions_html}
                  ],
                  "rows":rows
          })
        }}
      {% endif %}

      {% if can_create_submissions %}
        {{
          govukButton({
          "text": "Start a new " + collection.type.constants.singular,
          "classes": "govuk-button--secondary" if submission_helpers|length else "",
          "href": url_for('access_grant_funding.start_new_multiple_submission', organisation_id=grant_recipient.organisation.id, grant_id=grant_recipient.grant.id, collection_id=collection.id)})
        }}
      {% endif %}
    </div>
  </div>
{% endblock %}
