<?php
// Include DB connection
include './db.php';

// Start session
session_start();

// Periksa apakah user sudah login
if (!isset($_SESSION['user_id'])) {
    echo "User not logged in";
    exit();
}

$user_id = $_SESSION['user_id']; // Ambil user_id

// Query untuk mengambil 5 transaksi terbaru
$stmt = $conn->prepare("SELECT invoice_number, total_price, status, transaction_date FROM transactions WHERE user_id = ? ORDER BY transaction_date DESC LIMIT 3");
$stmt->bind_param("i", $user_id);
$stmt->execute();
$stmt->bind_result($invoice_number, $total_price, $status, $transaction_date);

$transaction_html = ''; // Untuk menampung HTML riwayat transaksi

while ($stmt->fetch()) {
    $transaction_html .= "
        <tr>
            <td>" . htmlspecialchars($invoice_number) . "</td>
            <td>Rp " . number_format($total_price, 0, ',', '.') . "</td>
            <td><span class='badge badge-success'>" . htmlspecialchars($status) . "</span></td>
            <td>" . htmlspecialchars($transaction_date) . "</td>
        </tr>
    ";
}

$stmt->close();

// Jika tidak ada transaksi
if (empty($transaction_html)) {
    $transaction_html = "<tr><td colspan='4'>No transactions found.</td></tr>";
}

echo $transaction_html; // Tampilkan hasil
?>
