[% WRAPPER layout.tt title=(create ? "Add 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="selected"
    [% END %]
    >[% role %]</option>
[% END %]

<form class="form-horizontal">

  <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) %]/>
      </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 => create ? '' : user.fullname) %]/>
      </div>
    </div>

    [% IF create || user.type == 'hydra' %]
    <div class="control-group">
      <label class="control-label">Password</label>
      <div class="controls">
        <input type="password" class="span3" name="password" value=""/>
      </div>
    </div>

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

    <div class="control-group">
      <label class="control-label">Email</label>
      <div class="controls">
        <input type="text" class="span3" name="emailaddress" [% IF !create && user.username.search('@') %]disabled="disabled"[% END %] [%+ HTML.attributes(value => user.emailaddress) %]/>
      </div>
    </div>

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

    <div class="control-group">
      <div class="controls">
        <label class="checkbox">
          <input type="checkbox" name="publicdashboard" [% IF !create && user.publicdashboard; 'checked="checked"'; END %]/>Public dashboard
        </label>
      </div>
    </div>

    [% IF !create || c.check_user_roles('admin') %]
      <div class="control-group">
        <label class="control-label">Roles</label>
        <div class="controls">
          <select multiple="multiple" name="roles" class="span3" [% IF !c.check_user_roles('admin') %]disabled="disabled"[% END %]>
            [% INCLUDE roleoption role="admin" %]
            [% INCLUDE roleoption role="create-projects" %]
            [% INCLUDE roleoption role="restart-jobs" %]
            [% INCLUDE roleoption role="bump-to-front" %]
            [% INCLUDE roleoption role="cancel-build" %]
          </select>
        </div>
      </div>
    [% END %]

    [% IF create && !c.check_user_roles('admin') %]
      <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=""/>
        </div>
      </div>
    [% END %]

    <div class="form-actions">
      <button id="submit-user" 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') && user.type == 'hydra' %]
        <button id="reset-password" class="btn btn-warning">
          <i class="icon-trash icon-white"></i>
          Reset password
        </button>
      [% END %]
      [% IF !create %]
        <button id="delete-user" class="btn btn-danger">
          <i class="icon-trash icon-white"></i>
          Delete this user
        </button>
      [% END %]
    </div>

  </fieldset>

</form>

<script>
  $("#submit-user").click(function() {
    requestJSON({
      [% IF create %]
        url: "[% c.uri_for(c.controller('User').action_for('register')) %]",
      [% ELSE %]
        url: "[% c.uri_for(c.controller('User').action_for('edit'), c.req.captures) %]",
      [% END %]
      data: $(this).parents("form").serialize(),
      type: 'PUT',
      success: backToReferrer
    });
    return false;
  });

  $("#reset-password").click(function() {
    bootbox.confirm(
      'Are you sure you want to reset the password for this user?',
      function(c) {
        if (!c) return;
        requestJSON({
          url: "[% c.uri_for(c.controller('User').action_for('reset_password'), [user.username]) %]",
          type: 'POST',
          success: backToReferrer
        });
      });
    return false;
  });

  $("#delete-user").click(function() {
    bootbox.confirm(
      'Are you sure you want to delete this user?',
      function(c) {
        if (!c) return;
        requestJSON({
          url: "[% c.uri_for(c.controller('User').action_for('edit'), c.req.captures) %]",
          type: 'DELETE',
          success: backToReferrer
        });
      });
    return false;
  });
</script>

[% END %]
