<?php
session_start();
include('db_connection.php');
include('functions.php'); // Ensure this line is at the top
// Check if the student is logged in
if (!isset($_SESSION['student_id'])) {
    header("Location: login.php");
    exit();
}

$student_id = $_SESSION['student_id'];

// Fetch friends list
$sql_friends = "SELECT sf.friend_id, p.name, p.profile_picture 
                FROM student_friends sf 
                JOIN students s ON sf.friend_id = s.student_id 
                JOIN profiles p ON sf.friend_id = p.student_id 
                WHERE sf.student_id = ?";
$stmt_friends = $conn->prepare($sql_friends);
$stmt_friends->bind_param("i", $student_id);
$stmt_friends->execute();
$result_friends = $stmt_friends->get_result();

// Fetch pending friend requests
$sql_requests = "SELECT sr.request_id, sr.student_id, p.name, p.profile_picture 
                  FROM student_requests sr 
                  JOIN students s ON sr.student_id = s.student_id 
                  JOIN profiles p ON sr.student_id = p.student_id 
                  WHERE sr.requested_id = ? AND sr.status = 'Pending'";
$stmt_requests = $conn->prepare($sql_requests);
$stmt_requests->bind_param("i", $student_id);
$stmt_requests->execute();
$result_requests = $stmt_requests->get_result();
$pending_requests_count = getPendingFriendRequestsCount($student_id);
$unread_messages_count = getUnreadMessagesCount($student_id);

// Fetch sent requests from the database
$student_id = $_SESSION['student_id'];
$query = "SELECT * FROM student_requests WHERE student_id = ? AND status = 'Pending'";
$stmt = $conn->prepare($query);
$stmt->bind_param("i", $student_id);
$stmt->execute();
$result_sent_requests = $stmt->get_result();

// Fetch groups joined by the student
$sql_groups = "SELECT sg.group_id, sg.group_name, sg.group_image 
                FROM group_members gm 
                JOIN student_groups sg ON gm.group_id = sg.group_id 
                WHERE gm.student_id = ?";
$stmt_groups = $conn->prepare($sql_groups);
$stmt_groups->bind_param("i", $student_id);
$stmt_groups->execute();
$result_groups = $stmt_groups->get_result();
?>
<!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>Discover</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: 20px;
    box-sizing: border-box;
}

.sidebar-header {
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-bottom: 10px;

}
 .sidebar h2 {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin: 0;
    padding: 16px;
    font-size: 20px;
    background-color: #fff;
    border-bottom: 1px solid #e0e0e0;
    color: #000;
}

.sidebar-header h2 {
    margin: 0;
    font-size: 24px;
    color: #333;
}

.search-bar {
    width: 100%;
    padding: 10px;
    margin-top: 10px;
    border: 1px solid #ddd;
    border-radius: 5px;
    box-sizing: border-box;
}
.sidebar-content {
    display: flex;
    flex-direction: column;
}
.sidebar-item {
    display: flex;
    align-items: center;
    padding: 10px;
    color: #333;
    text-decoration: none;
    border-radius: 5px;
    margin-bottom: 10px;
    transition: background 0.3s;
}
.sidebar-item:hover {
    background-color: #f0f0f0;
}
.sidebar-item i {
    font-size: 20px;
    margin-right: 10px;
}
.create-group {
    display: block;
    padding: 10px;
    margin-bottom: 20px;
    background-color: #e7f3ff;
    color: #007bff;
    text-align: center;
    border-radius: 5px;
    text-decoration: none;
    font-weight: bold;
}
.groups-manage h3 {
    margin-bottom: 10px;
    color: #666;
    font-size: 18px;
}
.group-item {
    display: flex;
    align-items: center;
    padding: 10px;
    color: #333;
    text-decoration: none;
    border-radius: 5px;
    margin-bottom: 10px;
    transition: background 0.3s;
}
.group-item:hover {
    background-color: #f0f0f0;
}

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

.group-item div {
    display: flex;
    flex-direction: column;
}

.group-item span {
    font-size: 16px;
    font-weight: bold;
}

.group-item small {
    font-size: 12px;
    color: #888;
}
.sidebar h2 i {
    font-size: 18px;
    color: #000;
    cursor: pointer;
    background-color: #ddd;
}

.sidebar ul {
    list-style: none;
    padding: 0;
    margin: 0;
}

.sidebar ul li {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 16px;
   font-size: 18px;
    color: #000;
    cursor: pointer;
}

.sidebar a {
    color: #000;
   }

.sidebar ul li i:first-child {
    margin-right: 10px;
    font-size: 18px;
}

.sidebar ul li i:last-child {
    color: gray;
}

.sidebar ul li.active,
.sidebar ul li:hover {
    background-color: #e4e6eb;
}

.sidebar ul li span {
    flex-grow: 1;
}

.sidebar ul li i.fa-chevron-right {
    visibility: visible;
    align-items: right;
}

.sidebar ul li:not(.active):hover i.fa-chevron-right {
    visibility: visible;
}
.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="sidebar">
        <div class="sidebar-header">
            <h2>Groups</h2>
            <input type="text" placeholder="Search groups" class="search-bar">
        </div>
        <div class="sidebar-content">
            <a href="pending_requests" class="sidebar-item">
                <i class="fas fa-newspaper"></i>
                <span>Your feed</span>
            </a>
            <a href="discover.php" class="sidebar-item">
                <i class="fas fa-compass"></i>
                <span>Discover</span>
            </a>
            <a href="#" class="sidebar-item">
                <i class="fas fa-users"></i>
                <span>Your groups</span>
            </a>
            <a href="creat_group.php" class="create-group">
                + Create New Group
            </a>
            <?php
    // Retrieve student_id from the session
if (isset($_SESSION['student_id'])) {
    $studentId = $_SESSION['student_id'];
} else {
    echo "User not logged in.";
    exit;
}

// Fetch groups managed by the logged-in student
$stmt = $conn->prepare("SELECT group_name, group_image, description, created_at FROM student_groups WHERE created_by = ?");
$stmt->bind_param("i", $studentId);
$stmt->execute();
$result = $stmt->get_result();

?>

<div class="groups-manage">
    <h3>Groups you manage</h3>
    <?php
    // Ensure the query includes group_id
    $sql = "SELECT group_id, group_name, group_image, description, created_at 
            FROM student_groups 
            WHERE student_id = ?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("i", $student_id);
    $stmt->execute();
    $result = $stmt->get_result();

    if ($result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {
            // Access the fields with correct keys
            $groupId = htmlspecialchars($row['group_id']);
            $groupName = htmlspecialchars($row['group_name']);
            $groupImage = htmlspecialchars($row['group_image']);
            $description = htmlspecialchars($row['description']);
            $createdAt = htmlspecialchars($row['created_at']);
            $lastActive = getTimeDifference($createdAt); // Function to calculate last active time

            echo '<a href="managed_group_details.php?id=' . $groupId . '" class="group-item">
                    <img src="uploads/' . $groupImage . '" alt="' . $groupName . '" class="group-img">
                    <div>
                        <span>' . $groupName . '</span>
                        <small>Last active ' . $lastActive . '</small>
                    </div>
                </a>';
        }
    } else {
        echo '<p>You do not manage any groups yet.</p>';
    }
    ?>
</div>

<?php


// Function to calculate the time difference
function getTimeDifference($datetime) {
    $now = new DateTime();
    $past = new DateTime($datetime);
    $diff = $now->diff($past);

    if ($diff->y > 0) {
        return $diff->y . ' year' . ($diff->y > 1 ? 's' : '') . ' ago';
    } elseif ($diff->m > 0) {
        return $diff->m . ' month' . ($diff->m > 1 ? 's' : '') . ' ago';
    } elseif ($diff->d > 0) {
        return $diff->d . ' day' . ($diff->d > 1 ? 's' : '') . ' ago';
    } elseif ($diff->h > 0) {
        return $diff->h . ' hour' . ($diff->h > 1 ? 's' : '') . ' ago';
    } elseif ($diff->i > 0) {
        return $diff->i . ' minute' . ($diff->i > 1 ? 's' : '') . ' ago';
    } elseif ($diff->s > 0) {
        return $diff->s . ' second' . ($diff->s > 1 ? 's' : '') . ' ago';
    } else {
        return 'just now';
    }
}
?>
        </div>

       <div class="groups-manage">
        
        <?php 
        // Assuming $student_id is obtained from the session or user authentication
$student_id = $_SESSION['student_id']; // Ensure the session is started and the student ID is set

// SQL query to fetch joined groups details
$sql = "
    SELECT sg.group_id, sg.group_name, sg.group_image 
    FROM group_members gm 
    JOIN student_groups sg ON gm.group_id = sg.group_id 
    WHERE gm.student_id = ?
";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $student_id);
$stmt->execute();
$result_groups = $stmt->get_result();

if ($result_groups->num_rows > 0) {
    echo '<div class="groups-manage">';
    echo '<h3>Groups Joined</h3>';
    while ($row = $result_groups->fetch_assoc()) {
        echo '<a href="group_details.php?id=' . $row['group_id'] . '" class="group-item">';
        echo '<img src="uploads/' . $row['group_image'] . '" alt="Group Picture" class="group-img">';
        echo '<div>';
        echo '<span>' . htmlspecialchars($row['group_name']) . '</span>';
        echo '</div>';
        echo '</a>';
    }
    echo '</div>';
} else {
    echo '<div class="groups-manage">';
    echo '<h3>No Groups Joined</h3>';
    echo '</div>';
}

$stmt->close();
$conn->close();
?>
    </div>
    </div>


 <div class="container">
    <div class="header1">
        
        <?php
        include('db_connection.php');
       

        $student_id = $_SESSION['student_id'];

        // Fetch groups from student_groups table
        $sql = "SELECT group_id, group_name, group_type, group_image, created_by FROM student_groups";
        $result = $conn->query($sql);

        if ($result->num_rows > 0) {
            while ($row = $result->fetch_assoc()) {
                $group_id = $row['group_id'];
                $created_by = $row['created_by'];

                // Skip groups created by the logged-in student
                if ($created_by == $student_id) {
                    continue;
                }

                // Check if the student is already a member of the group
                $member_sql = "SELECT * FROM group_members WHERE group_id = ? AND student_id = ?";
                $member_stmt = $conn->prepare($member_sql);
                $member_stmt->bind_param("ii", $group_id, $student_id);
                $member_stmt->execute();
                $member_result = $member_stmt->get_result();

                // Check if the student has a pending join request for the group
                $request_sql = "SELECT * FROM join_requests WHERE group_id = ? AND student_id = ? AND status = 'Pending'";
                $request_stmt = $conn->prepare($request_sql);
                $request_stmt->bind_param("ii", $group_id, $student_id);
                $request_stmt->execute();
                $request_result = $request_stmt->get_result();

                if ($member_result->num_rows > 0 || $request_result->num_rows > 0) {
                    echo '<div class="group">';
                    $group_image = !empty($row["group_image"]) ? 'uploads/' . htmlspecialchars($row["group_image"]) : 'default_image.jpg';
                    echo '<img src="' . $group_image . '" alt="' . htmlspecialchars($row["group_name"]) . '">';
                    echo '<div class="group-details">';
                    echo '<h3>' . htmlspecialchars($row["group_name"]) . '</h3>';
                    echo '<p>' . htmlspecialchars($row["group_type"]) . '</p>';
                    echo '<p>You are already in this group or have a pending join request.</p>';
                    echo '</div>';
                    echo '</div>';
                } else {
                    echo '<div class="group">';
                    $group_image = !empty($row["group_image"]) ? 'uploads/' . htmlspecialchars($row["group_image"]) : 'default_image.jpg';
                    echo '<img src="' . $group_image . '" alt="' . htmlspecialchars($row["group_name"]) . '">';
                    echo '<div class="group-details">';
                    echo '<h3>' . htmlspecialchars($row["group_name"]) . '</h3>';
                    echo '<p>' . htmlspecialchars($row["group_type"]) . '</p>';

                    // Display the appropriate form based on group type
                    if ($row['group_type'] == 'public') {
                        echo '<form method="POST" action="join_public_group.php">';
                        echo '<input type="hidden" name="group_id" value="' . htmlspecialchars($row["group_id"]) . '">';
                        echo '<input type="hidden" name="student_id" value="' . htmlspecialchars($student_id) . '">';
                        echo '<button type="submit">Join Public Group</button>';
                        echo '</form>';
                    } else {
                        echo '<form method="POST" action="join_private_group.php">';
                        echo '<input type="hidden" name="group_id" value="' . htmlspecialchars($row["group_id"]) . '">';
                        echo '<input type="hidden" name="student_id" value="' . htmlspecialchars($student_id) . '">';
                        echo '<button type="submit">Request to Join Private Group</button>';
                        echo '</form>';
                    }

                    echo '</div>';
                    echo '</div>';
                }

                $member_stmt->close();
                $request_stmt->close();
            }
        } else {
            echo "No groups found";
        }

        $conn->close();
        ?>
    </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>
