{% extends "standard-form.njk" %}

{% from "input/macro.njk" import govukInput %}
{% from "select/macro.njk" import govukSelect %}
{% from "inset-text/macro.njk" import govukInsetText %}

{% set title = mssgs.address_entry_you if data.isLicenceForYou else mssgs.address_entry_other %}
{% set describedByTag = "premises-hint postcode-hint" %}
{% set countries = [] %}

{% for country in data.countries %}
    {{ countries.push({
        text: country.name,
        id: country.code,
        value: country.code,
        selected: payload['country-code'] === country.code
    }) }}
{% endfor %}

{%
    set errorMap = {
        'premises': {
            'string.empty': { ref: '#premises', text: mssgs.address_entry_error_empty_num },
            'string.max': { ref: '#premises', text: mssgs.address_entry_error_max_char_num },
            'string.forbidden': { ref: '#premises', text: mssgs.address_entry_error_invalid_premises }
        },
        'postcode': {
            'string.empty': { ref: '#postcode', text: mssgs.address_entry_error_empty_postcode },
            'string.pattern.base': { ref: '#postcode', text: mssgs.address_entry_error_pattern_postcode },
            'string.forbidden': { ref: '#postcode', text: mssgs.address_entry_error_invalid_postcode }
        },
        'town': {
            'string.empty': { ref: '#town', text: mssgs.address_entry_error_empty_town },
            'string.max': { ref: '#town', text: mssgs.address_entry_error_max_char_town },
            'string.forbidden': { ref: '#town', text: mssgs.address_entry_error_invalid_town }
        },
        'country': {
            'string.empty': { ref: '#country', text: mssgs.address_entry_error_empty_country }
        }
    }
%}

{% block pageContent %}

    {% if data.searchTerms %}
        {{ govukInsetText({
          html: '<p>' + mssgs.address_entry_content_unable_to_find + ' <span class="govuk-!-font-weight-bold">' +
          data.searchTerms.premises + '</span> ' + mssgs.address_entry_content_and_postcode + ' <span class="govuk-!-font-weight-bold">' + data.searchTerms.postcode + '</span></p>' +
            '<p>' + mssgs.address_entry_enter_full_add + '</p>'
         }) }}
    {% endif %}

    {{ govukInput({
      label: {
        text: mssgs.address_entry_content_name_num
      },
      id: "premises",
      name: "premises",
      hint: { text: mssgs.address_entry_content_name_num_hint },
      value: payload['premises'],
      classes: "govuk-input--width-20",
      attributes: { maxlength: 50 },
      errorMessage: { text: mssgs.address_entry_error_empty_num } if error['premises']
    }) }}

    {{ govukInput({
      label: {
        text: mssgs.address_entry_content_street_1
      },
      id: "street",
      name: "street",
      value: payload['street'],
      classes: "govuk-input--width-20",
      attributes: { maxlength: 50 }
    }) }}

    {{ govukInput({
      label: {
        text: mssgs.address_entry_content_street_2
      },
      id: "locality",
      name: "locality",
      value: payload['locality'],
      classes: "govuk-input--width-20",
      autocomplete: 'address-level2',
      attributes: { maxlength: 50 }
    }) }}

    {{ govukInput({
      label: {
        text: mssgs.address_entry_content_town
      },
      id: "town",
      name: "town",
      value: payload['town'],
      classes: "govuk-input--width-20",
      attributes: { maxlength: 50 },
      autocomplete: 'address-level1',
      errorMessage: { text: mssgs.address_entry_error_empty_town } if error['town']
    }) }}

    {{ govukInput({
      label: {
        text: mssgs.address_entry_content_postcode
      },
      id: "postcode",
      name: "postcode",
      hint: { text: mssgs.address_lookup_postcode_hint },
      value: payload['postcode'],
      classes: "govuk-input--width-20",
      attributes: { maxlength: 10 },
      autocomplete: 'postal-code',
      errorMessage: { text: mssgs.address_entry_error_empty_postcode } if error['postcode']
    }) }}

    {{ govukSelect({
      id: "country-code",
      name: "country-code",
      label: {
        text: mssgs.address_entry_content_country
      },
      items: countries,
      errorMessage: { text: mssgs.address_entry_error_empty_country } if error['country-code']
    }) }}
{% endblock %}