{% extends "account/base.html" %}

{% load i18n %}
{% load django_bootstrap5 %}

{% block head_title %}{% trans "E-mail Addresses" %}{% endblock %}
{% block title %}{% trans "E-mail Addresses" %}{% endblock %}

{% block accounts_content %}
  <div class="header_custom">
    <div class="branding_custom">
      <h1 class="site-name_custom"><a>{% trans "E-mail Addresses" %}</a></h1>
    </div>
  </div>
  <div class="main_form">
    {% if user.emailaddress_set.all %}
      <p>{% trans 'The following e-mail addresses are associated with your account:' %}</p>
      <form action="{% url 'account_email' %}" class="email_list" method="post">
        {% csrf_token %}
        <fieldset class="blockLabels">

          {% for emailaddress in user.emailaddress_set.all %}
            <div class="ctrlHolder">
              <label for="email_radio_{{ forloop.counter }}"
                     class="{% if emailaddress.primary %}primary_email{% endif %}">

                <input id="email_radio_{{ forloop.counter }}" type="radio" name="email"
                       {% if emailaddress.primary or user.emailaddress_set.count == 1 %}checked="checked"{% endif %}
                       value="{{ emailaddress.email }}"/>

                {{ emailaddress.email }}
                {% if emailaddress.verified %}
                  <span class="verified">{% trans "Verified" %}</span>
                {% else %}
                  <span class="unverified">{% trans "Unverified" %}</span>
                {% endif %}
                {% if emailaddress.primary %}
                  <span class="primary">{% trans "Primary" %}</span>{% endif %}
              </label>
            </div>
          {% endfor %}
          <div class="buttonHolder">
            {% trans "Make Primary" as button_make_primary %}
            {% bootstrap_button button_make_primary button_type="submit" extra_classes="secondaryAction" name="action_primary" %}
            {% trans "Re-send Verification" as button_verification %}
            {% bootstrap_button button_verification button_type="submit" extra_classes="secondaryAction" name="action_send" %}
            {% trans "Remove" as button_remove %}
            {% bootstrap_button button_remove button_type="submit" extra_classes="secondaryAction" name="action_remove" %}
          </div>
        </fieldset>
      </form>
    {% else %}
      <p>
        <strong>{% trans 'Warning:' %}</strong> {% trans "You currently do not have any e-mail address set up. You should really add an e-mail address so you can receive notifications, reset your password, etc." %}
      </p>
    {% endif %}
    <br>
    <br>
    {% if can_add_email %}
      <h4>{% trans "Add new E-mail Address" %}</h4>

      <form method="post" action="{% url 'account_email' %}" class="add_email">
        {% csrf_token %}
        {% bootstrap_form form %}
        {% trans "Add E-mail" as button_text %}
        {% bootstrap_button button_text button_type="submit" extra_classes="w-100" name="action_add" %}
      </form>
    {% endif %}
  </div>
{% endblock %}


{% block extra_body %}
  <script type="text/javascript">
    (function () {
      var message = "{% trans 'Do you really want to remove the selected e-mail address?' %}";
      var actions = document.getElementsByName('action_remove');
      if (actions.length) {
        actions[0].addEventListener("click", function (e) {
          if (!confirm(message)) {
            e.preventDefault();
          }
        });
      }
    })();
  </script>
{% endblock %}
