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

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

$student_id = $_SESSION['student_id'];

// Helper function to calculate the time difference
function time_elapsed_string(string $datetime, bool $full = false): string {
    $now = new DateTime();
    $ago = new DateTime($datetime);
    $diff = $now->diff($ago);

    $diff->w = floor($diff->d / 7);
    $diff->d -= $diff->w * 7;

    $string = [
        'y' => 'year',
        'm' => 'month',
        'w' => 'week',
        'd' => 'day',
        'h' => 'hour',
        'i' => 'minute',
        's' => 'second',
    ];
    foreach ($string as $k => &$v) {
        if ($diff->$k) {
            $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
        } else {
            unset($string[$k]);
        }
    }

    if (!$full) $string = array_slice($string, 0, 1);
    return $string ? implode(', ', $string) . ' ago' : 'just now';
}

// Get the logged-in student's ID
if (!$student_id) {
    die("Student ID not found. Please log in.");
}

$group_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

// Validate group ID
if ($group_id <= 0) {
    die("Invalid Group ID: Group ID must be a positive integer.");
}

// SQL query to get the latest posts by the logged-in student
$sql = "SELECT p.post_id, p.group_id, p.student_id, p.post_content, p.timestamp, p.group_image, p.post_sender,
               IF(p.post_sender = 0, 'Anonymous sender', s.name) AS student_name, 
               s.profile_picture
        FROM group_posts p
        LEFT JOIN profiles s ON p.student_id = s.student_id
        WHERE p.group_id = ? AND (p.student_id = ? OR p.post_sender = ?)
        ORDER BY p.timestamp DESC
        LIMIT 2";
$stmt = $conn->prepare($sql);
$stmt->bind_param("iii", $group_id, $student_id, $student_id);
$stmt->execute();
$result = $stmt->get_result();

// Check if posts are retrieved
if ($result->num_rows === 0) {
    echo "No posts found.";
}

// Prepare SQL query to fetch group image, name, type, and count members
$sql = "SELECT group_image, group_name, group_type, 
               (SELECT COUNT(*) FROM group_members WHERE group_id = ?) as total_members 
        FROM student_groups WHERE group_id = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ii", $group_id, $group_id);
$stmt->execute();
$result_group = $stmt->get_result();

// Initialize variables with default values
$group_image = "default_image.png";
$group_name = "Group Name";
$group_type = "public";
$total_members = 0;

// Check if image, name, type, and members count are found
if ($result_group->num_rows > 0) {
    $row_group = $result_group->fetch_assoc();
    $group_image = !empty($row_group['group_image']) ? "uploads/" . htmlspecialchars($row_group['group_image']) : $group_image;
    $group_name = !empty($row_group['group_name']) ? htmlspecialchars($row_group['group_name']) : $group_name;
    $group_type = !empty($row_group['group_type']) ? htmlspecialchars($row_group['group_type']) : $group_type;
    $total_members = !empty($row_group['total_members']) ? (int)$row_group['total_members'] : $total_members;
} else {
    die("No group found with the provided Group ID.");
}

// Fetch student profile information
$profile_sql = "SELECT name, profile_picture FROM profiles WHERE student_id = ?";
$profile_stmt = $conn->prepare($profile_sql);
$profile_stmt->bind_param("i", $student_id);
$profile_stmt->execute();
$profile_result = $profile_stmt->get_result();
$profile = $profile_result->fetch_assoc();

// Fetch group type
$group_sql = "SELECT group_type FROM student_groups WHERE group_id = ?";
$group_stmt = $conn->prepare($group_sql);
$group_stmt->bind_param("i", $group_id);
$group_stmt->execute();
$group_result = $group_stmt->get_result();
$group = $group_result->fetch_assoc();

// Default values if not found
$profile_picture = !empty($profile['profile_picture']) ? "uploads/" . htmlspecialchars($profile['profile_picture']) : "uploads/default_profile_picture.jpg";
$student_name = htmlspecialchars($profile['name']);
$group_type = !empty($group['group_type']) ? htmlspecialchars($group['group_type']) : "Public group";

// Close the database connection
$stmt->close();
$profile_stmt->close();
$group_stmt->close();
$conn->close();


?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="sidebar.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
    <title>Managed Group</title>
    <style type="text/css">
        body {
    font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            display: flex;
            flex-direction: row; /* Adjusted to row to fit sidebar and main content */
            background-color: #f0f2f5;
    overflow-y: hidden; 

}

header {
        background-color: #34495e;
        color: white;
        padding: 10px 20px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        z-index: 2;
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);

    }

    .header-left, .header-center, .header-right {
        display: flex;
        align-items: center;
    }

    .header-left img {
        height: 40px;
        margin-right: 20px;
    }

    .search-box {
        padding: 5px;
        border-radius: 4px;
        border: none;
        width: 250px; /* Adjusted width */
    }

    .header-center .icon, .header-right .icon {
        margin: 0 10px;
        cursor: pointer;
        position: relative;
        margin-right: 35px; /* Space from the right edge */
    }


    .header-right .icon .dropdown {
        display: none;
        position: absolute;
        top: 100%;
        left: 50%;
        background-color: white;
        border: 1px solid #ddd;
        padding: 10px;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
        z-index: 1;
        width: max-content; /* Adjust to fit content */
        transform: translateX(-70%);
    }



    .header-right .icon:hover .dropdown {
        display: block;
    }

    .dropdown-item {
        padding: 15px 10px;
        cursor: pointer;
        display: flex;
        align-items: center;
        text-decoration: none;
        color: #000;
    }

    .dropdown-item i {
        margin-right: 8px;
    }

    .dropdown-item:hover {
        background-color: #f0f0f0;
    }

    .header-center a.dropdown-item {
        margin: 0 10px;
        cursor: pointer;
        text-decoration: none;
        color: white;
        display: flex;
        align-items: center;
    }

    .header-center a.dropdown-item:hover {
        color: #000;
    }

.sidebar {
    width: 300px;
    background-color: #fff;
    box-shadow: 0 0 10px rgba(0,0,0,0.1);
    border-radius: 8px;
    overflow: hidden;
    height: calc(100% - 50px);
    padding-top: 80px;
    position: fixed;
    left: 0;
    height: 100vh; /* Full height to align with header */
    top: 0; /* Ensure it's aligned with the top of the viewport */
    overflow-y: auto; /* Allow scrolling if needed */
    padding: 10px;
    box-sizing: border-box;
    margin-top: 68px; /* Add margin to create space below the header */
}

.sidebar h3 {
    margin-top: 0;
}

.sidebar-menu, .moderator-tools ul {
    list-style-type: none;
    padding: 0;
}

.sidebar-menu li, .moderator-tools ul li {
    margin: 10px 0;
}

.sidebar-menu li a, .moderator-tools ul li a {
    text-decoration: none;
    color: #333;
}

.moderator-tools h4 {
    margin-top: 20px;
}

.badge {
    background-color: #f44336;
    color: white;
    border-radius: 10px;
    padding: 2px 10px;
}

.new {
    font-size: 12px;
    color: blue;
}

.btn.create-chat {
    background-color: #4CAF50;
    color: white;
    border: none;
    padding: 10px;
    border-radius: 5px;
    cursor: pointer;
    margin-top: 20px;
    width: 100%;
}

.main-section {
    flex: 1;
    background-color: #fff;
    border: 1px solid #ddd;
    border-radius: 5px;
    padding: 20px;
}

.header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
}

.header h2 {
    font-size: 24px;
    margin: 0;
}

.header-actions {
    display: flex;
    align-items: center;
}

.header-actions .btn {
    padding: 10px 20px;
    border: none;
    border-radius: 5px;
    margin-left: 10px;
    cursor: pointer;
}

.header-actions .approve {
    background-color: #4CAF50;
    color: #fff;
}

.header-actions .decline {
    background-color: #f44336;
    color: #fff;
}

.header-actions .dropdown {
    padding: 10px;
    border-radius: 5px;
    margin-left: 10px;
}

.filters {
    display: flex;
    align-items: center;
    margin-bottom: 20px;
}

.filters .search {
    padding: 10px;
    border-radius: 5px;
    border: 1px solid #ddd;
    margin-right: 10px;
    flex: 1;
}

.filters .btn {
    padding: 10px 20px;
    border: none;
    border-radius: 5px;
    cursor: pointer;
    margin-right: 10px;
}

.filters .clear-filters {
    background-color: #f44336;
    color: #fff;
}

.filters .dropdown {
    padding: 10px;
    border-radius: 5px;
    margin-right: 10px;
}

.post {
    border: 1px solid #ddd;
    border-radius: 5px;
    padding: 20px;
    margin-bottom: 20px;
}

.post-header {
    display: flex;
    align-items: center;
    margin-bottom: 10px;
}

.post-checkbox {
    margin-right: 10px;
}

.post-profile-img {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    margin-right: 10px;
}

.post-info {
    flex: 1;
}

.post-author {
    font-weight: bold;
    display: block;
}

.post-time {
    color: #666;
    font-size: 12px;
}

.post-more {
    background: none;
    border: none;
    cursor: pointer;
    font-size: 20px;
}

.post-content {
    margin-top: 10px;
}

.post-content p {
    margin: 0 0 10px;
}

.post-img {
    width: 100%;
    border-radius: 5px;
}

.group-header {
    display: flex;
    align-items: center;
    margin-bottom: 20px;
}

.group-image {
    width: 50px;
    height: 50px;
    border-radius: 50%;
    margin-right: 10px;
}

.group-info h3 {
    margin: 0;
    font-size: 18px;
    font-weight: bold;
}

.group-info p {
    margin: 0;
    color: #666;
    font-size: 14px;
}

.tabs {
    display: flex;
    justify-content: space-between;
    margin-bottom: 20px;
}

.tab-button {
    padding: 10px;
    border: none;
    background-color: transparent;
    cursor: pointer;
    font-size: 16px;
    font-weight: bold;
    color: #666;
}

.tab-button.active {
    border-bottom: 2px solid red;
    color: #000;
}

.main-section {
    flex: 1;
    background-color: #fff;
    border: 1px solid #ddd;
    border-radius: 5px;
    padding: 20px;
    width: calc(100% - 300px); /* Adjust width to fit sidebar */
}

.sidebar-content .group-header-img {
    margin-bottom: 10px;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    margin-right: 10px;
}
.section {
    margin-bottom: 20px;

}

.section-title {
    font-size: 16px;
    font-weight: bold;
    color: #333;
    margin-bottom: 10px;
}

.moderator-tools {
    margin-bottom: 20px;
}

.tool {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 10px;
}

.tool.pending {
    background-color: #ffe6e6;
}

.tool.spam {
    background-color: #e6f7ff;
}

.badge {
    background-color: #ff0000;
    color: #fff;
    padding: 2px 6px;
    border-radius: 12px;
    font-size: 12px;
    margin-left: 10px;
}

.new-today {
    color: #666;
    font-size: 12px;
}

.create-chat {
    width: 100%;
    padding: 10px;
    background-color: #ff0000;
    color: #fff;
    border: none;
    cursor: pointer;
    font-size: 16px;
    border-radius: 4px;
    margin-top: 20px;
}
.container {
    margin-left: 300px; /* Align with the sidebar */
    width: calc(100% - 300px); /* Adjust width to fit sidebar */
    padding: 20px 20px;
    padding-top: 70px; /* Space for header */
    background-color: #ddd;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
    border-radius: 8px;
    overflow-y: auto; /* Enable vertical scrolling */
    height: calc(100vh - 80px); /* Adjust height to fit below the header */

}

.main {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
}

.main h2 {
    margin: 0;
    font-size: 24px;
}

.main .see-all {
    font-size: 14px;
    color: #1877f2;
    text-decoration: none;
}

.friend-requests {
    display: flex;
    flex-wrap: wrap;
    gap: 20px;
}

.card {
    width: calc(25% - 20px);
    background-color: #fff;
    border: 1px solid #e0e0e0;
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
    display: flex;
    flex-direction: column;
    align-items: center;
}
 


.card img {
    width: 100%;
    height: 180px;
    object-fit: cover;
}

.card .info {
    padding: 10px;
    text-align: center;
}

.card .info h3 {
    margin: 10px 0;
    font-size: 18px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.card .info p {
    margin: 0;
    font-size: 14px;
    color: gray;
}

.card .info p i {
    margin-right: 5px;
}

.card .btn {
    width: 100%;
    padding: 10px;
    margin: 5px 0;
    font-size: 16px;
    border: none;
    cursor: pointer;
}

.card .btn.confirm {
    background-color: #1877f2;
    color: #fff;
    border-radius: 5px;
}

.card .btn.delete {
    background-color: #f0f2f5;
    color: #000;
    border-radius: 5px;
}
.user-avatar .profile-picture {
    width: 40px; /* Adjust the width as needed */
    height: 40px; /* Adjust the height as needed */
    border-radius: 50%; /* Makes the image circular */
    object-fit: cover; /* Ensures the image covers the container without distortion */
    cursor: pointer; /* Adds a pointer cursor for better UX */
    border: 2px solid #fff; /* Optional: Adds a white border for better visibility */
}

.user-avatar {
    position: relative;
    display: inline-block;
}

.user-avatar .dropdown {
    display: none;
    position: absolute;
    top: 50px; /* Adjust to position the dropdown correctly */
    right: 0;
    background-color: #fff;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
    z-index: 1;
}

.user-avatar:hover .dropdown {
    display: block;
}

   

/* Media Queries for Responsiveness */
@media (max-width: 768px) {
    .main-content {
        flex-direction: column;
    }

    .sidebar {
        width: 100%;
        height: auto;
        box-shadow: none;
        padding: 10px;
    }

    .sidebar-header h2 {
        font-size: 20px;
    }

    .search-bar {
        padding: 8px;
        margin-top: 5px;
    }

    .sidebar-item i {
        font-size: 18px;
        margin-right: 8px;
    }

    .create-group {
        padding: 8px;
        margin-bottom: 15px;
    }

    .groups-manage h3 {
        font-size: 16px;
    }

    .group-item {
        padding: 8px;
    }

    .group-img {
        width: 40px;
        height: 40px;
        margin-right: 8px;
    }

    .group-item span {
        font-size: 14px;
    }

    .group-item small {
        font-size: 10px;
    }
}

@media (max-width: 480px) {
    .header {
        padding: 10px;
        font-size: 18px;
    }

    .sidebar-header h2 {
        font-size: 18px;
    }

    .search-bar {
        padding: 6px;
    }

    .sidebar-item i {
        font-size: 16px;
        margin-right: 6px;
    }

    .create-group {
        padding: 6px;
        margin-bottom: 10px;
    }

    .groups-manage h3 {
        font-size: 14px;
    }

    .group-item {
        padding: 6px;
    }

    .group-img {
        width: 30px;
        height: 30px;
        margin-right: 6px;
    }

    .group-item span {
        font-size: 12px;
    }

    .group-item small {
        font-size: 8px;
    }
}

        .group-image {
            height: 200px;
            object-fit: cover;
        }

        
        





.header1 {
    display: flex;
    flex-wrap: wrap;
    gap: 10px; /* Adjust the gap between groups */
    padding: 10px;
    background-color: #fff;
    border-radius: 8px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

.group {
    flex: 1 0 21%; /* Adjust the percentage to fit the number of groups per row */
    padding: 15px;
    margin: 5px;
    text-align: center;
    background-color: #eaeaea;
    border-radius: 4px;
    box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
    transition: background-color 0.3s ease;
}

.group img {
    width: 100%;
    height: auto;
    border-radius: 4px;
}

.group-details {
    margin-top: 10px;
}

.group h3 {
    margin: 10px 0 5px 0;
    font-size: 1.2em;
    color: #333;
}

.group p {
    margin: 0;
    font-size: 1em;
    color: #666;
}

.group button {
    margin-top: 10px;
    padding: 10px 15px;
    font-size: 1em;
    color: #fff;
    background-color: #007bff;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    transition: background-color 0.3s ease;
}

.group button:hover {
    background-color: #0056b3;
}

.group:hover {
    background-color: #ddd;
}

    </style>
</head>
<body>
    <header>
        <div class="header-left">
           <a href="index.php">
                <img src="/csms/admin/uploads/11.png" alt="Logo">
            </a>
            <div class="search-box">
                <input type="text" placeholder="Search">
            </div>
        </div>
        <div class="header-center">
            <a href="#" class="dropdown-item"><i class="fas fa-home"></i></a>
            <a href="creat_group.php" class="dropdown-item"><i class="fas fa-users"></i></a>
            <a href="#" class="dropdown-item"><i class="fa-brands fa-facebook-messenger"></i></a>
        </div>
        <div class="header-right">
            <div class="icon menu">
                <i class="fas fa-bars"></i>
                <div class="dropdown">
                    <a href="#" class="dropdown-item"><i class="fas fa-plus"></i> Create</a>
                    <a href="#" class="dropdown-item"><i class="fas fa-pen"></i> Post</a>
                    <a href="#" class="dropdown-item"><i class="fas fa-book"></i> Story</a>
                    <a href="#" class="dropdown-item"><i class="fas fa-birthday-cake"></i> Life Event</a>
                    <a href="#" class="dropdown-item"><i class="fas fa-video"></i> Reel</a>
                    <a href="#" class="dropdown-item"><i class="fas fa-flag"></i> Page</a>
                    <a href="#" class="dropdown-item"><i class="fas fa-users"></i> Group</a>
                    <a href="#" class="dropdown-item"><i class="fas fa-calendar-alt"></i> Event</a>
                </div>
            </div>
            <div class="icon"><i class="fas fa-bell"></i></div>
            <div class="icon user-avatar">
                <img src="<?= $profilePicturePath ?>" alt="Profile Picture" class="profile-picture">
 
                <div class="dropdown">
                    <a href="#" class="dropdown-item"><i class="fas fa-cog"></i> Settings</a>
                    <a href="#" class="dropdown-item"><i class="fas fa-user-shield"></i> Privacy</a>
                    <a href="#" class="dropdown-item"><i class="fas fa-adjust"></i> Display</a>
                    <a href="#" class="dropdown-item"><i class="fas fa-universal-access"></i> Accessibility</a>
                    <a href="logout.php" class="dropdown-item"><i class="fas fa-sign-out-alt"></i> Log Out</a>
                </div>
            </div>
        </div>
    </header>
    <div class="container">
    <div class="sidebar">
        <!-- Sidebar content -->
        <div class="sidebar-content">
            <img src="<?php echo $group_image; ?>" alt="Gunners Forum" class="group-header-img">
        <h3><?php echo $group_name; ?></h3>
            <p><i class="group-icon"></i> <?php echo ucfirst($group_type); ?> group · <?php echo number_format($total_members); ?> members</p>
            <ul class="sidebar-menu">
                <li><a href="#">Chats</a></li>
                <li><a href="#">Manage 20+</a></li>
                <li><a href="#">Community home</a></li>
                <li><a href="#">Overview</a></li>
            </ul>
            <div class="moderator-tools">
                <h4>Moderator tools</h4>
                <ul>
                    <li><a href="#">Member requests <span class="badge">20+</span></a></li>
                    <li><a href="#">Membership questions <span class="badge">20+</span></a></li>
                    <li><a href="#">Pending approvals <span class="badge">20+ <span class="new">4 new today</span></span></a></li>
                    <li><a href="#">Potential spam <span class="badge">20+ <span class="new">3 new today</span></span></a></li>
                </ul>
            </div>
            <button class="btn create-chat">+ Create a Chat</button>
        </div>
    </div>
    <div class="main-section">
        <div class="header">
            <h2>Pending approvals • 20+</h2>
            <div class="header-actions">
                <button class="btn approve">Approve</button>
                <button class="btn decline">Decline</button>
                <select class="dropdown">
                    <option value="newest">Newest first</option>
                    <option value="oldest">Oldest first</option>
                </select>
            </div>
        </div>
        <div class="filters">
            <input type="text" placeholder="Search" class="search">
            <button class="btn clear-filters">Clear filters</button>
            <select class="dropdown">
                <option value="link">Link</option>
                <option value="photo">Photo</option>
                <option value="text">Text</option>
            </select>
            <select class="dropdown">
                <option value="date">Select dates</option>
                <option value="last-week">Last week</option>
                <option value="last-month">Last month</option>
            </select>
            <select class="dropdown">
                <option value="author">Author</option>
                <option value="user1">User 1</option>
                <option value="user2">User 2</option>
            </select>
            <select class="dropdown">
                <option value="content-type">Content type</option>
                <option value="type1">Type 1</option>
                <option value="type2">Type 2</option>
            </select>
        </div>
        <div class="post">
             <?php while ($post = $result->fetch_assoc()): ?>
            <div class="post-header">
                <img src="<?php echo !empty($post['profile_picture']) ? 'uploads/' . htmlspecialchars($post['profile_picture']) : 'uploads/default_profile_picture.jpg'; ?>" alt="Profile Picture">
                <span class="name"><?php echo htmlspecialchars($post['student_name']); ?></span>
                <span class="timestamp"><?php echo !empty($post['timestamp']) ? time_elapsed_string($post['timestamp']) : 'Unknown time'; ?></span>
            
                <button class="btn post-more">•••</button>
            </div>
            <div class="post-content">
                <p><?php echo htmlspecialchars($post['post_content']); ?></p>
                <?php if ($post['group_image']): ?>
                <img src="uploads/<?php echo htmlspecialchars($post['group_image']); ?>" alt="Group Image">
                <?php endif; ?>
            </div>
        </div>
        <?php endwhile; ?>
    </div>
</div>


<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>

</body>
</html>
