[%- SET has_ldap   = (settings.ldap   AND settings.ldap.size   > 0) -%]
[%- SET has_radius = (settings.radius AND settings.radius.size > 0) -%]
[%- SET has_tacacs = (settings.tacacs AND settings.tacacs.size > 0) -%]
[%- SET demo_site  = (settings.defanged_admin != 'admin') -%]

[% IF settings.allow_permanent_tokens %]
<div class="alert alert-info">
  Configured Token Lifetime: [% settings.api_token_lifetime | html_entity %]s
</div>
[% END %]

<table id="data-table" class="table table-striped table-bordered" width="100%" cellspacing="0">
  <thead>
    <tr>
      <th class="nd_center-cell">Full Name</th>
      <th class="nd_center-cell">Username</th>
      <th class="nd_center-cell">Password</th>
      <th class="nd_center-cell">Auth Method</th>
      <th class="nd_center-cell">Allowed IPs</th>
      <th class="nd_center-cell">Port Control</th>
      <th class="nd_center-cell">Administrator</th>
      <th class="nd_center-cell">Created</th>
      <th class="nd_center-cell">Last Login</th>
      <th class="nd_center-cell">Note</th>
      <th class="nd_center-cell">Action</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td class="nd_center-cell"><input data-form="add" name="fullname" type="text"></td>
      <td class="nd_center-middle-cell"><input class="span2" data-form="add" name="username" type="text"></td>
      [% IF settings.no_auth %]
      <td class="nd_center-cell muted">Bypassed</td>
      [% ELSE %]
      <td class="nd_center-cell"><input class="span1 nd_pw_field" data-form="add" name="password" type="password"></td>
      [% END %]
      <td class="nd_center-cell">
        <div class="form-group">
          <select class="span2 form-control nd_auth_method" data-form="add" name="auth_method">
            [% IF demo_site OR NOT settings.no_auth %]<option value="" selected>Netdisco Password</option>[% END %]
            [% IF demo_site OR has_ldap %]<option value="ldap">LDAP</option>[% END %]
            [% IF demo_site OR has_radius %]<option value="radius">RADIUS</option>[% END %]
            [% IF demo_site OR has_tacacs %]<option value="tacacs">TACACS</option>[% END %]
            [% IF demo_site OR settings.allow_permanent_tokens %]<option value="permanent_token">Permanent Token</option>[% END %]
          </select>
        </div>
      </td>
      <td class="nd_center-cell">
        <input class="span2 nd_allowed_ips_field" data-form="add" name="token_allowed_ips" type="text" placeholder="192.0.2.0/28, 2001:db8::/64" disabled>
      </td>
      <td class="nd_center-cell">
        <div class="form-group">
          <select class="span2 form-control" data-form="add" name="port_control">
            <option value="" selected>Off</option>
            <option value="_global_">Enabled (any port)</option>
            [% FOREACH role IN port_control_roles %]
            <option value="[% role | html_entity %]">Role: [% role | html_entity %]</option>
            [% END %]
          </select>
        </div>
      </td>
      <td class="nd_center-cell"><input data-form="add" type="checkbox" name="admin"></td>
      <td class="nd_center-cell"></td>
      <td class="nd_center-cell"></td>
      <td class="nd_center-cell"><input class="span2" data-form="add" name="note" type="text"></td>
      <td nowrap class="nd_center-cell">
        <button class="btn btn-small nd_adminbutton" name="add" type="submit"><i class="icon-plus-sign"></i> Add</button>
      </td>
    </tr>

    [% SET count = 0 %]
    [% FOREACH row IN results %]
    [% SET count = count + 1 %]
    <tr>
      <td class="nd_center-cell">
        <input data-form="update" name="fullname" type="text" value="[% row.fullname | html_entity %]">
      </td>
      <td class="nd_center-cell">
        <input data-form="update" name="username" type="hidden" value="[% row.username | html_entity %]">
        [% row.username | html_entity %]
      </td>
      [% IF settings.no_auth %]
      <td class="nd_center-cell muted">Bypassed</td>
      [% ELSE %]
      <td class="nd_center-middle-cell">
        <input class="span1 nd_pw_field" data-form="update" name="password" type="password"
          [% IF row.token_auth_only %]
          style="display: none" disabled
          [% ELSE %]
          value="********"
          [% END %]
          >
        <small class="nd_token_hint muted"
          [% IF NOT row.token_auth_only %]
          style="display: none"
          [% END %]
          >
          [% IF row.token_hint %]
          <span class="nd_token-hint-value">...[% row.token_hint | html_entity %]</span>
          [% ELSE %]
          <span class="nd_token-hint-value">no token</span>
          [% END %]
        </small>
        [% IF row.token_auth_only %]
        <br />
        <button class="btn btn-small nd_tokenbutton" data-username="[% row.username | html_entity %]" type="button" title="Generate new API token">
          <i class="icon-key"></i> <i class="icon-refresh"></i>
        </button>
        [% END %]
      </td>
      [% END %]
      <td class="nd_center-cell">
        <div class="form-group">
          <select class="span2 form-control nd_auth_method" data-form="update" name="auth_method">
            [% IF demo_site OR NOT settings.no_auth %]<option[% ' selected' IF NOT row.token_auth_only AND NOT row.ldap AND NOT row.radius AND NOT row.tacacs %] value="">Netdisco Password</option>[% END %]
            [% IF demo_site OR has_ldap OR row.ldap %]<option[% ' selected' IF row.ldap %] value="ldap">LDAP</option>[% END %]
            [% IF demo_site OR has_radius OR row.radius %]<option[% ' selected' IF row.radius %] value="radius">RADIUS</option>[% END %]
            [% IF demo_site OR has_tacacs OR row.tacacs %]<option[% ' selected' IF row.tacacs %] value="tacacs">TACACS</option>[% END %]
            <option[% ' selected' IF row.token_auth_only %] value="permanent_token">Permanent Token</option>
          </select>
        </div>
      </td>
      <td class="nd_center-cell">
        <input class="span2 nd_allowed_ips_field" data-form="update" name="token_allowed_ips" type="text"
          value="[% row.token_allowed_ips.join(', ') IF row.token_allowed_ips %]"
          [% IF NOT row.token_auth_only %]disabled[% END %] placeholder="192.0.2.0/28, 2001:db8::/64">
      </td>
      <td class="nd_center-cell">
        <div class="form-group">
          <select class="span2 form-control" data-form="update" name="port_control">
            <option[% ' selected' IF NOT row.port_control %] value="">Off</option>
            <option[% ' selected' IF row.port_control AND row.portctl_role == "" %] value="_global_">Enabled (any port)</option>
            [% FOREACH role IN port_control_roles %]
            <option[% ' selected' IF row.port_control AND row.portctl_role == role %] value="[% role | html_entity %]">Role: [% role | html_entity %]</option>
            [% END %]
          </select>
        </div>
      </td>
      <td class="nd_center-cell">
        <input data-form="update" name="admin" type="checkbox" [% ' checked="checked"' IF row.admin %]>
      </td>
      <td class="nd_center-cell">[% row.created   | html_entity %]</td>
      <td class="nd_center-cell">[% row.last_seen | html_entity %]</td>
      <td class="nd_center-cell">
        <input class="span2" data-form="update" name="note" type="text" value="[% row.note | html_entity %]">
      </td>

      <td nowrap class="nd_center-cell">
        <button class="btn nd_adminbutton" name="update" type="submit"><i class="icon-save text-warning"></i></button>

        <button class="btn" data-toggle="modal"
          data-target="#nd_devdel-[% count | html_entity %]" type="button"><i class="icon-trash text-error"></i></button>

        <div id="nd_devdel-[% count | html_entity %]" class="nd_modal nd_deep-horizon modal hide fade" tabindex="-1"
          role="dialog" aria-labelledby="nd_devdel-label-[% count | html_entity %]" aria-hidden="true">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">x</button>
            <h3 id="nd_devdel-label-[% count | html_entity %]">Are you sure?</h3>
          </div>
          <div class="modal-body">
            <blockquote>
              <p class="text-info">User &quot;[% row.username | html_entity %]&quot; will be deleted.</p>
            </blockquote>
            <input data-form="del" name="username" type="hidden" value="[% row.username | html_entity %]">
          </div>
          <div class="modal-footer">
            <button class="btn btn-success" data-dismiss="modal" aria-hidden="true">Cancel</button>
            <button class="btn btn-danger nd_adminbutton" name="del" data-dismiss="modal">Confirm</button>
          </div>
        </div>
      </td>
    </tr>
    [% END %]
  </tbody>
</table>

<div id="nd_token-reveal" class="nd_modal nd_deep-horizon modal hide fade" tabindex="-1" role="dialog">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal">x</button>
    <h3>API Token</h3>
  </div>
  <div class="modal-body">
    <p class="text-info">Copy this token — it will not be shown again.</p>
    <div class="input-append">
      <input id="nd_token-value" type="text" class="span4" readonly>
      <button class="btn" id="nd_token-copy" type="button"><i class="icon-copy"></i> Copy</button>
    </div>
  </div>
  <div class="modal-footer">
    <button class="btn btn-success" data-dismiss="modal">Done</button>
  </div>
</div>

<script>
$(document).ready(function() {
  function toggleTokenFields(select) {
    var row  = $(select).closest('tr');
    var pw   = row.find('.nd_pw_field');
    var cell = pw.closest('td');
    var hint = row.find('.nd_token_hint');
    var ips  = row.find('.nd_allowed_ips_field');
    var btn  = row.find('.nd_tokenbutton');
    if ($(select).val() === 'permanent_token') {
      pw.hide().val('').prop('disabled', true);
      cell.addClass('nd_center-middle-cell');
      cell.removeClass('nd_center-cell');
      hint.show();
      btn.show();
      ips.prop('disabled', false);
    } else {
      pw.show().prop('disabled', false).attr('placeholder', '');
      cell.addClass('nd_center-cell');
      cell.removeClass('nd_center-middle-cell');
      hint.hide();
      btn.hide();
      ips.val('').prop('disabled', true);
    }
  }
  $('.nd_auth_method').each(function() { toggleTokenFields(this); });
  $(document).on('change', '.nd_auth_method', function() { toggleTokenFields(this); });

  $('#nd_token-reveal').modal({show: false});
  $('#nd_token-copy').on('click', function() {
    navigator.clipboard.writeText($('#nd_token-value').val());
    $(this).html('<i class="icon-ok"></i> Copied');
  });

  // Called by admintask.js when the server returns a data-nd-api-key span
  window.nd_show_api_token = function(apiKey) {
    $('#nd_token-value').val(apiKey);
    $('#nd_token-copy').html('<i class="icon-copy"></i> Copy');
    $('#nd_token-reveal').one('hidden', function() {
      var tab = '[% task.tag | html_entity %]';
      $('#' + tab + '_form').trigger('submit');
    });
    $('#nd_token-reveal').modal('show');
  };

  $(document).on('click', '.nd_tokenbutton', function() {
    var username  = $(this).data('username');
    var hint = $(this).closest('td').find('.nd_token-hint-value');
    $.get(uri_base + '/ajax/control/admin/users/token', { username: username, permanent: 1 }, function(apiKey) {
      apiKey = $.trim(apiKey);
      if (apiKey && typeof window.nd_show_api_token === 'function') {
        $(hint).text('...' + apiKey.slice(-8));
        window.nd_show_api_token(apiKey);
      } else {
        toastr.error('Could not retrieve token');
      }
    }, 'text').fail(function() {
      toastr.error('Could not retrieve token');
    });
  });

  $('#data-table').dataTable({
    "columnDefs": [
      {
        "targets": [ 2, 3, 4, 5, 6, 7, 8, 9, 10 ],
        "searchable": false
      },
      {
        "targets": [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ],
        "orderable": false
      }
    ],
[% INCLUDE 'ajax/datatabledefaults.tt' -%]
  } );
} );
</script>