<?php
// roi_history_fetch.php
require("../config.php");
session_start();

if (!isset($_SESSION['email']) || !isset($_GET['invest_id'])) exit;
$email = $_SESSION['email'];
$invest_id = (int)$_GET['invest_id'];

$stmt = $pdo->prepare("SELECT * FROM roi_history WHERE invest_id = ? AND email = ? ORDER BY id DESC");
$stmt->execute([$invest_id, $email]);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

$currency = '$';
try {
    $invStmt = $pdo->prepare("SELECT user_cur FROM invest WHERE id = ? LIMIT 1");
    $invStmt->execute([$invest_id]);
    $invRow = $invStmt->fetch(PDO::FETCH_ASSOC);
    if ($invRow && !empty($invRow['user_cur'])) $currency = htmlspecialchars($invRow['user_cur']);
} catch (Exception $e) { /* ignore */ }

if (empty($rows)) {
    echo '<tr><td colspan="3" class="py-6 text-center text-gray-500">No ROI history yet.</td></tr>';
    exit;
}

foreach ($rows as $row) {
    echo '<tr class="border-b border-[#1A1428] hover:bg-[#1A1428]/50">';
    echo '<td class="px-4 py-3 text-green-400">' . $currency . number_format($row['amount'], 2) . '</td>';
    echo '<td class="px-4 py-3">' . htmlspecialchars($row['percentage']) . '%</td>';
    echo '<td class="px-4 py-3">' . date("M d, Y h:i A", strtotime($row['created_at'])) . '</td>';
    echo '</tr>';
}
