[% WRAPPER layout.tt title=(create ? "Register new user" : "Editing user $user.username") %]
[% PROCESS common.tt %]

[% BLOCK roleoption %]
  <option value="[% role %]"
    [% checked = false %]
    [% FOREACH r IN user.userroles %]
      [% checked = r.role == role %]
      [% BREAK IF checked %]
    [% END %]
    [% IF checked %]
      SELECTED
    [% END %]
    >[% role %]</option>
[% END %]

<form class="form-horizontal" method="post">

  <fieldset>

    [% IF create %]
    <div class="control-group">
      <label class="control-label">User name</label>
      <div class="controls">
        <input type="text" class="span3" name="username" [% HTML.attributes(value => username) %]></input>
      </div>
    </div>
    [% END %]

    <div class="control-group">
      <label class="control-label">Full name</label>
      <div class="controls">
        <input type="text" class="span3" name="fullname" [% HTML.attributes(value => fullname) %]></input>
      </div>
    </div>

    <div class="control-group">
      <label class="control-label">Password</label>
      <div class="controls">
        <input type="password" class="span3" name="password" value=""></input>
      </div>
    </div>

    <div class="control-group">
      <label class="control-label">Confirm password</label>
      <div class="controls">
        <input type="password" class="span3" name="password2" value=""></input>
      </div>
    </div>

    <!--
    <div class="control-group">
      <label class="control-label">Email</label>
      <div class="controls">
        <input type="text" class="span3" name="emailaddress" [% HTML.attributes(value => user.emailaddress) %]></input>
      </div>
    </div>
    -->

    [% IF !create %]
      <div class="control-group">
        <div class="controls">
          <label class="checkbox">
            <input type="checkbox" name="emailonerror" [% IF emailonerror; 'checked="checked"'; END %]></input>Receive evaluation error notifications
          </label>
        </div>
      </div>
    [% END %]

    [% IF !create && c.check_user_roles('admin') %]
      <div class="control-group">
        <label class="control-label">Roles</label>
        <div class="controls">
          <select multiple name="roles" class="span3">
            [% INCLUDE roleoption role="admin" %]
            [% INCLUDE roleoption role="create-project" %]
          </select>
        </div>
      </div>
    [% END %]

    [% IF create %]
      <div class="control-group">
        <div class="controls">
          <img src="[% c.uri_for('/captcha') %]" alt="CAPTCHA"/>
        </div>
      </div>

      <div class="control-group">
        <label class="control-label">Type the digits shown in the image above</label>
        <div class="controls">
          <input type="text" class="span3" name="captcha" value=""></input>
        </div>
      </div>
    [% END %]

    <div class="form-actions">
      <button type="submit" class="btn btn-primary">
        <i class="icon-ok icon-white"></i>
        [%IF create %]Create[% ELSE %]Apply changes[% END %]
      </button>
      [% IF !create && c.check_user_roles('admin') %]
        <button id="reset-password" type="submit" class="btn btn-warning" name="submit" value="reset-password">
          <i class="icon-trash icon-white"></i>
          Reset password
        </button>
        <script type="text/javascript">
          $("#reset-password").click(function() {
            bootbox.confirm(
              'Are you sure you want to reset the password for this user?',
              function(c) {
                if (!c) return;
                $.post("[% c.uri_for(c.controller('User').action_for('edit'), [user.username]) %]", { submit: 'reset-password' })
                  .done(function(data) {
                    if (data.error)
                      bootbox.alert("Unable to reset password: " + data.error);
                    else
                      bootbox.alert("An email containing the new password has been sent to the user.");
                  })
                  .fail(function() { bootbox.alert("Server request failed!"); });
              });
            return false;
          });
        </script>
      [% END %]
      [% IF !create %]
        <button id="delete-user" type="submit" class="btn btn-danger" name="submit" value="delete">
          <i class="icon-trash icon-white"></i>
          Delete this user
        </button>
        <script type="text/javascript">
          $("#delete-user").click(function() {
            return confirm("Are you sure you want to delete this user?");
          });
        </script>
      [% END %]
  </div>
</p>

</form>

[% END %]
