{% extends "common/base.html" %}
{% from "common/partials/mhclg-header.html" import mhclgHeader %}
{% from "common/macros/navigation.html" import accessGrantFundingServiceNavigation %}
{% from "govuk_frontend_jinja/components/footer/macro.html" import govukFooter %}
{% from "common/partials/mhclg-test-banner.html" import mhclgTestBanner, mhclgRequestTracingBanner with context %}
{% from "govuk_frontend_jinja/components/cookie-banner/macro.html" import govukCookieBanner %}

{% set nav_items = nav_items or [] %}
{% set right_nav_items = right_nav_items or [] %}

{% block pageTitle %}{{ page_title }} - {{ super() }}{% endblock pageTitle %}
{% block head %}
  {{ super() }}
  <script nonce="{{ csp_nonce() }}">
    window.dataLayer = window.dataLayer || [];
    function gtag() {
      dataLayer.push(arguments);
    }
    gtag("consent", "default", {
      ad_user_data: "denied",
      ad_personalization: "denied",
      ad_storage: "denied",
      analytics_storage: "denied",
      wait_for_update: 500,
    });
    dataLayer.push({ "gtm.start": new Date().getTime(), event: "gtm.js", env: "{{get_current_env_name()}}" });

    gtag("set", "user_properties", {
      is_deliver_user: "{{'true' if current_user.is_authenticated and current_user.email.endswith(config['INTERNAL_DOMAINS']) else 'false'}}",
    });
    window.googleTagId = "{{ get_google_tag_manager_id() }}";
  </script>
{% endblock head %}
{% block header %}
  {{ mhclgRequestTracingBanner() }}
  {{
    mhclgHeader(
      params={
        "homepageUrl": "#",
        "assetPath": assetPath,
        "productName": "MHCLG Access grant funding",
        "isAccess": true
      },
      currentUser=current_user
    )
  }}

  {% set user_organisation = grant_recipient.organisation if grant_recipient else (organisation if organisation else None) %}
  {% set is_deliver_user_testing = authorisation_helper.is_deliver_user_testing_access(current_user, user_organisation=user_organisation) %}
  {% if is_deliver_user_testing %}
    {{ mhclgTestBanner("Testing grant recipient journey") }}
  {% endif %}

  {% if current_user.is_authenticated %}
    {% if grant_recipient %}
      {%
        set nav_items = [
            { "text": "Forms" if feature_flags.PRE_AWARD.is_enabled else "Reports", "href": url_for("access_grant_funding.list_collections", organisation_id=grant_recipient.organisation.id, grant_id=grant_recipient.grant.id), "active": active_item_identifier in ["pre_award", "reports"] },
            { "text": "Team", "href": url_for("access_grant_funding.list_grant_team", organisation_id=grant_recipient.organisation.id, grant_id=grant_recipient.grant.id), "active": active_item_identifier == "team" },
        ]
      %}
    {% else %}
      {% set nav_items = [] %}
    {% endif %}
    {{ accessGrantFundingServiceNavigation(current_user, nav_items, grant_recipient=grant_recipient, organisation=organisation if not grant_recipient else None) }}
  {% endif %}
{% endblock header %}

{% block bodyStart %}
  {% set cookie_banner_html %}
    <p class="govuk-body">We use some essential cookies to make this service work.</p>
    <p class="govuk-body">We’d also like to use analytics cookies so we can understand how you use the service and make improvements.</p>
  {% endset %}


  {% set acceptedHtml %}
    <p class="govuk-body">You’ve accepted additional cookies. You can <a class="govuk-link" href="{{ url_for("access_grant_funding.cookies") }}">change your cookie settings</a> at any time.</p>
  {% endset %}


  {% set rejectedHtml %}
    <p class="govuk-body">You’ve rejected additional cookies. You can <a class="govuk-link" href="{{ url_for("access_grant_funding.cookies") }}">change your cookie settings</a> at any time.</p>
  {% endset %}

  {{
    govukCookieBanner({
      "ariaLabel": "Cookies on Access grant funding",
      "attributes":{"id":"cookie_banner"},
      "hidden":true,
      "messages": [
        {
          "headingText": "Cookies on Access grant funding",
          "html": cookie_banner_html,
          "attributes":{"id":"cookie_choice_msg"},
          "actions": [
            {
              "text": "Accept analytics cookies",
              "attributes":{"id":"btn_accept_analytics_cookies"},
              "type": "button"
            },
            {
              "text": "Reject analytics cookies",
              "attributes":{"id":"btn_reject_analytics_cookies"},
              "type": "button"
            },
            {
              "text": "View cookies",
              "href": url_for("access_grant_funding.cookies")
            }
          ],
          "hidden": true
        },
        {
          "html": acceptedHtml,
          "attributes":{"id":"cookies_accepted_msg"},
          "actions": [
            {
              "text": "Hide cookie message",
              "type": "submit",
              "name": "cookies[hide]",
              "value": "yes",
              "attributes":{"id":"btn_hide_cookies"},
            }
          ],
          "hidden": true
        },
        {
          "html": rejectedHtml,
          "attributes":{"id":"cookies_rejected_msg"},
          "actions": [
            {
              "text": "Hide cookie message",
              "type": "submit",
              "name": "cookies[hide]",
              "value": "yes",
              "attributes":{"id":"btn_hide_cookies"},
            }
          ],
          "hidden": true
        }
      ]
    })
  }}
{% endblock bodyStart %}
{% block bodyEnd %}
  {{ super() }}
  <script type="module" src="{{ vite_asset('app/assets/components/cookie-consent/index.js') }}"></script>
{% endblock %}
{% block footer %}
  {% set grant_id = grant.id if grant is defined else request.view_args.grant_id %}

  {{
    govukFooter({
      "contentLicence": none,
      "meta": {
        "items": [
          {
            "href": url_for("access_grant_funding.accessibility_statement"),
            "text": "Accessibility statement"
          },
          {
            "href": url_for("access_grant_funding.cookies"),
            "text": "Cookies"
          },
          {
            "href": url_for("access_grant_funding.privacy_policy", grant_id=grant_id) if grant_id else url_for("access_grant_funding.privacy_policy"),
            "text": "Privacy"
          },
          {
            "href": config.ACCESS_SERVICE_DESK_URL,
            "text": "Contact us"
          },
        ]
      }
    })
  }}
{% endblock footer %}
