{% extends "base.htm" %}
{% from 'bs_page_header.htm' import bs_page_header with context %}
{% from 'bs_form_body.htm' import bs_form_body with context %}

{% block main %}
{{ bs_page_header(_("E-mail Addresses")) }}
{% if request.user.emailaddress_set.exists() %}

<div class="panel panel-default">
    <div class="panel-heading"><h3 class="panel-title">{{ _('The following e-mail addresses are associated with your account:') }}</h3></div>
    <div class="panel-body">
        <form role="form" action="{{ url('account_email') }}" class="email_list" method="post">
            <input type="hidden" name="csrfmiddlewaretoken" value="{{ csrf_token }}">
            <fieldset class="blockLabels">

            {% for emailaddress in request.user.emailaddress_set.all() %}
            <div class="ctrlHolder">
                <label for="email_radio_{{ loop.index0 }}" class="{% if emailaddress.primary %}primary_email{%endif%}">
                    <input id="email_radio_{{ loop.index0 }}" type="radio" name="email" {% if emailaddress.primary or request.user.emailaddress_set.count() == 1 %}checked="checked"{%endif %} value="{{emailaddress.email}}"/>

                    {{ emailaddress.email }}
                    {% if emailaddress.verified %}
                    <span class="verified">{{ _("Verified") }}</span>
                    {% else %}
                    <span class="unverified">{{ _("Unverified") }}</span>
                    {% endif %}
                    {% if emailaddress.primary %}
                    <span class="primary">{{ _("Primary") }}</span>
                    {% endif %}
                </label>
            </div>
            {% endfor %}

            <div class="buttonHolder">
                  <button class="btn btn-primary secondaryAction" type="submit" name="action_primary" >{{ _('Make Primary') }}</button>
                  <button class="btn btn-info secondaryAction" type="submit" name="action_send" >{{ _('Re-send Verification') }}</button>
                  <button class="btn btn-danger primaryAction" type="submit" name="action_remove" >{{ _('Remove') }}</button>
            </div>

            </fieldset>
        </form>
    </div>
</div>

{% else %}
<div class="jumbotrion">
    <strong>{{ _('Warning:') }}</strong> {{ _("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.") }}
</div>
{% endif %}


<div class="panel panel-default">
    <div class="panel-heading"><h3 class="panel-title">{{ _("Add E-mail Address") }}</h3></div>
    <div class="panel-body">
        <form role="form" method="post" action="{{ url('account_email') }}" class="add_email">
            {{ bs_form_body(form) }}
            <button class="btn btn-primary" name="action_add" type="submit">{{ _("Add E-mail") }}</button>
        </form>
    </div>
</div>

{% endblock main %}


{% block bottom_scripts %}
<script type="text/javascript">
(function() {
    var message = "{{ _('Do you really want to remove the selected e-mail address?') }}";
    var actions = document.getElementsByName('action_remove');
    var confirmed = false;
    if (actions.length) {
        actions[0].addEventListener("click", function(e) {
            if (confirmed === false) {
                new App.Dialog({
                    'message': message,
                    'callback': function(result) {
                        confirmed = result;
                        if (confirmed) {
                            $(actions[0]).click();
                        }
                    }
                }).confirm();
                e.preventDefault();
            }
            confirmed = false;
        });
    }
})();
</script>
{% endblock bottom_scripts %}
