extends layout

block content
  - var help = { user: "The id used for login, can not be changed once created"}
  - help.name = "Friendly name for user";
  - help.forced = "A moloch expression that is silently added to all queries, useful to limit what data a user can access (ex what nodes or ips)";
  - help.enabled = "Is the account currently enabled for anything";
  - help.admin = "Can create new accounts and change the settings for other accounts";
  - help.web = "Can access the web interface, when off only APIs can be used";
  - help.webauth = "Can login using the web auth header, setting doesn't disable the password so it should be scrambled";
  - help.email = "Can perform email searches";
  - help.remove = "Can delete tags or delete/scrub pcap data";
  h2 System Users
  table#users(cellpadding="0",cellspacing="0",border="0",class="display",style="table { clear: both }")
    thead
      tr
        th(title=help.user) User Id
        th(title=help.name) Name
        th(title=help.forced) Forced Expression
        th(title=help.enabled) Enabled?
        th(title=help.admin) Admin?
        th(title=help.web) Web Interface?
        th(title=help.webauth) Web Auth Header?
        th(title=help.email) Email Search?
        th(title=help.remove) Can Remove Data?
        th Actions
    tbody
  br
  #users
  hr
  <h2> Add User</h2>
  form#addUser
    input(type='hidden', id='token', name='token', value='#{token}')
    label(for='userId',title=help.user) User ID
    input(type='text', id='userId', name='userId', size='30')
    br
    label(for='userName',title=help.name) User Name
    input(type='text', id='userName', name='userName', size='30')
    br
    label(for='Password') Password
    input(type='password', id='password', name='password', size='30')
    br
    label(for='Expression',title=help.forced) Forced Search Expression
    input(type='expression', id='expression', name='expression', size='60')
    br
    label(for='enabled',title=help.enabled) Enabled:
    input(type='checkbox', id='enabled', name='enabled', CHECKED)
    br
    label(for='createEnabled',title=help.admin) Admin User:
    input(type='checkbox', id='createEnabled', name='createEnabled')
    br
    label(for='webEnabled',title=help.web) Web Enabled User:
    input(type='checkbox', id='webEnabled', name='webEnabled', CHECKED)
    br
    label(for='headerAuthEnabled',title=help.webauth) Web Auth Header:
    input(type='checkbox', id='headerAuthEnabled', name='headerAuthEnabled')
    br
    label(for='emailSearch',title=help.email) Allow Email Searches:
    input(type='checkbox', id='emailSearch', name='emailSearch')
    br
    label(for='removeEnabled',title=help.remove) Can Scrub/Delete data:
    input(type='checkbox', id='removeEnabled', name='removeEnabled')
    br
    button#create create

  script(type='text/javascript').
    $("th[title]").qtip();
    $("label[title]").qtip();
    var usersTable;

    $(document).ready(function() {
      usersTable = $('#users').dataTable( {
        "oLanguage": {
          "sProcessing": "<img src=\"watching.gif\"><div class='blink'>I'm Hootin' :D</div>",
          "sLengthMenu": "_MENU_"
        },
        "sDom": '<"H"lipr>t<"F"ip>',
        "iDisplayLength": 500,
        "aLengthMenu": [100, 500, 1000, 5000],
        "bProcessing": true,
        "bServerSide": true,
        "sAjaxSource": "#{basePath}users.json",
        "fnServerData": function ( sSource, aoData, fnCallback ) {
          $.ajax( {
           "dataType": 'json',
            "type": "POST",
            "url": sSource,
            "data": aoData,
            "success": fnCallback
          } );
        },
        "bRetrieve": true,
        "bFilter": false,
        "bScrollCollapse": true,
        "bAutoWidth": false,
        "aoColumns": [
          { mDataProp : "userId"
          },
          { mDataProp : "userName",
            sClass: "userName",
            fnRender: function(oObj) {
              return "<div contentEditable>" + oObj.aData.userName + "</div>";
            }
          },
          { mDataProp : "expression",
            sClass: "forcedExpression",
            fnRender: function(oObj) {
              return "<div contentEditable>" + oObj.aData.expression + "</div>";
            }
          },
          { mDataProp : "enabled",
            sClass: "enabled",
            fnRender : function(oObj) {
              return "<input type='checkbox' " + (oObj.aData.enabled?"CHECKED":"") + ">";
            }
          },
          { mDataProp : "createEnabled",
            sClass: "createEnabled",
            fnRender : function(oObj) {
              return "<input type='checkbox' " + (oObj.aData.createEnabled?"CHECKED":"") + ">";
            }
          },
          { mDataProp : "webEnabled",
            sClass: "webEnabled",
            fnRender : function(oObj) {
              return "<input type='checkbox' " + (oObj.aData.webEnabled?"CHECKED":"") + ">";
            }
          },
          { mDataProp : "headerAuthEnabled",
            sClass: "headerAuthEnabled",
            fnRender : function(oObj) {
              return "<input type='checkbox' " + (oObj.aData.headerAuthEnabled?"CHECKED":"") + ">";
            }
          },
          { mDataProp : "emailSearch",
            sClass: "emailSearch",
            fnRender : function(oObj) {
              return "<input type='checkbox' " + (oObj.aData.emailSearch?"CHECKED":"") + ">";
            }
          },
          { mDataProp : "removeEnabled",
            sClass: "removeEnabled",
            fnRender : function(oObj) {
              return "<input type='checkbox' " + (oObj.aData.removeEnabled?"CHECKED":"") + ">";
            }
          },
          { mDataProp : null,
            bSortable : false,
            fnRender : function(oObj) {
              return "<a href='#' class='deleteUserAction'>Delete</a> " + 
                     "<a href='#' class='settingsUserAction'>Settings</a>";
            }
          }
        ]
      } );

      function saveRow(row) {
        var aData = usersTable.fnGetData( row );
        $.ajax( {
          "dataType": 'json',
          "type": "POST",
          "data": [{name: "token", value: "#{token}"}],
          "url": "#{basePath}updateUser/" + aData.userId + "?" +
            "&expression="        + encodeURIComponent($(row).find(".forcedExpression div").text()) +
            "&userName="          + encodeURIComponent($(row).find(".userName div").text()) +
            "&enabled="           + $(row).find(".enabled input").is(':checked') +
            "&webEnabled="        + $(row).find(".webEnabled input").is(':checked') +
            "&headerAuthEnabled=" + $(row).find(".headerAuthEnabled input").is(':checked') +
            "&emailSearch="       + $(row).find(".emailSearch input").is(':checked') +
            "&removeEnabled="     + $(row).find(".removeEnabled input").is(':checked') +
            "&createEnabled="     + $(row).find(".createEnabled input").is(':checked'),
          "success": function(data) {
            if (!data.success) {
              alert(data.text);
            }
          }
        });
        return true;
      }

      $(document).on('click', '.settingsUserAction', function (e) {
        var aData = usersTable.fnGetData( this.parentNode.parentNode );
        window.location = 'settings?userId=' + aData.userId;
      });

      $(document).on('click', '.deleteUserAction', function (e) {
        var aData = usersTable.fnGetData( this.parentNode.parentNode );
        return deleteUser(aData.userId);
      });

      $(document).on('focusout', '.forcedExpression div, .userName div', function () {
        var nTr = this.parentNode.parentNode;
        return saveRow(nTr);
      });

      $(document).on('keypress', '.forcedExpression div, .userName div', function (e) {
        if ((e.keyCode ? e.keyCode : e.which) === 13) {
          e.target.blur();
          return false;
        }
      });

      $(document).on('change', '#users tbody td input', function () {
        var nTr = this.parentNode.parentNode;
        return saveRow(nTr);
      } );

      $('#addUser').submit(function() {
        $.ajax( {
          "dataType": 'json',
          "type": "POST",
          "url": "#{basePath}addUser",
          "data": $("#addUser").serialize(),
          "success": function(data) {
            if (!data.success) {
              alert(data.text);
            } else {
              $("#userId").val("");
              $("#userName").val("");
              $("#password").val("");
            }
            usersTable.fnDraw();
          }
        });
        return false;
      });
    } );
    function deleteUser(user) {
      $.ajax( {
        "dataType": 'json',
        "type": "POST",
        "url": "#{basePath}deleteUser/" + user,
        "data": [{name: "token", value: "#{token}"}],
        "complete": function(data) {
          if (!data.success) {
            alert(data.text);
          }
          usersTable.fnDraw();
        }
      });
      return false;
    }
