<?php
// Include the database connection
include 'db_connection.php';

$term = $_POST['term'];
$session = $_POST['session'];

$query_income = "SELECT SUM(amount) AS total_income FROM income WHERE term = '$term' AND session = '$session'";
$result_income = $conn->query($query_income);
$row_income = $result_income->fetch_assoc();
$total_income = $row_income['total_income'];

$query_expenses = "SELECT SUM(amount) AS total_expenses FROM expenses WHERE term = '$term' AND session = '$session'";
$result_expenses = $conn->query($query_expenses);
$row_expenses = $result_expenses->fetch_assoc();
$total_expenses = $row_expenses['total_expenses'];

$profit_loss = $total_income - $total_expenses;
?>

<!DOCTYPE html>
<html>
<head>
    <title>Term Profit and Loss</title>
    <link rel="stylesheet" type="text/css" href="#">
    <style type="text/css">
        .container {
    max-width: 800px;
    margin: 0 auto;
    padding: 20px;
    background-color: #f4f4f4;
    border-radius: 8px;
}

h2 {
    text-align: center;
    margin-bottom: 20px;
}

form {
    display: flex;
    flex-direction: column;
    align-items: center;
}

label {
    margin-top: 10px;
}

select, button {
    margin-top: 5px;
    padding: 10px;
    border-radius: 5px;
    border: 1px solid #ccc;
}

button {
    background-color: #007bff;
    color: white;
    cursor: pointer;
}

button:hover {
    background-color: #0056b3;
}

    </style>
</head>
<body>
    <div class="container">
        <h2>Profit and Loss for Term: <?php echo $term; ?>, Session: <?php echo $session; ?></h2>
        <p>Total Income: <?php echo $total_income; ?></p>
        <p>Total Expenses: <?php echo $total_expenses; ?></p>
        <p>Profit/Loss: <?php echo $profit_loss; ?></p>
    </div>
</body>
</html>
