{% extends "sentry/bases/account.html" %}

{% load crispy_forms_tags %}
{% load i18n %}
{% load sentry_helpers %}

{% block title %}{% trans "Notification Settings" %} | {{ block.super }}{% endblock %}

{% block inner %}
    <style type="text/css">
    table label,
    table input[type=checkbox] {
        margin: 0;
    }
    table caption {
        font-weight: normal;
        text-align: left;
        font-size: 16px;
        padding-bottom: 10px;
    }
    </style>
    <script>
    $(function(){
        $('form a[data-toggle=change-target-value]').click(function(){
            var $this = $(this);
            var $target = $('#' + $this.data('target'));
            var result = window.prompt('Enter an email address', $target.val());
            $target.val(result || '');
            if (result !== null) {
                $this.text(result);
            } else {
                $this.html('<em>default</em>');
            }
        })
    });
    </script>

    <form action="" method="post">
        {% csrf_token %}

        <p>{% blocktrans %}Notifications are generally sent when a new event is
            seen, an already resolved event has changed to unresolved, or a
            system-wide spike in events is detected.{% endblocktrans %}</p>

        {% with settings_form as form %}
            <fieldset>
                <div><legend>{% trans "General" %}</legend></div>
                {% include "sentry/partial/form_base.html" %}
            </fieldset>
        {% endwith %}

        {% if project_forms %}
            <fieldset>
                <div><legend>{% trans "Mail" %}</legend></div>
                <p>{% blocktrans %}Choose which projects you wish to receive email notifications for, as well as an alternative email address to send to.{% endblocktrans %}</p>
                <p><small>{% blocktrans %}Note: You will never receive notifications if a project has them disabled (via the Project settings){% endblocktrans %}</small></p>

                {% for project, form in project_forms %}
                    {{ form|as_crispy_errors }}
                {% endfor %}
                {% for project, form in project_forms %}
                    {% ifchanged project.team %}
                        {% if not forloop.first %}
                            </table>
                        {% endif %}
                        <table class="table table-bordered table-striped">
                            <caption>{{ project.team.name }}</caption>

                            <thead>
                                <tr>
                                    <th style="width:5px"><input type="checkbox" onclick="$('tbody input[type=checkbox]', $(this).parents('table')).attr('checked', this.checked)"></th>
                                    <th>{% trans "Project Name" %}</th>
                                    <th style="width:100px;overflow:hidden;text-align:right">
                                        {% trans "Email Address" %}</th>
                                </tr>
                            </thead>
                            <tbody>
                    {% endifchanged %}
                    <tr>
                        <td>{{ form.alert }}</td>
                        <td><label for="{{ form.alert.auto_id }}">{{ project.name }}</label></td>
                        <td style="text-align:right">
                            <a href="javascript:void(0)" data-target="{{ form.email.auto_id }}" data-toggle="change-target-value">{% if form.email.value %}{{ form.email.value }}{% else %}<em>default</em>{% endif %}</a>
                            {{ form.email }}
                        </td>
                    </tr>
                {% endfor %}
                </tbody></table>
            </fieldset>
        {% endif %}

        {% for form in ext_forms %}
            <fieldset>
                <div><legend>{{ form.get_title }}</legend></div>
                {% with form.get_description as description %}
                    {% if description %}
                        {{ description|linebreaks }}
                    {% endif %}
                {% endwith %}
                {% include "sentry/partial/form_base.html" %}
            </fieldset>
        {% endfor %}

        <fieldset class="form-actions">
            <button type="submit" class="btn btn-primary">{% trans "Save Changes" %}</button>
        </fieldset>
    </form>
{% endblock %}
