{% extends "standard-form.njk" %}
{% from "input/macro.njk" import govukInput %}
{% from "radios/macro.njk" import govukRadios %}

{% set errorMap = {
        'email-address': {
            'string.empty': { ref: '#email-address', text: 'You have not entered an email address' },
            'string.email': { ref: '#email-address', text: 'You did not enter a valid email address' },
            'any.invalid': { ref: '#email-address', text: 'Enter a different email address'  }
        },
        'change-email': {
            'any.required': { ref: '#email-address', text: 'What is the email address'  }
        }
    }
%}

{% set emailAddress %}
   {{ govukInput({
      id: "email-address",
      name: "email-address",
      type: "email",
      autocomplete: "email",
      spellcheck: false,
      classes: "govuk-input--width-50",
      attributes: { maxlength: 50 },
      errorMessage: { text: 'Enter the email address' } if error['email-address']
    }) }}
{% endset -%}

{% block pageContent %}
    {% if not data.email %}
        {{ emailAddress | safe }}
        <input type="hidden" id="change-email" name="change-email" value="yes">
    {% else %}
        {{ govukRadios({
          idPrefix: "change-email",
          name: "change-email",
          hint: { text: hintText } if hintText else '',
          errorMessage: {
            text: noneSelected
          } if error['change-email'] === 'any.required',
          items: [
           {
                value: "no",
                text: data.email,
                checked: true
            },
            {
              divider: "or"
            },
            {
              value: "yes",
              text: "Add a new email address",
              conditional: {
                html: emailAddress
              }
            }
          ]
        }) }}
    {% endif %}
{% endblock %}
