<!DOCTYPE html>
<html>
<head>
    <title>Compare Periods</title>
    <link rel="stylesheet" type="text/css" href="#">
    <script>
        function submitForm() {
            document.getElementById("compareForm").submit();
        }
    </script>
    <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:

    </style>
</head>
<body>
    <div class="container">
        <?php
       // Include the database connection
include 'db_connection.php';
        // Fetch terms and sessions for the dropdown
        $terms_sql = "SELECT DISTINCT term FROM income";
        $terms_result1 = $conn->query($terms_sql);
        $terms_result2 = $conn->query($terms_sql);

        $sessions_sql = "SELECT DISTINCT session FROM income";
        $sessions_result1 = $conn->query($sessions_sql);
        $sessions_result2 = $conn->query($sessions_sql);

        // Fetch selected terms and sessions from form
        $selected_term1 = isset($_POST['term1']) ? $_POST['term1'] : '';
        $selected_term2 = isset($_POST['term2']) ? $_POST['term2'] : '';
        $selected_session1 = isset($_POST['session1']) ? $_POST['session1'] : '';
        $selected_session2 = isset($_POST['session2']) ? $_POST['session2'] : '';
        ?>
        <form id="compareForm" method="post" action="">
            <label for="term1">Select Term 1:</label>
            <select name="term1" id="term1" onchange="submitForm()">
                <option value="">Select Term</option>
                <?php
                while ($row = $terms_result1->fetch_assoc()) {
                    $term = $row['term'];
                    echo "<option value='$term'" . ($selected_term1 == $term ? " selected" : "") . ">$term</option>";
                }
                ?>
            </select>

            <label for="session1">Select Session 1:</label>
            <select name="session1" id="session1" onchange="submitForm()">
                <option value="">Select Session</option>
                <?php
                while ($row = $sessions_result1->fetch_assoc()) {
                    $session = $row['session'];
                    echo "<option value='$session'" . ($selected_session1 == $session ? " selected" : "") . ">$session</option>";
                }
                ?>
            </select>

            <label for="term2">Select Term 2:</label>
            <select name="term2" id="term2" onchange="submitForm()">
                <option value="">Select Term</option>
                <?php
                while ($row = $terms_result2->fetch_assoc()) {
                    $term = $row['term'];
                    echo "<option value='$term'" . ($selected_term2 == $term ? " selected" : "") . ">$term</option>";
                }
                ?>
            </select>

            <label for="session2">Select Session 2:</label>
            <select name="session2" id="session2" onchange="submitForm()">
                <option value="">Select Session</option>
                <?php
                while ($row = $sessions_result2->fetch_assoc()) {
                    $session = $row['session'];
                    echo "<option value='$session'" . ($selected_session2 == $session ? " selected" : "") . ">$session</option>";
                }
                ?>
            </select>
        </form>

        <?php
        if (!empty($selected_term1) && !empty($selected_session1) && !empty($selected_term2) && !empty($selected_session2)) {
            // Calculate income and expenses for the selected periods
            $income_sql1 = "SELECT SUM(amount) AS total_income FROM income WHERE term=? AND session=?";
            $income_stmt1 = $conn->prepare($income_sql1);
            $income_stmt1->bind_param("ss", $selected_term1, $selected_session1);
            $income_stmt1->execute();
            $income_result1 = $income_stmt1->get_result();
            $income_row1 = $income_result1->fetch_assoc();
            $total_income1 = $income_row1['total_income'];

            $expenses_sql1 = "SELECT SUM(amount) AS total_expenses FROM expenses WHERE term=? AND session=?";
            $expenses_stmt1 = $conn->prepare($expenses_sql1);
            $expenses_stmt1->bind_param("ss", $selected_term1, $selected_session1);
            $expenses_stmt1->execute();
            $expenses_result1 = $expenses_stmt1->get_result();
            $expenses_row1 = $expenses_result1->fetch_assoc();
            $total_expenses1 = $expenses_row1['total_expenses'];

            $income_sql2 = "SELECT SUM(amount) AS total_income FROM income WHERE term=? AND session=?";
            $income_stmt2 = $conn->prepare($income_sql2);
            $income_stmt2->bind_param("ss", $selected_term2, $selected_session2);
            $income_stmt2->execute();
            $income_result2 = $income_stmt2->get_result();
            $income_row2 = $income_result2->fetch_assoc();
            $total_income2 = $income_row2['total_income'];

            $expenses_sql2 = "SELECT SUM(amount) AS total_expenses FROM expenses WHERE term=? AND session=?";
            $expenses_stmt2 = $conn->prepare($expenses_sql2);
            $expenses_stmt2->bind_param("ss", $selected_term2, $selected_session2);
            $expenses_stmt2->execute();
            $expenses_result2 = $expenses_stmt2->get_result();
            $expenses_row2 = $expenses_result2->fetch_assoc();
            $total_expenses2 = $expenses_row2['total_expenses'];

            $profit_loss1 = $total_income1 - $total_expenses1;
            $profit_loss2 = $total_income2 - $total_expenses2;
            ?>
            <h2>Comparison of Selected Periods</h2>
            <div class="card">
                <h3>Term: <?php echo $selected_term1; ?>, Session: <?php echo $selected_session1; ?></h3>
                <p>Total Income: <?php echo $total_income1; ?></p>
                <p>Total Expenses: <?php echo $total_expenses1; ?></p>
                <p>Profit/Loss: <?php echo $profit_loss1; ?></p>
            </div>
            <div class="card">
                <h3>Term: <?php echo $selected_term2; ?>, Session: <?php echo $selected_session2; ?></h3>
                <p>Total Income: <?php echo $total_income2; ?></p>
                <p>Total Expenses: <?php echo $total_expenses2; ?></p>
                <p>Profit/Loss: <?php echo $profit_loss2; ?></p>
            </div>
            <?php
        }
        ?>
    </div>
</body>
</html>
