<!--User List-->
<?xjs

	var pageSize = 500;

	// Uncomment/comment properties below to enable/disable list columns
	// Rearrange to change order of appearance
	var columns = {
		// number : { name : "#", type : "number" },
		alias : { name : 'Alias', type : 'string' },
		// name : { name : "Name", type : "string" },
		// age : { name : "Age", type : "number" },
		// gender : { name : "Sex", type : "string" },
		location : { name : 'Location', type : 'string' },
		laston_date : { name : 'Last On', type : 'date' },
		// connection : { name : "Via", type : "string" },
		// firston_date : { name : "First On", type : "date" },
		// total_logons : { name : "Calls", type : "number" },
		// total_posts : { name : "Posts", type : "number" }
	};

	// Most people won't need to edit below this line
	load('sbbsdefs.js');

	if (typeof http_request.query.offset === 'undefined' ||
		isNaN(parseInt(http_request.query.offset[0])) ||
		parseInt(http_request.query.offset[0]) < 1
	) {
		var offset = 0;
	} else {
		var offset = parseInt(http_request.query.offset[0]);
	}

	var url = format(
		'http://%s%s',
		http_request.host,
		http_request.request_string.split("&")[0]
	);

	function sortUser(a, b, sortOrder, type) {
		if (type === 'string') {
			a = a.toUpperCase();
			b = b.toUpperCase();
			ret = (
				a < b
				? (sortOrder === 'ascending' ? 1 : -1)
				: (	a > b
					? (sortOrder === 'ascending' ? -1 : 1)
					: 0
				)
			);
		} else if (type === 'number' || type === 'date') {
			ret = (
				a < b
				? (sortOrder === 'ascending' ? -1 : 1)
				: ( a > b
					? (sortOrder === 'ascending' ? 1 : -1)
					: 0
				)
			);
		}
		return ret;
	}

	function sortUsers(a, b) {
		var ret = 0;
		if (typeof http_request.query.sortby !== 'undefined' &&
			typeof http_request.query.sortorder !== 'undefined'	&&
			(	http_request.query.sortorder[0] === 'ascending' ||
				http_request.query.sortorder[0] === 'descending'
			)
		) {
			var sortBy = http_request.query.sortby[0].toLowerCase();
			var sortOrder = http_request.query.sortorder[0].toLowerCase();
			for (var c in columns) {
				if (sortBy != c) continue;
				ret = sortUser(a[c], b[c], sortOrder, columns[c].type);
				break;
			}
		}
		return ret;
	}

	function makeSortURLs(field, order) {
		return format(
			'<a class="icon" ' +
			'href="./?page=%s&sortby=%s&sortorder=ascending&offset=%s">' +
			'<span class="glyphicon glyphicon-arrow-up"></span>' +
			'</a>' +
			'<a class="icon" ' +
			'href="./?page=%s&sortby=%s&sortorder=descending&offset=%s">' +
			'<span class="glyphicon glyphicon-arrow-down"></span>' +
			'</a>',
			http_request.query.page[0],
			field,
			offset,
			http_request.query.page[0],
			field,
			offset
		);
	}

	function makePagerURLs() {
		var ret = { previous : '', next : '' };
		if (offset > 1) {
			ret.previous = format(
				'<li><a href="./?page=%s&offset=%s">Previous</a></li>',
				http_request.query.page[0],
				Math.max(0, offset - pageSize)
			);
		}
		if (system.lastuser - offset > pageSize) {
			ret.next = format(
				'<li><a href="./?page=%s&offset=%s">Next</a></li>',
				http_request.query.page[0],
				Math.min(
					offset + pageSize, offset + (system.lastuser - offset)
				)
			);
		}
		return ret;
	}

	function copyProperties(source, dest) {
		for (var property in source) {
			if ((	typeof source[property] === 'string' ||
					typeof source[property] === 'number'
				) && typeof columns[property] !== 'undefined'
			) {
				dest[property] = source[property];
			} else if (property === 'stats') {
				for (var stat in source.stats) {
					if (typeof columns[stat] === 'undefined') continue;
					dest[stat] = source.stats[stat];
				}
			}
		}
		return dest;
	}

	function loadUsers(offset, pageSize) {
		var users = [];
    var usr = new User(1);
		for (var u = 1; u < system.lastuser; u++) {
			usr.number = u;
			if (usr.settings&USER_DELETED ||
				usr.compare_ars('REST Q') ||
				usr.alias === 'Guest' ||
				usr.alias === settings.guest
			) {
				continue;
			}
			users.push(copyProperties(usr, {}));
		}
    usr = undefined;
		users.sort(sortUsers);
		return users.slice(offset, offset + pageSize);
	}

	function writeColumns() {
		for (var c in columns) {
			writeln('<th>' + columns[c].name + ' ' + makeSortURLs(c) + '</th>');
		}
	}

	function writeRows(users) {
		for (var u = 0; u < users.length; u++) {
			writeln('<tr>');
			for (var c in columns) {
				writeln(
					'<td>' +
					(	columns[c].type === 'date'
						? system.timestr(users[u][c])
						: users[u][c].toString()
					) +	'</td>'
				);
			}
			writeln('</tr>');
		}
	}

	var pager = makePagerURLs();

?>

<div class="well well-sm"><h3>User List</h3></div>
<table class="table table-striped">
	<thead>
		<tr>
			<?xjs writeColumns(); ?>
		</tr>
	</thead>
	<tbody>
		<?xjs writeRows(loadUsers(offset, pageSize)); ?>
	</tbody>
</table>

<nav>
	<ul class="pager">
		<?xjs writeln(pager.previous); writeln(pager.next); ?>
	</ul>
</nav>
