<?php include("../app/headl.php"); ?>

<?php
if (!isset($_SESSION['user'])) exit();

$user = mysqli_query($conn, "SELECT * FROM `tb_user` WHERE user = '" . $_SESSION['user'] . "'") or die(mysqli_error($conn));
$u = mysqli_fetch_array($user);
$usersID = $u['cuid'];

$filter = $_GET['filter'] ?? '';

// Query history
$where = "WHERE userID='$usersID'";

if ($filter == 'game') {
    $where .= " AND result IS NOT NULL";
} elseif ($filter == 'depowd') {
    $where .= " AND (note LIKE '%Deposit%' OR note LIKE '%Withdraw%')";
} elseif ($filter == 'log') {
    $where .= " AND (note LIKE '%Login%' OR note LIKE '%Logout%' OR note LIKE '%Password%')";
} elseif ($filter == 'dll') {
    $where .= " AND (note LIKE '%Bonus%' OR note LIKE '%Freebet%' OR note LIKE '%Promo%')";
}

$sql = mysqli_query($conn, "SELECT * FROM tb_history_game $where ORDER BY datetime DESC") or die(mysqli_error($conn));

if (mysqli_num_rows($sql) > 0) {
    while ($row = mysqli_fetch_assoc($sql)) {
        $statusColor = ($row['result'] == 'Win') ? 'color:#00ff00; font-weight:bold;' : 'color:#ff4444; font-weight:bold;';
        echo "<tr>
                <td align='center'>" . htmlspecialchars($row['datetime']) . "</td>
                <td align='center'>" . htmlspecialchars($row['note']) . "</td>
                <td align='center'>Rp " . number_format($row['bet'], 0, ',', '.') . "</td>
                <td align='center'>Rp " . number_format($row['win'], 0, ',', '.') . "</td>
                <td align='center' style='$statusColor'>" . htmlspecialchars($row['result']) . "</td>
                <td align='center'>" . htmlspecialchars($row['provider']) . "</td>
              </tr>";
    }
} else {
    echo "<tr><td colspan='6' align='center'>Belum ada transaksi.</td></tr>";
}
?>
