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

 // Fetch current session from form or use default
        $selected_session = isset($_POST['session']) ? $_POST['session'] : '';

        // Fetch all sessions for the dropdown
        $sessions_sql = "SELECT DISTINCT session FROM income";
        $sessions_result = $conn->query($sessions_sql);
?>
<!DOCTYPE html>
<html>
<head>
    <title>Session Profit and Loss</title>
    <script>
        function submitForm() {
            document.getElementById("sessionForm").submit();
        }
    </script>
    <link rel="stylesheet" type="text/css" href="#">
    <style type="text/css">
    /* General Styles */
body {
    font-family: Arial, sans-serif;
    background-color: #f4f4f4;
    margin: 0;
    padding: 0;
}

.container {
    width: 80%;
    margin: 20px auto;
    padding: 20px;
    background-color: #fff;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

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

/* Card Styles */
.card {
    padding: 20px;
    margin: 10px 0;
    border: 1px solid #ddd;
    border-radius: 5px;
    background-color: #f9f9f9;
}

.card h3, .card h4 {
    margin: 0 0 10px;
    color: #555;
}

/* Form Styles */
form {
    display: flex;
    flex-direction: column;
    align-items: center;
}

form label {
    margin: 10px 0 5px;
    font-weight: bold;
    color: #333;
}

form select {
    padding: 10px;
    width: 100%;
    max-width: 300px;
    margin-bottom: 10px;
    border: 1px solid #ddd;
    border-radius: 5px;
}

form button {
    padding: 10px 20px;
    background-color: #4CAF50;
    color: #fff;
    border: none;
    border-radius: 5px;
    cursor: pointer;
    transition: background-color 0.3s ease;
}

form button:hover {
    background-color: #45a049;
}

/* Chart Styles */
canvas {
    margin: 20px auto;
    display: block;
}

/* Responsive Styles */
@media (max-width: 768px) {
    .container {
        width: 95%;
    }

    form input[type="text"] {
        width: 100%;
    }
}

    </style>
</head>
<body>
     <form id="sessionForm" method="post" action="">
            <label for="session">Select Session:</label>
            <select name="session" id="session" onchange="submitForm()">
                <option value="Select Session">Select Session</option>
                <?php
                while ($row = $sessions_result->fetch_assoc()) {
                    $session = $row['session'];
                    echo "<option value='$session'" . ($selected_session == $session ? " selected" : "") . ">$session</option>";
                }
                ?>
            </select>
     </form>
     <?php
        if (!empty($selected_session)) {
            // Calculate total income for the selected session
            $income_sql = "SELECT SUM(amount) AS total_income FROM income WHERE session=?";
            $income_stmt = $conn->prepare($income_sql);
            $income_stmt->bind_param("s", $selected_session);
            $income_stmt->execute();
            $income_result = $income_stmt->get_result();
            $income_row = $income_result->fetch_assoc();
            $total_income = $income_row['total_income'];

            // Calculate total expenses for the selected session
            $expenses_sql = "SELECT SUM(amount) AS total_expenses FROM expenses WHERE session=?";
            $expenses_stmt = $conn->prepare($expenses_sql);
            $expenses_stmt->bind_param("s", $selected_session);
            $expenses_stmt->execute();
            $expenses_result = $expenses_stmt->get_result();
            $expenses_row = $expenses_result->fetch_assoc();
            $total_expenses = $expenses_row['total_expenses'];

            // Calculate profit/loss
            $profit_loss = $total_income - $total_expenses;
            ?>
            <h2>Profit and Loss for Session: <?php echo $selected_session; ?></h2>
            <div class="card">
                <h3>Total Income: <?php echo $total_income; ?></h3>
            </div>
            <div class="card">
                <h3>Total Expenses: <?php echo $total_expenses; ?></h3>
            </div>
            <div class="card">
                <h3>Profit/Loss: <?php echo $profit_loss; ?></h3>
            </div>
            <?php
        }
        ?>
    </div>
</body>
</html>
