<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Manage Student Fees</title>
    <link rel="stylesheet" href="#">
    <style type="text/css">
        /* styles.css */

        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f9;
            margin: 0;
            padding: 0;
        }

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

        h2 {
            text-align: center;
            color: #333;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin: 20px 0;
        }

        table, th, td {
            border: 1px solid #ddd;
        }

        th, td {
            padding: 12px;
            text-align: left;
        }

        th {
            background-color: #f2f2f2;
        }

        tr:nth-child(even) {
            background-color: #f9f9f9;
        }

        tr:hover {
            background-color: #f1f1f1;
        }

        a {
            color: #3498db;
            text-decoration: none;
        }

        a:hover {
            text-decoration: underline;
        }

        .btn {
            display: inline-block;
            padding: 10px 15px;
            font-size: 14px;
            cursor: pointer;
            text-align: center;
            text-decoration: none;
            outline: none;
            color: #fff;
            background-color: #4CAF50;
            border: none;
            border-radius: 15px;
            box-shadow: 0 9px #999;
        }

        .btn:hover {background-color: #3e8e41}

        .btn:active {
            background-color: #3e8e41;
            box-shadow: 0 5px #666;
            transform: translateY(4px);
        }
    </style>
</head>
<body>
    <?php include 'header.php'; ?>
    <h2>Manage Student Fees</h2>

    <form method="GET" action="">
        <label for="class">Select Class:</label>
        <select name="class" id="class" onchange="this.form.submit()">
            <option value="">All Classes</option>
            <?php
            // Include your database connection file
            include('db_connection.php');

            // Fetch classes
            $class_query = "SELECT class_id, class_name FROM classes";
            $class_result = mysqli_query($conn, $class_query);

            while ($class_row = mysqli_fetch_assoc($class_result)) {
                $selected = isset($_GET['class']) && $_GET['class'] == $class_row['class_id'] ? 'selected' : '';
                echo "<option value='{$class_row['class_id']}' {$selected}>{$class_row['class_name']}</option>";
            }
            ?>
        </select>
    </form>

    <table>
        <thead>
            <tr>
                <th>ID</th>
                <th>Name</th>
                <th>Class</th>
                <th>Fees Paid</th>
                <th>Fees Outstanding</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            <?php
            // Fetch student fee records along with class name
            $class_filter = isset($_GET['class']) && $_GET['class'] !== '' ? "WHERE students.class_id = " . $_GET['class'] : '';
            $query = "
                SELECT students.student_id, students.name, students.fees_paid, students.fees_outstanding, classes.class_name
                FROM students
                JOIN classes ON students.class_id = classes.class_id
                $class_filter
            ";
            $result = mysqli_query($conn, $query);

            while ($row = mysqli_fetch_assoc($result)) {
                echo "<tr>
                        <td>{$row['student_id']}</td>
                        <td>{$row['name']}</td>
                        <td>{$row['class_name']}</td>
                        <td>{$row['fees_paid']}</td>
                        <td>{$row['fees_outstanding']}</td>
                        <td>
                            <a href='edit_fees.php?id={$row['student_id']}'>Edit</a>
                        </td>
                    </tr>";
            }
            ?>
        </tbody>
    </table>
</body>
</html>
