<%! from django.utils.translation import ugettext as _ %>
<%inherit file="base.html" />
<%block name="title">${_("Course Team Settings")}</%block>
<%block name="bodyclass">is-signedin course users settings team</%block>


<%block name="content">
<div class="wrapper-mast wrapper">
  <header class="mast has-actions has-subtitle">
    <h1 class="page-header">
      <small class="subtitle">${_("Course Settings")}</small>
      <span class="sr">&gt; </span>${_("Course Team")}
    </h1>

    <nav class="nav-actions">
      <h3 class="sr">${_("Page Actions")}</h3>
      <ul>
        %if allow_actions:
        <li class="nav-item">
          <a href="#" class="button new-button new-user-button"><i class="icon-plus"></i> ${_("New User")}</a>
        </li>
        %endif
      </ul>
    </nav>
  </header>
</div>

<div class="main-wrapper">
  <div class="inner-wrapper">

    <div class="details">
      <p>${_("The following list of users have been designated as course staff. This means that these users will have permissions to modify course content. You may add additional course staff below, if you are the course instructor. Please note that they must have already registered and verified their account.")}</p>
    </div>

    <article class="user-overview">
      %if allow_actions:
      <form class="new-user-form">
        <div id="result"></div>
        <div class="form-elements">
          <label>email: </label><input type="text" id="email" class="email-input" autocomplete="off" placeholder="email@example.com">
          <input type="submit" value="Add User" id="add_user" class="add-button" />
          <input type="button" value="Cancel" class="cancel-button" />
        </div>
      </form>
      %endif
      <div>
        <ol class="user-list">
          % for user in staff:
          <li>
            <span class="user-name">${user.username}</span>
            <span class="user-email">${user.email}</span>
            %if allow_actions :
            <div class="item-actions">
              %if request_user_id != user.id:
              <a href="#" class="delete-button remove-user" data-id="${user.email}"><span class="delete-icon"></span></a>
              %endif
            </div>
            %endif
          </li>
          % endfor
        </ol>
      </div>
    </article>
  </div>
</div>
</%block>

<%block name="jsextra">
<script type="text/javascript">
  var $newUserForm;
  var addUserPostbackUrl = "${add_user_postback_url}";
  var removeUserPostbackUrl = "${remove_user_postback_url}";

  function showNewUserForm(e) {
    e.preventDefault();
    $newUserForm.slideDown(150);
    $newUserForm.find('.email-input').focus();
  }

  function hideNewUserForm(e) {
    e.preventDefault();
    $newUserForm.slideUp(150);
    $('#result').hide();
    $('#email').val('');
  }

  function checkForCancel(e) {
    if(e.which == 27) {
      e.data.$cancelButton.click();
    }
  }

  function addUser(e) {
    e.preventDefault();

    $.ajax({
      url: addUserPostbackUrl,
      type: 'POST',
      dataType: 'json',
      contentType: 'application/json',
      data: JSON.stringify({ 'email': $('#email').val()}),
      success: function(data) {
        location.reload();
      },
      notifyOnError: false,
      error: function(jqXHR, textStatus, errorThrown) {
        data = JSON.parse(jqXHR.responseText);
        $('#result').show().empty().append(data.ErrMsg);
      }
    });
  }

  $(document).ready(function() {
    $newUserForm = $('.new-user-form');
    var $cancelButton = $newUserForm.find('.cancel-button');
    $newUserForm.bind('submit', addUser);
    $cancelButton.bind('click', hideNewUserForm);

    $('.new-user-button').bind('click', showNewUserForm);
    $('body').bind('keyup', { $cancelButton: $cancelButton }, checkForCancel);

    $('.remove-user').click(function() {
      $.ajax({
        url: removeUserPostbackUrl,
        type: 'POST',
        dataType: 'json',
        contentType: 'application/json',
        data:JSON.stringify({ 'email': $(this).data('id')}),
        }).done(function() {
          location.reload();
        })
    });
  });
</script>
</%block>
