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

{% set type_constants = collection.type.constants %}
{% set page_title = "Submissions - " ~ collection.name %}
{% set active_item_identifier = type_constants.active_nav %}

{% block beforeContent %}
  {% if submission_mode==enum.submission_mode.TEST %}
    {{ mhclgInlinePageTestDataBanner("Test submissions") }}
  {% endif %}
  {{ deliver_grant_funding_collection_breadcrumb(grant=grant, collection=collection, this_page_breadcrumb_item={"text":"Submissions"}) }}
{% endblock beforeContent %}

{% block content %}
  {% if delete_all_form %}
    {% set banner_html %}
      <p class="govuk-body govuk-!-font-weight-bold">Are you sure you want to reset all test submissions?</p>
      <form method="post" novalidate>
        {{ delete_all_form.csrf_token }}
        <div class="govuk-button-group govuk-!-margin-bottom-0">
          {{ delete_all_form.confirm_deletion(params={"text": "Yes, reset all test submissions", "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.list_submissions', grant_id=grant.id, collection_type=collection.type, collection_id=collection.id, submission_mode=submission_mode) }}"
            >Cancel</a
          >
        </div>
      </form>
    {% endset %}

    {{ govukNotificationBanner(params={"role": "alert", "titleText": "Important", "classes": "", "html": banner_html}) }}
  {% endif %}

  <div class="govuk-grid-row">
    <div class="govuk-grid-column-two-thirds">
      {% with flashes = get_flashed_messages(category_filter=[enum.flash_message_type.TEST_SUBMISSION_RESET ]) %}
        {% if flashes %}
          {% set test_submission_reset_html %}
          {% set user_can_test_grant_recipient_journey = collection.grant.test_grant_recipients and collection.grant in current_user.access_grants %}
            <h3 class="govuk-notification-banner__heading">Test submission reset</h3>
            <p class="govuk-body">Your team's test submission has been reset.</p>

            {% if user_can_test_grant_recipient_journey %}
              <p class="govuk-body">
                To start again you can
                <a class="govuk-notification-banner__link" href="{{ url_for('deliver_grant_funding.start_test_grant_recipient_journey', grant_id=collection.grant.id, collection_type=collection.type, collection_id=collection.id) }}"
                  >test the grant recipient journey for this {{ type_constants.singular }}</a
                >.
              </p>
            {% endif %}
          {% endset %}
          {{
            govukNotificationBanner({
              "html": test_submission_reset_html,
              "type":"success"
            })
          }}
        {% endif %}
      {% endwith %}
      {% with flashes = get_flashed_messages(category_filter=[enum.flash_message_type.TEST_SUBMISSIONS_RESET ]) %}
        {% if flashes %}
          {% set test_submission_reset_html %}
          {% set user_can_test_grant_recipient_journey = collection.grant.test_grant_recipients and collection.grant in current_user.access_grants %}
            <h3 class="govuk-notification-banner__heading">Test submissions reset</h3>
            <p class="govuk-body">All test submissions have been reset.</p>

            {% if user_can_test_grant_recipient_journey %}
              <p class="govuk-body">
                To start again you can
                <a class="govuk-notification-banner__link" href="{{ url_for('deliver_grant_funding.start_test_grant_recipient_journey', grant_id=collection.grant.id, collection_type=collection.type, collection_id=collection.id) }}">
                  test the grant recipient journey for this {{ type_constants.singular }} </a
                >.
              </p>
            {% endif %}
          {% endset %}
          {{
            govukNotificationBanner({
              "html": test_submission_reset_html,
              "type":"success"
            })
          }}
        {% endif %}
      {% endwith %}

      <h1 class="govuk-heading-l">
        <span class="govuk-caption-l">{{ collection.name }}</span>
        {{ type_constants.plural | uppercase_first }}
      </h1>
      <p class="govuk-body">View the information that has been provided by grant recipients in their {{ type_constants.plural }}.</p>
      <p class="govuk-body">
        {{
          govukButton({
            "text": "Export as CSV",
            "href": url_for("deliver_grant_funding.export_collection_submissions", grant_id=grant.id, collection_type=collection.type, collection_id=collection.id, submission_mode=submission_mode, export_format='csv'),
            "classes": "govuk-button--secondary govuk-!-margin-bottom-2",
          })
        }}
        {{
          govukButton({
            "text": "Export as JSON",
            "href": url_for("deliver_grant_funding.export_collection_submissions", grant_id=grant.id, collection_type=collection.type, collection_id=collection.id, submission_mode=submission_mode, export_format='json'),
            "classes": "govuk-button--secondary govuk-!-margin-bottom-2",
          })
        }}
      </p>
    </div>
    <div class="govuk-grid-column-full">
      {% set rows = [] %}
      {% set column_count = 4 if collection.allow_multiple_submissions else 3 %}

      {% for submission in submissions %}
        {% set cells = [] %}

        {% if collection.allow_multiple_submissions %}
          {% if submission.submission_id %}
            {% set submission_name_link %}
              <a class="govuk-link govuk-link--no-visited-state" href="{{ url_for('deliver_grant_funding.view_submission', grant_id=grant.id, submission_id=submission.submission_id) }}" data-submission-link>{{ submission.name }}</a>
            {% endset %}
            {% do cells.append({"html": submission_name_link}) %}
          {% else %}
            {% do cells.append({"text": "No submission"}) %}
          {% endif %}
          {% do cells.append({"text": submission.organisation_name}) %}
        {% elif submission.submission_id %}
          {% set grant_recipient_link %}
            <a class="govuk-link govuk-link--no-visited-state" href="{{ url_for('deliver_grant_funding.view_submission', grant_id=grant.id, submission_id=submission.submission_id) }}" data-submission-link
              >{{ submission.organisation_name }}</a
            >
          {% endset %}
          {% do cells.append({"html": grant_recipient_link}) %}
        {% else %}
          {% do cells.append({"text": submission.organisation_name}) %}
        {% endif %}

        {% if submission.is_assessed %}
          {% do cells.append({"html": assessmentStatusTag(submission.assessment_status)}) %}
        {% else %}
          {% if submission.submission_id %}
            {% do cells.append({"html": submissionStatusTag(submission.status, submission.is_overdue)}) %}
          {% else %}
            {% do cells.append({"html": submissionStatusTag(enum.submission_status.NOT_SUBMITTED if collection.is_closed else enum.submission_status.NOT_STARTED, collection.is_overdue)}) %}
          {% endif %}
        {% endif %}
        {% do cells.append({"text": format_date_short(submission.last_updated_at_utc) if submission.last_updated_at_utc else ""}) %}
        {% do rows.append(cells) %}
      {% else %}
        {% set no_submissions_text %}
          <span>No submissions found for this {{ type_constants.singular }}</span>
        {% endset %}
        {% do rows.append([{"html": no_submissions_text, "colspan": column_count}]) %}
      {% endfor %}

      {% set head = [] %}
      {% if collection.allow_multiple_submissions %}
        {% do head.append({"text": (collection.submission_name_question.name | uppercase_first) if collection.submission_name_question else "Submission", "classes": "govuk-!-width-one-third"}) %}
        {% do head.append({"text": "Grant recipient"}) %}
      {% else %}
        {% do head.append({"text": "Grant recipient", "classes": "govuk-!-width-one-half"}) %}
      {% endif %}
      {% do head.append({"text": "Status"}) %}
      {% do head.append({"text": "Last updated"}) %}

      {{
        govukTable({
          "captionClasses": "govuk-table__caption--m",
          "head": head,
          "rows": rows
        })
      }}
    </div>
  </div>

  {% if submission_mode == enum.submission_mode.TEST %}
    <div class="govuk-grid-row">
      <div class="govuk-grid-column-two-thirds">
        <h2 class="govuk-heading-m govuk-!-margin-top-5">Manage test submissions</h2>
        <ul class="govuk-list govuk-list--spaced">
          <li><a class="govuk-link govuk-link--no-visited-state" href="?delete_all">Reset all test submissions</a></li>
        </ul>
      </div>
    </div>
  {% endif %}
{% endblock content %}
