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

// Assuming the logged-in student's ID is stored in the session
$loggedInStudentId = $_SESSION['student_id'];

// Fetch all pending join requests for groups created by the logged-in student
$sql = "SELECT jr.request_id, jr.group_id, jr.student_id, sg.group_name, s.name 
        FROM join_requests jr 
        JOIN student_groups sg ON jr.group_id = sg.group_id 
        JOIN students s ON jr.student_id = s.student_id 
        WHERE jr.status = 'Pending' AND sg.created_by = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $loggedInStudentId);
$stmt->execute();
$result = $stmt->get_result();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Pending Join Requests</title>
    <style type="text/css">
        body {
    font-family: Arial, sans-serif;
    background-color: #f4f4f4;
    margin: 0;
    padding: 0;
}

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

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

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

table th, table td {
    padding: 12px;
    text-align: left;
    border-bottom: 1px solid #ddd;
}

table th {
    background-color: #f8f8f8;
    color: #333;
}

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

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

a:hover {
    text-decoration: underline;
}

.btn {
    display: inline-block;
    padding: 8px 16px;
    font-size: 14px;
    color: #fff;
    background-color: #3498db;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    text-align: center;
}

.btn:hover {
    background-color: #2980b9;
}

.action-buttons {
    text-align: right;
}

.table-responsive {
    overflow-x: auto;
}

    </style>
</head>
<body>
    <div class="container">
        <h1>Pending Join Requests</h1>
        <div class="table-responsive">
            <table>
                <thead>
                    <tr>
                        <th>Request ID</th>
                        <th>Group Name</th>
                        <th>Student Name</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php while ($row = $result->fetch_assoc()): ?>
                        <tr>
                            <td><?= htmlspecialchars($row['request_id']) ?></td>
                            <td><?= htmlspecialchars($row['group_name']) ?></td>
                            <td><?= htmlspecialchars($row['name']) ?></td>
                            <td class="action-buttons">
                                <a href="approve_join_request.php?request_id=<?= htmlspecialchars($row['request_id']) ?>" class="btn">Approve</a>
                            </td>
                        </tr>
                    <?php endwhile; ?>
                </tbody>
            </table>
        </div>
    </div>
</body>
</html>

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