<?php
include('db_connection.php');


// Fetch terms from the database
$query_terms = "SELECT * FROM terms";
$result_terms = $conn->query($query_terms);
$terms = [];
if ($result_terms->num_rows > 0) {
    while ($row = $result_terms->fetch_assoc()) {
        $terms[] = $row;
    }
}

// Fetch sessions from the database
$query_sessions = "SELECT * FROM sessions";
$result_sessions = $conn->query($query_sessions);
$sessions = [];
if ($result_sessions->num_rows > 0) {
    while ($row = $result_sessions->fetch_assoc()) {
        $sessions[] = $row;
    }
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>Add Expense</title>
    
</head>
<body>
    <?php include 'header.php'; ?>
    <form action="process_add_expense.php" method="post">
        <label for="description">Description:</label>
        <input type="text" id="description" name="description" required>
        <label for="amount">Amount:</label>
        <input type="number" id="amount" name="amount" step="0.01" required>
        <label for="date">Date:</label>
        <input type="date" id="date" name="date" required>
         <label for="term">Term:</label>
            <select id="term" name="term">
                <?php if (!empty($terms)) { ?>
                    <?php foreach ($terms as $term) { ?>
                        <option value="<?php echo $term['term_name']; ?>"><?php echo $term['term_name']; ?></option>
                    <?php } ?>
                <?php } else { ?>
                    <option value="">No terms available</option>
                <?php } ?>
            </select>

            <label for="session">Session:</label>
            <select id="session" name="session">
                <?php if (!empty($sessions)) { ?>
                    <?php foreach ($sessions as $session) { ?>
                        <option value="<?php echo $session['session_name']; ?>"><?php echo $session['session_name']; ?></option>
                    <?php } ?>
                <?php } else { ?>
                    <option value="">No sessions available</option>
                <?php } ?>
            </select>
        <button type="submit">Add Expense</button>
    </form>
</body>
</html>
