<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once('inc/config.php');
$frame = 0; if(isset($_GET["frame"])) $frame = $_GET["frame"];
?>

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title><?= $main_name ?></title>
    <link rel="stylesheet" href="css/main.css" />
  </head>
  <body>
    <div id="hd">
      <img src="css/img/site/statistics.png" style="float: left; margin-right: 10px; width:30px;">
      <h2><a href="<?= $main_url ?>"><?= $main_name ?></a>
      <a href="<?= $main_site ?>"><button class="button">Back to site</button></a>
    </div>
    <?php
    if (!$frame) {
        echo '
    <table>
      <tr id=a>
        <th>Status</th>
        <th>Name</th>
        <th>IP</th>
        <th>Players</th>
        <th>Map</th>
      </tr>';

        require_once('lgsl/lgsl_files/lgsl_class.php');
        foreach ($servers as $server) {
            if (isset($server['ip_port'])) {
                $ip_port_parts = explode(':', $server['ip_port']);
                $server_ip = $ip_port_parts[0];
                $server_port = $ip_port_parts[1];

                $result = lgsl_query_live('halflife', $server_ip, $server_port, $server_port, 0, "sep");
                if ($result['b']['status']) {
                    $players = $result['s']['players'] . '/' . $result['s']['playersmax'];
                    $map_image = '<img class="map-image" src="https://image.gametracker.com/images/maps/160x120/cs/' . strtolower($result['s']['map']) . '.jpg" alt="' . $result['s']['map'] . '" />';
                    $server_name = $result['s']['name'];
                    echo '<tr>';
                    echo '<td id=on></td>';
                    echo '<td><a href=' . $main_url . '?frame=1&ip=' . $server['ip_port'] . '>' . $server_name . '</a></td>';
                    echo '<td>' . $server_ip . ':' . $server_port . '</td>';
                    echo '<td>' . $players . '</td>';
                    echo '<td>' . $map_image . '</td>';
                    echo '</tr>';
                } else {
                    $server_name = $server_ip . ':' . $server_port;
                    echo '<tr>';
                    echo '<td id=off></td>';
                    echo '<td>' . $server_name . '</td>';
                    echo '<td>' . $server_ip . ':' . $server_port . '</td>';
                    echo '<td>N/A</td>';
                    echo '<td>N/A</td>';
                    echo '</tr>';
                }
            }
        }
        echo '</table>';
    } else {
    if(isset($_GET['ip'])) {
        $selected_ip = $_GET['ip'];
        echo '<iframe src="'.$main_url.'top15.php?top=15&player=&style=1&order='.$default_order.'&default_order='.$default_order.'&ip_port='.$selected_ip.'&type=0&search="></iframe>';
    } else {
        echo '<p>No server selected.</p>';
    }
}
  ?>
  </body>
</html>