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

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

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

{% block inner %}
  <div class="page-header">
    <div class="pull-right">
      <a href="{% url 'sentry-organization-members' organization.slug %}">Back to Organization Members</a>
    </div>
    <h2>
      Member Settings
      <small>{{ member.get_display_name }}</small>
    </h2>
  </div>

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

    {% if form.errors %}
      <div class="alert alert-error">{% trans "Please correct the errors below." %}</div>
    {% endif %}

    {{ form|as_crispy_errors }}

    <fieldset>
      <div class="control-group">
        <label>{% trans "Email" %}</label>
        <div class="controls">
          <a href="mailto:{{ member.get_email }}">{{ member.get_email }}</a>
        </div>
      </div>

      <div class="control-group">
        <label>{% trans "Status" %}</label>
        <div class="controls">
          {% if member.is_pending %}
            Invited
          {% else %}
            Active
          {% endif %}
        </div>
      </div>

      {{ form.type|as_crispy_field }}
    </fieldset>

    <fieldset>
      <h2>{% trans "Teams" %}</h2>

      {{ form.has_global_access|as_crispy_field }}

      <fieldset class="team-choices">
        {% for team_id, team_name in form.teams.field.choices %}
          <div class="control-group">
            <div class="controls">
              <label class="checkbox">
                <input type="checkbox" name="teams" value="{{ team_id }}"
                       class="checkboxinput"{% if team_id in form.teams.value %} checked="checked"{% endif %}>
                  {{ team_name }}
                </label>
            </div>
          </div>
        {% endfor %}
      </fieldset>
    </fieldset>

    <div class="form-actions">
      <button type="submit" name="op" value="reinvite" class="btn btn-default">{% trans "Resend Invite" %}</button>
      <button type="submit" class="btn btn-primary">{% trans "Save Changes" %}</button>
    </div>
  </form>
{% endblock %}

{% block content_after %}
  <script>
  $(function(){
    var selector = $('.team-choices input[type=checkbox]');
    var selectedTeams = $('.team-choices input[type=checkbox]:checked').slice(0);
    $('#id_has_global_access').change(function(){
      var $this = $(this);
      var checked = $this.is(':checked');

      if (checked) {
        selectedTeams = $('.team-choices input[type=checkbox]:checked').slice(0);
        selector.prop('checked', true);
      } else {
        selector.prop('checked', false);
        $(selectedTeams).prop('checked', true);
        selectedTeams = [];
      }

      $('.team-choices').prop('disabled', checked);
    }).change();
  });
  </script>
{% endblock %}
