var DashboardUrl = "/live/stats"; // Adjust this URL if needed
var GameEditUpdateInterval = 3000;

function GameEdit() {
  var base = this;
  this.timer = null;

  this.onInit = function() {
    base.timer = setInterval(base.updateStatus, GameEditUpdateInterval);
    this.updateStatus();
  };

  this.onDeinit = function() {
    if (base.timer != null) clearInterval(base.timer);
  };

  // Function to fetch and update player stats from the server
  this.updateStatus = function() {
    $.ajax({
      url: '/fetch_data.php', // PHP file to fetch data from the SQLite database
      type: 'GET',
      dataType: 'json',
      success: function(res) {
        // Update player data
        if (res.players) {
          $("#playersTable tbody").empty();
          res.players.forEach(function(player) {
            var row = $("<tr></tr>");
            row.append(`<td>${player.id}</td>`);
            row.append(`<td>${player.name}</td>`);
            row.append(`<td>${player.score}</td>`);
            $("#playersTable tbody").append(row);
          });
        }

        // Update stats data
        if (res.stats) {
          $("#statsTable tbody").empty();
          res.stats.forEach(function(stat) {
            var row = $("<tr></tr>");
            row.append(`<td>${stat.id}</td>`);
            row.append(`<td>${stat.player_id}</td>`);
            row.append(`<td>${stat.kills}</td>`);
            row.append(`<td>${stat.deaths}</td>`);
            $("#statsTable tbody").append(row);
          });
        }
      },
      error: function() {
        alert("Failed to fetch data!");
      }
    });
  };
}

// Register the GameEdit page
mainFrame.setActivePage(new GameEdit());
