{% extends "deliver_grant_funding/grant_base.html" %}
{% from "govuk_frontend_jinja/components/table/macro.html" import govukTable %}
{% from "govuk_frontend_jinja/components/notification-banner/macro.html" import govukNotificationBanner %}
{% from "govuk_frontend_jinja/components/button/macro.html" import govukButton %}

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

{% set is_deliver_grant_admin = authorisation_helper.is_deliver_grant_admin(grant.id, current_user) %}

{% block content %}
  <div class="govuk-grid-row">
    <div class="govuk-grid-column-two-thirds">
      {% with flashes = get_flashed_messages() %}
        {% if flashes %}
          {% set user_add_success_message %}
            <h3 class="govuk-notification-banner__heading">Grant team member added</h3>
            <p class="govuk-body">{{ flashes[0] }}</p>
          {% endset %}
          {{
            govukNotificationBanner({
              "html": user_add_success_message, "type": "success"
            })
          }}
        {% endif %}
      {% endwith %}
      <h1 class="govuk-heading-l">
        <span class="govuk-caption-l">{{ grant.name }}</span>
        Team
      </h1>

      {% if not grant.grant_team_users %}
        <p class="govuk-body">No grant team members have been added to this grant yet.</p>
      {% endif %}

      {{
        govukButton({
            "text": "Add grant team member",
            "href": url_for("deliver_grant_funding.add_user_to_grant", grant_id=grant.id)
        })
      }}
    </div>
  </div>

  {% set table_pending_signin_rows = namespace(items=[]) %}

  {% for invited_email in invited_emails %}
    {%
      set table_pending_signin_rows.items = table_pending_signin_rows.items + [[
        {"text": invited_email},
        {"text": "Grant team member"}
      ]]
    %}
  {% endfor %}

  {% if table_pending_signin_rows.items %}
    <div class="govuk-grid-row">
      <div class="govuk-grid-column-full">
        <h2 class="govuk-heading-m govuk-!-margin-bottom-1 govuk-!-margin-top-9">Not yet signed in</h2>
        <p class="govuk-body">Grant team members who have been added but not signed in yet.</p>
        {{
          govukTable({
              "captionClasses": "govuk-table__caption--m",
              "firstCellIsHeader": false,
              "head": [{"text": "Email"}, {"text": "Role", "classes": "govuk-!-width-one-quarter"}],
              "rows": table_pending_signin_rows.items
          })
        }}
      </div>
    </div>
  {% endif %}

  {% set table_rows = namespace(items=[]) %}

  {% for user in grant.grant_team_users if authorisation_helper.has_logged_in(user) %}
    {%
      set table_rows.items = table_rows.items + [[
        {"text": user.name},
        {"text": user.email},
        {"text": "Grant team member"}
      ]]
    %}
  {% endfor %}

  {% if table_rows.items %}
    <div class="govuk-grid-row">
      <div class="govuk-grid-column-full">
        <h2 class="govuk-heading-m govuk-!-margin-bottom-1 govuk-!-margin-top-9">Grant team members</h2>
        {{
          govukTable({
              "captionClasses": "govuk-table__caption--m",
              "firstCellIsHeader": false,
              "head": [{"text": "Name"}, {"text": "Email"}, {"text": "Role", "classes": "govuk-!-width-one-quarter"}],
              "rows": table_rows.items
          })
        }}
      </div>
    </div>
  {% endif %}
  {% if not is_deliver_grant_admin %}
    <div class="govuk-grid-row">
      <div class="govuk-grid-column-full">
        <h2 class="govuk-heading-m govuk-!-margin-top-5">If you need to remove or change grant team members</h2>
        <p class="govuk-body">
          Contact us through our
          <a class="govuk-link govuk-link--no-visited-state" href="{{ service_desk_url }}" target="_blank">support desk (opens in new tab)</a>.
        </p>
        <p class="govuk-body">Monday to Friday, 9am to 5pm (except public holidays).</p>
      </div>
    </div>
  {% endif %}
{% endblock content %}
