<?php

require_once 'databaseConnection.php';
$mysqli = db_connect();

$currentExpense = array();
$expenses = array();

if ($mysqli) {

    if (isset($_GET['id'])) {
        // Fetch the specific expense details by id
        $id = $_GET['id'];

        $stmt = $mysqli->prepare("SELECT reference, expense, narration, category, date, amount, status FROM addExpense WHERE reference = ?");
        $stmt->bind_param("s", $id);  // assuming reference is string, change type if needed
        $stmt->execute();
        $stmt->store_result();
        $stmt->bind_result($reference, $expense, $narration, $category, $date, $amount, $status);

        if ($stmt->fetch()) {
            $currentExpense = array(
                'reference' => $reference,
                'expense' => $expense,
                'narration' => $narration,
                'category' => $category,
                'date' => $date,
                'amount' => $amount,
                'status' => $status
            );
        }
        $stmt->close();

    } else {
        // Fetch all expenses
        $stmt = $mysqli->prepare("SELECT reference, expense, narration, category, date, amount, status FROM addExpense");
        $stmt->execute();
        $stmt->store_result();
        $stmt->bind_result($reference, $expense, $narration, $category, $date, $amount, $status);

        while ($stmt->fetch()) {
            $expenses[] = array(
                'reference' => $reference,
                'expense' => $expense,
                'narration' => $narration,
                'category' => $category,
                'date' => $date,
                'amount' => $amount,
                'status' => $status
            );
        }
        $stmt->close();
    }
} else {
    echo "Database connection failed: " . $mysqli->connect_error;
    exit;
}

db_close($mysqli);
