<?php
require_once '../middleware/user_auth.php';

$user_id = $_SESSION['user_id'];

// Fetch license keys for this user
$licenseQuery = $conn->query("
    SELECT license_key 
    FROM fxh_licenses 
    WHERE user_id = '$user_id'
");

$licenses = [];
while($l = $licenseQuery->fetch_assoc()){
    $licenses[] = "'" . $conn->real_escape_string($l['license_key']) . "'";
}

// If user has no licenses
if(count($licenses) === 0){
    die("<h2>No active licenses found.</h2>");
}

$licenseList = implode(",", $licenses);
?>
<!DOCTYPE html>
<html>
<head>
<title>Live Positions</title>

<style>
body { font-family: Arial; padding:20px; background:#f5f5f5; }
.card { background:white; padding:15px; border-radius:10px; box-shadow:0 3px 8px rgba(0,0,0,0.1); margin-bottom:25px; }
table { width:100%; border-collapse:collapse; }
th, td { border:1px solid #ccc; padding:8px; font-size:14px; }
th { background:#eee; }
.pnl-green { color:green; font-weight:bold; }
.pnl-red { color:red; font-weight:bold; }
</style>

<script>
// Auto refresh every 3 seconds
setInterval(function(){
    fetch('live_positions_ajax.php')
        .then(response => response.text())
        .then(html => {
            document.getElementById('liveTable').innerHTML = html;
        });
}, 3000);
</script>

</head>
<body>

<h2>Live Positions</h2>

<div class="card" id="liveTable">
Loading...
</div>

</body>
</html>
