<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Recorder</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <link rel="icon" sizes="192x192" href="static/recorder.png" />
    <link rel="apple-touch-icon" href="static/recorder.png" />

    <script src="static/js/jquery-1.12.0.min.js" type="text/javascript"></script>
    <script src="static/js/mustache.js" type="text/javascript"></script>
    <script src="static/js/moment.min.js" type="text/javascript"></script>
    <style>
    body, a, a:visited {
        font-family: Helvetica, sans-serif;
	color: #3f72b5;
    }
    #usertable {
        font-family: Helvetica, sans-serif;
        width: 80%;
        border-collapse: collapse;
    }

    #usertable td, #usertable th {
        font-size: 1em;
        border: 1px solid #305e9f;
        padding: 3px 7px 2px 7px;
    }

    #usertable th {
        font-size: 1.1em;
        text-align: left;
        padding-top: 5px;
        padding-bottom: 4px;
        background-color: #305e9f;
	color: white;
    }
    </style>

    <script type="text/javascript">

    function getuserlist() {
        var dataURI = location.protocol + "//" + location.host;

        var parts = location.pathname.split('/');
        for (var i = 1; i < parts.length - 1; i++) {
                dataURI = dataURI + "/" + parts[i];
        }
        dataURI = dataURI + "/api/0/last" + location.search;

        console.log("dataURI = " + dataURI);

	var now = moment();

	var f12 = now.subtract(12, 'hours').utc().format('YYYY-MM-DDTHH:mm');
	var f7 = now.subtract(7, 'days').utc().format('YYYY-MM-DDTHH:mm');
	var f30 = now.subtract(30, 'days').utc().format('YYYY-MM-DDTHH:mm');

	$.ajax({
		url: dataURI,
		type: "GET",
		dataType: 'json',
		success: function(data) {
			// console.log(JSON.stringify(data));

			for (var i = 0; i < data.length; i++) {
				var d = data[i];
				if (!d['_type']) {
					console.log(d);
					continue;
				}
				d['udev'] = "user=" + d['username'] + "&device=" + d['device'];
				d['f12'] = f12;
				d['f7'] = f7;
				d['f30'] = f30;

	var line = "<tr><td>{{ username }} / {{ device }}</td>" +
	"<td><a href='map/index.html?{{udev}}&format=geojson&from={{f12}}'>12h</a></td>" +
	"<td><a href='map/index.html?{{udev}}&format=geojson&from={{f7}}'>7d</a></td>" +

	"<td><a href='map/index.html?{{udev}}&format=linestring&from={{f12}}'>12h</a></td>" +
	"<td><a href='map/index.html?{{udev}}&format=linestring&from={{f7}}'>7d</a></td>" +
	"<td><a href='map/index.html?{{udev}}&format=linestring&from={{f30}}'>30d</a></td>" +
	"</tr>";

				var row = Mustache.render(line, d);

				$('#usertable > tbody:last-child').append(row);
			}
		},
		error: function(jqXHR, textstatus, errorthrown) {
			console.log(textstatus, errorthrown);
			console.log(jqXHR.status + " " + jqXHR.responseText);
		},
	});
    }

    $(document).ready(function() {
	getuserlist();

    });

    </script>
  </head>
  <body>
<div>
  <ul>
  <li><a href="last/index.html">Live MAP of current locations</a></li>
  <li><a href="table/index.html">TABLE of current locations</a></li>
  </ul>
</div>

    <div id='content'>
    <table id='usertable'>
      <thead>
        <tr><th>User/Device</th><th>Points</th><th>Points</th><th>Track</th><th>Track</th><th>Track</th></tr>
      </thead>
      <tbody>
      </tbody>
    </table>
    </div>

  </body>
</html>
