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

// Fetch all students for the dropdown
$students_query = "SELECT student_id, name FROM students";
$students_result = $conn->query($students_query);

// Fetch all classes for the dropdown
$classes_query = "SELECT class_id, class_name FROM classes";
$classes_result = $conn->query($classes_query);

// Initialize filters
$selected_student_id = isset($_POST['student_id']) ? $_POST['student_id'] : '';
$selected_class_id = isset($_POST['class_id']) ? $_POST['class_id'] : '';

// Construct the payment query with optional filters
$payment_query = "SELECT p.payment_id, p.amount, p.date, p.receipt_no, p.purpose, s.name as student_name, c.class_name 
                  FROM payments p
                  JOIN students s ON p.student_id = s.student_id
                  JOIN classes c ON s.class_id = c.class_id";
$where_clauses = [];
$params = [];
$types = '';

if ($selected_student_id) {
    $where_clauses[] = 'p.student_id = ?';
    $params[] = $selected_student_id;
    $types .= 'i';
}

if ($selected_class_id) {
    $where_clauses[] = 'c.class_id = ?';
    $params[] = $selected_class_id;
    $types .= 'i';
}

if (count($where_clauses) > 0) {
    $payment_query .= ' WHERE ' . implode(' AND ', $where_clauses);
}

$stmt = $conn->prepare($payment_query);

if (count($params) > 0) {
    $stmt->bind_param($types, ...$params);
}

$stmt->execute();
$payments_result = $stmt->get_result();
$stmt->close();
?>

<!DOCTYPE html>
<html>
<head>
    <title>View Payments</title>
    <style>
        table {
            width: 100%;
            border-collapse: collapse;
        }
        table, th, td {
            border: 1px solid black;
        }
        th, td {
            padding: 10px;
            text-align: left;
        }
        th {
            background-color: #f2f2f2;
        }
        .filter-form {
            margin-bottom: 20px;
        }
    </style>
</head>
<body>
    <h2>View Payments</h2>
    <form method="post" class="filter-form">
        <label for="student_id">Select Student:</label>
        <select name="student_id" id="student_id">
            <option value="">All Students</option>
            <?php while ($student_row = $students_result->fetch_assoc()): ?>
                <option value="<?php echo htmlspecialchars($student_row['student_id']); ?>" <?php echo ($student_row['student_id'] == $selected_student_id) ? 'selected' : ''; ?>>
                    <?php echo htmlspecialchars($student_row['name']); ?>
                </option>
            <?php endwhile; ?>
        </select>
        <label for="class_id">Select Class:</label>
        <select name="class_id" id="class_id">
            <option value="">All Classes</option>
            <?php while ($class_row = $classes_result->fetch_assoc()): ?>
                <option value="<?php echo htmlspecialchars($class_row['class_id']); ?>" <?php echo ($class_row['class_id'] == $selected_class_id) ? 'selected' : ''; ?>>
                    <?php echo htmlspecialchars($class_row['class_name']); ?>
                </option>
            <?php endwhile; ?>
        </select>
        <button type="submit">Filter</button>
    </form>

    <table>
        <thead>
            <tr>
                <th>Payment ID</th>
                <th>Receipt No</th>
                <th>Date</th>
                <th>Student Name</th>
                <th>Class</th>
                <th>Amount Paid</th>
                <th>Purpose</th>
                <th>Action</th>
            </tr>
        </thead>
        <tbody>
            <?php if ($payments_result->num_rows > 0): ?>
                <?php while ($row = $payments_result->fetch_assoc()): ?>
                    <tr>
                        <td><?php echo htmlspecialchars($row['payment_id']); ?></td>
                        <td><?php echo htmlspecialchars($row['receipt_no']); ?></td>
                        <td><?php echo htmlspecialchars($row['date']); ?></td>
                        <td><?php echo htmlspecialchars($row['student_name']); ?></td>
                        <td><?php echo htmlspecialchars($row['class_name']); ?></td>
                        <td><?php echo number_format($row['amount'], 2); ?></td>
                        <td><?php echo htmlspecialchars($row['purpose']); ?></td>
                        <td><a href="view_receipt.php?id=<?php echo $row['payment_id']; ?>&format=pos">View Receipt</a></td>
                    </tr>
                <?php endwhile; ?>
            <?php else: ?>
                <tr>
                    <td colspan="8">No payments found.</td>
                </tr>
            <?php endif; ?>
        </tbody>
    </table>
</body>
</html>

<?php
$conn->close();
?>
