{% extends "access_grant_funding/base.html" %}
{% from "govuk_frontend_jinja/components/table/macro.html" import govukTable %}
{% from "common/macros/status.html" import submissionStatusTag with context %}
{% from "common/macros/status.html" import multiSubmissionStatusTag with context %}
{% from "govuk_frontend_jinja/components/notification-banner/macro.html" import govukNotificationBanner %}

{% set page_title = "Reports" %}
{% set active_item_identifier = "reports" %}
{% 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_SIGN_OFF_DECLINED]) %}
        {% if flashes %}
          {% set submission_declined_html %}
            <h3 class="govuk-notification-banner__heading">Report declined</h3>
            <p class="govuk-body">
              {{ flashes[0]["collection_name"] }} report for {{ flashes[0]["grant_name"] }} sign off declined and sent back to {{ flashes[0]["sent_for_certification_by"] }}.
              <a
                class="govuk-notification-banner__link"
                href="{{ url_for('access_grant_funding.route_to_submission', grant_id=grant_recipient.grant_id, organisation_id=grant_recipient.organisation_id,collection_id=flashes[0]["collection_id"]) }}">
                View report
              </a>
            </p>
          {% endset %}
          {{
            govukNotificationBanner({
              "html": submission_declined_html,
              "type":"success",
              "titleText": "Confirmed"
            })
          }}
        {% endif %}
      {% endwith %}
    </div>
    <div class="govuk-grid-column-two-thirds">
      <h1 class="govuk-heading-l">Reports</h1>
    </div>
  </div>
  <div class="govuk-grid-row">
    <div class="govuk-grid-column-full">
      {% if monitoring_reports|length == 0 %}
        <p class="govuk-body">There are no reports for this grant.</p>
      {% else %}
        {% set rows=[] %}
        {% for report in monitoring_reports %}
          {% set report_name = report.name or (format_date_range_short(report.reporting_period_start_date, report.reporting_period_end_date) if (report.reporting_period_start_date and report.reporting_period_end_date) else "Dates to be confirmed") %}
          {% set report_submissions = submissions | selectattr("collection_id", "equalto", report.id) | list %}
          {% set collection_helper = collection_helpers | selectattr("id", "equalto", report.id) | list | first %}

          {% if report.allow_multiple_submissions %}
            {% set submission_status_html %}
              {{ multiSubmissionStatusTag(report_submissions, collection_helper, report.is_overdue) }}
            {% endset %}
            {% set report_action_html %}
              <a class="govuk-link govuk-link--no-visited-state" href="{{ url_for('access_grant_funding.route_to_submission', organisation_id=grant_recipient.organisation.id, grant_id=grant_recipient.grant.id, collection_id=report.id) }}">
                Go to reports
                <span class="govuk-visually-hidden"> for {{ report_name }} </span>
              </a>
            {% endset %}
            {%
              do rows.append([
                {"text": report_name},
                {"text": format_date_short(report.submission_period_end_date) if report.submission_period_end_date else "Dates to be confirmed"},
                {"html": submission_status_html},
                {"html": report_action_html}
              ])
            %}
          {% else %}
            {% set submission = (report_submissions | first) if report_submissions else none %}
            {% set submission_status = SubmissionHelper.get_status(submission.submission, report) %}
            {% set submission_status_html %}
              {{ submissionStatusTag(submission_status, submission.submission.is_overdue if submission else report.is_overdue) }}
            {% endset %}
            {% set report_action_config = SubmissionHelper.get_access_submission_action(report, grant_recipient, submission.submission) %}
            {% set report_action_html %}
              {% if report_action_config.href %}
                <a class="govuk-link govuk-link--no-visited-state" href="{{ report_action_config.href }}">
                  {{ report_action_config.label }}
                  <span class="govuk-visually-hidden"> for {{ report_name }} </span>
                </a>
              {% elif report_action_config.label %}
                <span>{{ report_action_config.label }}</span>
              {% endif %}
            {% endset %}
            {# todo: standardise this behaviour everywhere by adding this logic as a property on the model if its signed off #}
            {%
              do rows.append([
                {"text": report_name},
                {"text": format_date_short(report.submission_period_end_date) if report.submission_period_end_date else "Dates to be confirmed"}, {"html": submission_status_html},
                {"html": report_action_html}
              ])
            %}
          {% endif %}
        {% endfor %}
        {% set actions_html %}
          <span class="govuk-visually-hidden">Actions</span>
        {% endset %}
        {{
          govukTable(params={
                  "firstCellIsHeader": false,
                  "head":[
                    {"text": "Report"},
                    {"text": "Due date"},
                    {"text": "Status"},
                    {"html": actions_html}
                  ],
                  "rows":rows
          })
        }}
      {% endif %}
    </div>
  </div>
{% endblock %}
