<?php
include 'db.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $project_id = $_POST['project_id'];
    $expense_name = $_POST['expense_name'];
    $expense_amount = $_POST['expense_amount'];
    $expense_date = $_POST['expense_date'];

    $sql = "INSERT INTO project_expenses (project_id, expense_name, expense_amount, expense_date) 
            VALUES ('$project_id', '$expense_name', '$expense_amount', '$expense_date')";
    $conn->query($sql);
    header("Location: project_report.php?id=$project_id");
    exit;
}
?>

<!-- Trigger Button -->
<button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#expenseModal">Add Expense</button>

<!-- Modal -->
<div class="modal fade" id="expenseModal" tabindex="-1">
  <div class="modal-dialog">
    <form method="POST" action="add_expense.php">
      <div class="modal-content">
        <div class="modal-header"><h5>Add Project Expense</h5></div>
        <div class="modal-body">
          <input type="hidden" name="project_id" value="<?= $project_id ?>">
          <div class="mb-3">
            <label>Expense Name</label>
            <input type="text" name="expense_name" class="form-control" required>
          </div>
          <div class="mb-3">
            <label>Expense Amount</label>
            <input type="number" step="0.01" name="expense_amount" class="form-control" required>
          </div>
          <div class="mb-3">
            <label>Expense Date</label>
            <input type="date" name="expense_date" class="form-control" required>
          </div>
        </div>
        <div class="modal-footer">
          <button type="submit" class="btn btn-success">Save</button>
        </div>
      </div>
    </form>
  </div>
</div>
