{% extends "deliver_grant_funding/grant_base.html" %}
{% from "govuk_frontend_jinja/components/summary-list/macro.html" import govukSummaryList %}
{% from "govuk_frontend_jinja/components/table/macro.html" import govukTable %}
{% from "common/macros/status.html" import grantStatusTag with context %}

{% set page_title = "Grant details - " ~ grant.name %}
{% set active_item_identifier = "details" %}

{% block content %}
  <div class="govuk-grid-row">
    <div class="govuk-grid-column-full">
      <h1 class="govuk-heading-l">
        <span class="govuk-caption-l">{{ grant.name }}</span>
        Grant details
      </h1>

      {% set privacy_policy_link %}
        {% if grant.privacy_policy_markdown %}
          <a class="govuk-link govuk-link--no-visited-state" href="{{ url_for('access_grant_funding.privacy_policy', grant_id=grant.id) }}" target="_blank">Review the grant's privacy policy (opens in a new tab)</a>
        {% else %}
          The grant does not have a privacy policy yet.
        {% endif %}
      {% endset %}
      {{
        govukSummaryList({
          "rows": [
            {
              "key": {"text": "Grant status"},
              "value": {"html": grantStatusTag(grant.status)},
            },
            {
              "key": {"text": "GGIS reference number"},
              "value": {"text": grant.ggis_number},
              "actions": {
                "items": [{
                  "href": url_for("deliver_grant_funding.grant_change_ggis", grant_id=grant.id),
                  "classes": "govuk-link--no-visited-state",
                  "text": "Change",
                  "visuallyHiddenText": "GGIS reference number"
                }]
              } if authorisation_helper.is_platform_admin(current_user) else {}
            },
            {
              "key": {"text": "Grant name"},
              "value": {"text": grant.name},
              "actions": {
                "items": [{
                  "href": url_for("deliver_grant_funding.grant_change_name", grant_id=grant.id),
                  "classes": "govuk-link--no-visited-state",
                  "text": "Change",
                  "visuallyHiddenText": "grant name"
                }]
              } if authorisation_helper.has_deliver_grant_role(grant.id, roles_enum.ADMIN, current_user) else {}
            },
            {
              "key": {"text": "Grant code"},
              "value": {"text": grant.code}
            },
            {
              "key": {"text": "Main purpose"},
              "value": {"text": grant.description},
              "actions": {
                "items": [{
                  "href": url_for("deliver_grant_funding.grant_change_description", grant_id=grant.id),
                  "classes": "govuk-link--no-visited-state",
                  "text": "Change",
                  "visuallyHiddenText": "main purpose"
                }]
              } if authorisation_helper.has_deliver_grant_role(grant.id, roles_enum.ADMIN, current_user) else {}
            },
            {
              "key": {"text": "Main contact"},
              "value": {"html": grant.primary_contact_name + "<br>" + grant.primary_contact_email},
              "actions": {
                "items": [{
                  "href": url_for("deliver_grant_funding.grant_change_contact", grant_id=grant.id),
                  "classes": "govuk-link--no-visited-state",
                  "text": "Change",
                  "visuallyHiddenText": "main contact"
                }]
              } if authorisation_helper.has_deliver_grant_role(grant.id, roles_enum.ADMIN, current_user) else {}
            },
            {
              "key": {"text": "Privacy policy"},
              "value": {"html": privacy_policy_link},
              "actions": { }
            }
          ]
        })
      }}

      <h2 class="govuk-heading-m govuk-!-margin-top-9">Recipients</h2>

      {% set tableRows = [] %}
      {% if not grant_recipients %}
        {% set noGrantRecipientsHtml %}
          <span>No grant recipients have been set up yet</span>
        {% endset %}
        {% do tableRows.append([{"html": noGrantRecipientsHtml, "colspan": 2}]) %}
      {% else %}
        {% for grant_recipient in grant_recipients %}
          {% set userDetails %}
            <p class="govuk-body govuk-!-font-weight-bold govuk-!-margin-bottom-0">Can edit and submit reports:</p>
            {% if grant_recipient.data_providers %}
              <ul class="govuk-list govuk-list--bullet">
                {% for user in grant_recipient.data_providers | sort(attribute="name") %}
                  <li>{{ user.name }} ({{ user.email }})</li>
                {% endfor %}
              </ul>
            {% else %}
              <p class="govuk-body govuk-!-margin-bottom-0">No data providers have been set up</p>
            {% endif %}
            <p class="govuk-body govuk-!-font-weight-bold govuk-!-margin-bottom-0">Certifiers:</p>
            {% if grant_recipient.certifiers %}
              <ul class="govuk-list govuk-list--bullet govuk-!-margin-bottom-0">
                {% for user in grant_recipient.certifiers | sort(attribute="name") %}
                  <li>{{ user.name }} ({{ user.email }})</li>
                {% endfor %}
              </ul>
            {% else %}
              <p class="govuk-body govuk-!-margin-bottom-0">No certifiers have been set up</p>
            {% endif %}
          {% endset %}
          {%
            do tableRows.append([
              {"text": grant_recipient.organisation.name},
              {"html": userDetails}
            ])
          %}
        {% endfor %}
      {% endif %}

      {{
        govukTable({
          "head": [
            {"text": "Grant recipient"},
            {"text": "Users"}
          ],
          "rows": tableRows
        })
      }}
    </div>
  </div>
{% endblock content %}
