<?php
session_start();
if (!isset($_SESSION['admin_id']) && $_SESSION['role'] != 'admin') {
    header("Location: ../login.php");
    exit();
}
include '../../include/config.php';

$investments = $conn->query("
    SELECT i.*, p.name as user_name, e.equipment_name
    FROM hps_user_investments i
    LEFT JOIN patients p ON i.user_id = p.patient_id
    LEFT JOIN hps_invest_equipment e ON i.equipment_id = e.id
    ORDER BY i.created_at DESC
");

$page_title = 'Investment Management';
include '../../include/header.php';
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Investment Management</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container mt-4">
    <div class="d-flex justify-content-between mb-4">
        <h2><i class="fas fa-chart-line me-2"></i> HPS Investments</h2>
        <a href="dashboard.php" class="btn btn-secondary">← Back</a>
    </div>
    <div class="card shadow">
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-bordered">
                    <thead><tr><th>ID</th><th>User</th><th>Equipment</th><th>Shares</th><th>Amount</th><th>Expected ROI</th><th>Maturity</th><th>Status</th></tr></thead>
                    <tbody>
                        <?php while($i = $investments->fetch_assoc()): ?>
                        <tr>
                            <td><?= $i['id'] ?></td>
                            <td><?= htmlspecialchars($i['user_name'] ?? $i['user_id']) ?></td>
                            <td><?= htmlspecialchars($i['equipment_name'] ?? 'N/A') ?></td>
                            <td><?= $i['shares_purchased'] ?></td>
                            <td>₦<?= number_format($i['amount_invested'], 2) ?></td>
                            <td class="text-success"><?= $i['expected_return'] ?>%</td>
                            <td><?= date('M d, Y', strtotime($i['maturity_date'])) ?></td>
                            <td><span class="badge bg-<?= $i['status'] == 'active' ? 'success' : 'secondary' ?>"><?= ucfirst($i['status']) ?></span></td>
                        </tr>
                        <?php endwhile; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
<?php include '../../include/footer.php'; ?>