{% extends "auth_template.html" %}

{% load i18n %}
{% load widget_tweaks %}

{% block auth_content %}
    <div class="row">
        <div class="col-md-8">
            <form method="post" action="{% url 'account_email' %}" class="add_email">
                {% csrf_token %}
                {{ form.errors.email }}
                <div class="input-group">
                    {% render_field form.email class="form-control" %}
                    <span class="input-group-btn">
                        <button type="submit" name="action_add" class="btn btn-outline-success">
                            <i class="fa fa-plus"></i>
                        </button>
                    </span>
                </div>
            </form>

            <br/>

            <form action="{% url 'account_email' %}" class="email_list" method="post">
                {% csrf_token %}
                <fieldset class="blockLabels">

                    {% for emailaddress in user.emailaddress_set.all %}
                        <div class="input-group">
                            <span class="input-group-addon email-radio">
                                <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 }}"/>
                            </span>
                            <label class="input-group-addon email-address" for="email_radio_{{ forloop.counter }}"
                                   class="{% if emailaddress.primary %}primary_email{% endif %}">

                                {{ emailaddress.email }}
                            </label>
                            {% if emailaddress.verified %}
                                <span class="input-group-addon email-verified">{% trans "Verified" %}</span>
                            {% else %}
                                <span class="input-group-addon email-unverified">{% trans "Unverified" %}</span>
                            {% endif %}
                            {% if emailaddress.primary %}
                                <span class="input-group-addon email-primary">{% trans "Primary" %}</span>
                            {% endif %}
                        </div>
                        <br/>
                    {% endfor %}

                    <div class="btn-toolbar">
                        <button class="btn btn-outline-primary" type="submit"
                                name="action_primary">{% trans 'Make Primary' %}</button>
                        <button class="btn btn-secondary" type="submit"
                                name="action_send">{% trans 'Re-send Verification' %}</button>
                        <button class="btn btn-outline-danger" type="submit"
                                name="action_remove">{% trans 'Remove' %}</button>
                    </div>

                </fieldset>
            </form>

        </div>

        <div class="col-md-4">
            <p>All correspondence is sent only to your <strong>primary</strong> address. Secondary addresses may be used
                to login to the application.</p>

            <p>To change your address you must first add the new email and confirm it. You can then make the new address
                your <em>primary</em> address and, if desired, remove the old one.</p>


        </div>

    </div>

{% endblock %}


{% block closing_js %}
    <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 %}
