<?xjs var _nll = getLanguage(settings.language_file || 'english.ini', 'sidebar_node_list'); ?>

<div id="sbbs-nodelist"></div>

<script type="text/javascript">

  const anl = <?xjs write(settings.active_node_list); ?>;
  const nch = '<?xjs write(settings.active_node_list ? _nll.label_connection_column : _nll.label_node_column) ?>';
  const nll = <?xjs write(system.node_list.length); ?>;
  var niu = 0;

  function _sb_list_node(e) {
    $('#sbbs-nodelist-table').append(
      '<tr>' +
        '<th scope="row">' +
          (anl ? e.connection : (typeof e.node == 'number' ? e.node + 1 : e.connection)) +
        '</th>' +
        '<td id="nodelist-' + e.node + '">' +
          (e.user == '' ? e.status : ('<strong>' + e.user + '</strong> ' + e.action)) +
        '</td>' +
      '</tr>'
    );
    if (e.user != '' && <?xjs write(user.alias != settings.guest); ?>) {
      $('#nodelist-' + e.node).attr(
        'title', '<?xjs write(_nll.label_send_telegram); ?>'
      );
      $('#nodelist-' + e.node).css('cursor', 'pointer');
      $('#nodelist-' + e.node).click(function () { sendTelegram(e.user); });
    }
    if (typeof e.node == 'number' && e.user != '') niu++;
  }

  function _sb_nodelist() {
  	$.getJSON('./api/system.ssjs?call=node-list', function (data) {
      if (anl && !data.length) {
        $('#sbbs-nodelist').parent().addClass('hidden');
      } else {
        if (anl) $('#sbbs-nodelist').parent().removeClass('hidden');
  			$('#sbbs-nodelist').html(
  				"<h4><?xjs write(_nll.label_title); ?></h4>" +
  				'<table id="sbbs-nodelist-table" class="table table-condensed table-responsive table-striped">' +
    				'<thead><tr>' +
              '<th>' + nch + '</th>' +
              '<th><?xjs write(_nll.label_status_column); ?></th>' +
            '</tr></thead>' +
  				  '<tbody></tbody>' +
  				'</table>'
  			);
        niu = 0;
        if (!anl) {
          const nodes = Array(nll);
          data.forEach(function (e) {
            if (typeof e.node == 'number') {
              nodes[e.node] = e;
            } else {
              nodes.push(e); // Web user
            }
          });
          for (var n = 0; n < nodes.length; n++) {
            if (!nodes[n]) {
              _sb_list_node({ node: n, user: '', status :'Waiting for call' });
            } else {
              _sb_list_node(nodes[n], n);
            }
          }
        } else {
          data.forEach(_sb_list_node);
        }
        // Spans with these classes are used in the 'system stats' sidebar module.
        // Update the nodes in use / available counters there.
        $('#sidebar').find('.sb-nodes-in-use').text(niu);
        $('#sidebar').find('.sb-nodes-available').text(nll - niu);
      }
  	});
  }

  _sb_nodelist();
  setInterval(_sb_nodelist, <?xjs write(settings.refresh_interval || 60000); ?>);

</script>
