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

// Check if student is logged in
if (!isset($_SESSION['student_id'])) {
    header("Location: login.php");
    exit();
}

$student_id = $_SESSION['student_id'];

// Fetch groups managed by the logged-in student
$sql = "SELECT g.group_id, g.group_name, g.description, g.created_by, g.created_at, s.name AS admin_name
        FROM student_groups g
        JOIN students s ON g.created_by = s.student_id
        WHERE g.created_by = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $student_id);
$stmt->execute();
$result = $stmt->get_result();
$groups = [];
while ($row = $result->fetch_assoc()) {
    $groups[] = $row;
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Your Groups</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f4f4f4;
        }
        .navbar {
            background-color: #34495e;
            color: #fff;
            padding: 15px;
            display: flex;
            justify-content: center;
            gap: 20px;
            position: fixed;
            width: 100%;
            top: 0;
            z-index: 1000;
        }
        .navbar a {
            color: #fff;
            text-decoration: none;
            padding: 10px 20px;
            font-weight: bold;
        }
        .navbar a:hover {
            background-color: #2c3e50;
            border-radius: 5px;
        }
        h2 {
            text-align: center;
            margin-top: 60px;
        }
        table {
            width: 80%;
            margin: 20px auto;
            border-collapse: collapse;
            background-color: #fff;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }
        th, td {
            padding: 10px;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }
        th {
            background-color: #f4f4f4;
        }
        .action-buttons {
            display: flex;
            gap: 10px;
        }
        .action-buttons a {
            text-decoration: none;
            color: #3498db;
            padding: 5px 10px;
            border: 1px solid #3498db;
            border-radius: 4px;
            background-color: #fff;
        }
        .action-buttons a:hover {
            background-color: #3498db;
            color: #fff;
        }
    </style>
</head>
<body>
    <div class="navbar">
        <a href="for_you.php">For You</a>
        <a href="your_group.php">Your Group</a>
        <a href="discover.php">Discover</a>
    </div>

    <h2>Your Groups</h2>
    <?php if (count($groups) > 0): ?>
        <table>
            <tr>
                <th>Group ID</th>
                <th>Group Name</th>
                <th>Description</th>
                <th>Created By</th>
                <th>Created At</th>
                <th>Action</th>
            </tr>
            <?php foreach ($groups as $group): ?>
                <tr>
                    <td><?php echo htmlspecialchars($group['group_id']); ?></td>
                    <td><?php echo htmlspecialchars($group['group_name']); ?></td>
                    <td><?php echo htmlspecialchars($group['description']); ?></td>
                    <td><?php echo htmlspecialchars($group['admin_name']); ?></td>
                    <td><?php echo htmlspecialchars($group['created_at']); ?></td>
                    <td class="action-buttons">
                        <a href="view_group_members.php?group_id=<?php echo htmlspecialchars($group['group_id']); ?>">View Members</a>
                        <a href="join_group.php?group_id=<?php echo htmlspecialchars($group['group_id']); ?>">Join Via Link</a>
                        <a href="message_group.php?group_id=<?php echo htmlspecialchars($group['group_id']); ?>">Message Group</a>
                    </td>
                </tr>
            <?php endforeach; ?>
        </table>
    <?php else: ?>
        <p>You are not managing any groups.</p>
    <?php endif; ?>
</body>
</html>
