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

[% BLOCK roleoption %]
  [% checked = false %]
  [% FOREACH r IN user.userroles %]
    [% checked = r.role == role %]
    [% BREAK IF checked %]
  [% END %]
  <input
    type="checkbox"
    name="roles"
    [% IF checked %]
      checked="checked"
    [% END %]
    [% IF !mutable %]
      disabled="disabled"
    [% END %]
    [% HTML.attributes(id => "role-${role}", value => role) %] />
  <label [% HTML.attributes(for => "role-${role}") %]> [% role %]</label><br />
  [% END %]

<form>

  [% IF create %]
  <div class="form-group row">
    <label class="col-sm-3" for="userusername">User name</label>
    <div class="col-sm-9">
      <input type="text" class="form-control" id="userusername" name="username" [% HTML.attributes(value => username) %]/>
    </div>
  </div>
  [% END %]

  <div class="form-group row">
    <label class="col-sm-3" for="userfullname">Full name</label>
    <div class="col-sm-9">
      <input type="text" class="form-control" id="userfullname" name="fullname" [% HTML.attributes(value => create ? '' : user.fullname) %]/>
    </div>
  </div>

  [% IF create || user.type == 'hydra' %]
  <div class="form-group row">
    <label class="col-sm-3" for="userpassword">Password</label>
    <div class="col-sm-9">
      <input type="password" class="form-control" id="userpassword" name="password" value=""/>
    </div>
  </div>

  <div class="form-group row">
    <label class="col-sm-3" for="userpassword2">Confirm password</label>
    <div class="col-sm-9">
      <input type="password" class="form-control" id="userpassword2" name="password2" value=""/>
    </div>
  </div>
  [% END %]

  <div class="form-group row">
    <label class="col-sm-3" for="useremailaddress">Email</label>
    <div class="col-sm-9">
      <input type="text" class="form-control" id="useremailaddress" name="emailaddress" [% IF !create && user.username.search('@') %]disabled="disabled"[% END %] [%+ HTML.attributes(value => user.emailaddress) %]/>
    </div>
  </div>

  <div class="form-group row">
    <label class="col-sm-3" for="useremailonerror">Receive evaluation error notifications</label>
    <div class="col-sm-9">
      <input type="checkbox" name="emailonerror" [% IF !create && user.emailonerror; 'checked="checked"'; END %]/>
    </div>
  </div>

  <div class="form-group row">
    <label class="col-sm-3" for="userpublicdashboard">Public dashboard</label>
    <div class="col-sm-9">
      <input type="checkbox" name="publicdashboard" [% IF !create && user.publicdashboard; 'checked="checked"'; END %]/>
    </div>
  </div>

  [% IF !create || c.check_user_roles('admin') %]
    [% mutable = c.check_user_roles('admin') %]
    <div class="form-group row card [% mutable ? "border-danger" : "border-light" %]">
      <div class="card-header">User Roles</div>
      <div class="card-body [% mutable ? "text-danger" : "" %]">
        [% IF mutable %]
          <h5 class="card-title">Take care, you're allowed to change [% HTML.escape(user.fullname || user.emailaddress || "this user") %]'s roles.</h5>
        [% ELSE %]
          <h5 class="card-title">[% HTML.escape(user.fullname || user.emailaddress || "this user") %]'s roles.</h5>
        [% END %]
        <p class="card-text">
          [% INCLUDE roleoption mutable=mutable role="admin" %]
          [% INCLUDE roleoption mutable=mutable role="create-projects" %]
          [% INCLUDE roleoption mutable=mutable role="restart-jobs" %]
          [% INCLUDE roleoption mutable=mutable role="bump-to-front" %]
          [% INCLUDE roleoption mutable=mutable role="cancel-build" %]
        </p>
      </div>
    </div>
  [% END %]

  [% IF create && !c.check_user_roles('admin') %]
    <div class="form-group row">
      <label class="col-sm-3"></label>
      <div class="col-sm-9">
        <img src="[% c.uri_for('/captcha') %]" alt="CAPTCHA">
      </div>
    </div>
    <div class="form-group row">
      <label class="col-sm-3" for="usercaptcha">Type the digits shown in the image above</label>
      <div class="col-sm-9">
        <input type="text" class="form-control" id="usercaptcha" name="captcha" value="">
      </div>
    </div>
  [% END %]

  <button id="submit-user" class="btn btn-primary">
    <i class="fas fa-check"></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="fas fa-lock"></i>
      Reset password
    </button>
  [% END %]
  [% IF !create %]
    <button id="delete-user" class="btn btn-danger">
      <i class="fas fa-trash"></i>
      Delete this user
    </button>
  [% END %]
</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 %]