<?php
include 'db.php';
$project_id = $_GET['id'];

// Project details
$project = $conn->query("SELECT * FROM projects WHERE id=$project_id")->fetch_assoc();

// Expenses
$expenses = $conn->query("SELECT * FROM project_expenses WHERE project_id=$project_id");
$total_expenses = 0;
while ($row = $expenses->fetch_assoc()) {
    $total_expenses += $row['expense_amount'];
}

$margin = $project['project_po_amount'] - $total_expenses;
?>
<!DOCTYPE html>
<html>
<head>
  <title>Project Report</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
</head>
<body class="p-4">

<h2>Project: <?= $project['name'] ?></h2>
<p><b>Project PO:</b> RM <?= number_format($project['project_po_amount'], 2) ?></p>
<p><b>Total Expenses:</b> RM <?= number_format($total_expenses, 2) ?></p>
<p><b>Margin:</b> RM <?= number_format($margin, 2) ?></p>

<h4>Expenses</h4>
<table class="table table-bordered">
  <tr><th>Date</th><th>Name</th><th>Amount (RM)</th></tr>
  <?php
  $expenses = $conn->query("SELECT * FROM project_expenses WHERE project_id=$project_id");
  while ($row = $expenses->fetch_assoc()) {
      echo "<tr>
              <td>{$row['expense_date']}</td>
              <td>{$row['expense_name']}</td>
              <td>" . number_format($row['expense_amount'],2) . "</td>
            </tr>";
  }
  ?>
</table>

<a href="export_project_pdf.php?id=<?= $project_id ?>" class="btn btn-danger">Export to PDF</a>

</body>
</html>
