<?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 unread messages count
$sql_messages_count = "SELECT COUNT(*) FROM student_messages WHERE receiver_id = ? AND status = 'Unread'";
$stmt_messages_count = $conn->prepare($sql_messages_count);
$stmt_messages_count->bind_param("i", $student_id);
$stmt_messages_count->execute();
$stmt_messages_count->bind_result($unread_messages_count);
$stmt_messages_count->fetch();
$stmt_messages_count->close();

// Fetch recent messages
$sql_recent_messages = "SELECT sender_id, message, timestamp FROM student_messages WHERE receiver_id = ? ORDER BY timestamp DESC LIMIT 5";
$stmt_recent_messages = $conn->prepare($sql_recent_messages);
$stmt_recent_messages->bind_param("i", $student_id);
$stmt_recent_messages->execute();
$result_recent_messages = $stmt_recent_messages->get_result();

// Fetch student profile information
$sql_profile = "SELECT name, profile_picture FROM students WHERE student_id = ?";
$stmt_profile = $conn->prepare($sql_profile);
$stmt_profile->bind_param("i", $student_id);
$stmt_profile->execute();
$stmt_profile->bind_result($name, $profile_picture);
$stmt_profile->fetch();
$stmt_profile->close();

// Define the path to the profile picture
$profile_picture_path = '/csms/student/uploads/profile_pictures/' . $profile_picture;

// Fetch friend requests
$sql_requests = "SELECT sr.request_id, sr.student_id, s.name, s.profile_picture 
                  FROM student_requests sr 
                  JOIN students s ON sr.student_id = s.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();

// Fetch user groups
$sql_groups = "SELECT g.group_id, g.group_name FROM student_groups sg
                JOIN groups g ON sg.group_id = g.group_id
                WHERE sg.student_id = ?";
$stmt_groups = $conn->prepare($sql_groups);
$stmt_groups->bind_param("i", $student_id);
$stmt_groups->execute();
$result_groups = $stmt_groups->get_result();

// Fetch recent activities
$sql_activities = "SELECT * FROM upcoming_activities WHERE activity_id = ? ORDER BY activity_date DESC LIMIT 5";
$stmt_activities = $conn->prepare($sql_activities);
$stmt_activities->bind_param("i", $student_id);
$stmt_activities->execute();
$result_activities = $stmt_activities->get_result();

// Fetch pending friend requests count using a function if defined
$pending_requests_count = getPendingFriendRequestsCount($student_id);

// Fetch unread messages count using a function if defined
$unread_messages_count = getUnreadMessagesCount($student_id);

// Close the database connection
$conn->close();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Student Dashboard</title>
    <link rel="stylesheet" href="styles.css"> <!-- Link to your CSS file -->
    <style>
        /* Custom CSS styling */
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f4f4f4;
        }
        .header {
            background-color: #34495e;
            color: #fff;
            padding: 15px;
            text-align: center;
            position: fixed;
            top: 0;
            width: 100%;
            z-index: 1000;
        }
        .header .nav-links {
            display: flex;
            justify-content: space-around;
            align-items: center;
        }
        .header .nav-links a, .header .nav-links .dropdown {
            color: #fff;
            text-decoration: none;
            padding: 10px 20px;
            font-weight: bold;
            position: relative;
        }
        .header .nav-links a:hover, .header .nav-links .dropdown:hover {
            background-color: #2c3e50;
            border-radius: 5px;
        }
        .header .dropdown-content {
            display: none;
            position: absolute;
            background-color: #fff;
            min-width: 200px;
            box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
            z-index: 1;
        }
        .header .dropdown-content a {
            color: black;
            padding: 12px 16px;
            text-decoration: none;
            display: block;
        }
        .header .dropdown-content a:hover {
            background-color: #ddd;
        }
        .header .dropdown:hover .dropdown-content {
            display: block;
        }
        .main-content {
            padding: 80px 20px 20px;
            display: flex;
            gap: 20px;
        }
        .profile-section {
            width: 30%;
            background: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }
        .profile-section img {
            width: 150px;
            height: 150px;
            border-radius: 50%;
        }
        .section {
            width: 70%;
        }
        .section .widget {
            background: #fff;
            padding: 20px;
            margin-bottom: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }
        .footer {
            background: #34495e;
            color: #fff;
            text-align: center;
            padding: 10px;
            position: fixed;
            bottom: 0;
            width: 100%;
        }
        .widget ul {
            list-style: none;
            padding: 0;
        }
        .widget li {
            margin-bottom: 10px;
            display: flex;
            align-items: center;
        }
        .widget li img {
            margin-right: 10px;
        }
        .widget form {
            display: inline;
        }
        .widget button {
            margin-left: 10px;
            cursor: pointer;
        }
        .notification-badge {
            position: absolute;
            top: -10px;
            right: -10px;
            background-color: red;
            color: white;
            border-radius: 50%;
            width: 20px;
            height: 20px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 12px;
        }
    </style>
</head>
<body>
    <div class="header">
        <div class="nav-links">
            <a href="dashboard.php">Home</a>
            <a href="view_profile.php">Profile</a>
            <div class="dropdown">
                <a href="#" class="dropdown-toggle">Messages
                    <?php if ($unread_messages_count > 0): ?>
                        <span class="notification-badge"><?php echo htmlspecialchars($unread_messages_count); ?></span>
                    <?php endif; ?>
                </a>
                <div class="dropdown-content">
                    <a href="messages.php">Compose Message</a>
                    <a href="view_message.php">View Messages</a>
                </div>
            </div>
            <div class="dropdown">
                <a href="#" class="dropdown-toggle">Friends
                    <?php if ($pending_requests_count > 0): ?>
                        <span class="notification-badge"><?php echo htmlspecialchars($pending_requests_count); ?></span>
                    <?php endif; ?>
                </a>
                <div class="dropdown-content">
                    <a href="friends.php">View Friends</a>
                    <a href="friend_requests.php">Friend Requests</a>
                    <a href="search_friends.php">Search Friends</a>
                </div>
            </div>
            <div class="dropdown">
                <a href="#" class="dropdown-toggle">Groups</a>
                <div class="dropdown-content">
                    <a href="Creat_group.php">Create Group</a>
                    <a href="add_member.php">Add Students</a>
                     <a href="view_groups.php">View Group</a>
                    <a href="view_group_members.php">View Members</a>
                    <a href="join_group.php">Join Groups</a>
                </div>
            </div>
            <a href="logout.php">Logout</a>
        </div>
    </div>

    <div class="main-content">
        <div class="profile-section">
            <?php if (!empty($profile_picture) && file_exists($_SERVER['DOCUMENT_ROOT'] . $profile_picture_path)): ?>
                <img src="<?php echo htmlspecialchars($profile_picture_path, ENT_QUOTES, 'UTF-8'); ?>" alt="Profile Picture">
            <?php else: ?>
                <p>Profile picture not found.</p>
            <?php endif; ?>
            <h2><?php echo htmlspecialchars($name); ?></h2>
            <a href="view_profile.php">View Full Profile</a>
        </div>

        <div class="section">
            <div class="widget">
                <h3>Friend Requests</h3>
                <?php if ($result_requests->num_rows > 0): ?>
                    <ul>
                        <?php while ($row = $result_requests->fetch_assoc()): ?>
                            <li>
                                <img src="<?php echo htmlspecialchars('/csms/student/uploads/profile_pictures/' . $row['profile_picture'], ENT_QUOTES, 'UTF-8'); ?>" alt="Profile Picture" width="50">
                                <span><?php echo htmlspecialchars($row['name']); ?></span>
                                <form action="accept_request.php" method="POST" style="display: inline;">
                                    <input type="hidden" name="request_id" value="<?php echo htmlspecialchars($row['request_id']); ?>">
                                    <button type="submit">Accept</button>
                                </form>
                                <form action="reject_request.php" method="POST" style="display: inline;">
                                    <input type="hidden" name="request_id" value="<?php echo htmlspecialchars($row['request_id']); ?>">
                                    <button type="submit">Reject</button>
                                </form>
                            </li>
                        <?php endwhile; ?>
                    </ul>
                <?php else: ?>
                    <p>No friend requests.</p>
                <?php endif; ?>
            </div>

            <div class="widget">
                <h3>Messages</h3>
                <?php if ($result_recent_messages->num_rows > 0): ?>
                    <ul>
                        <?php while ($row = $result_recent_messages->fetch_assoc()): ?>
                            <li>
                                <strong><?php echo htmlspecialchars($row['sender_id']); ?></strong>: <?php echo htmlspecialchars($row['message']); ?>
                                <br>
                                <small><?php echo htmlspecialchars($row['timestamp']); ?></small>
                            </li>
                        <?php endwhile; ?>
                    </ul>
                <?php else: ?>
                    <p>No recent messages.</p>
                <?php endif; ?>
            </div>

            <div class="widget">
                <h3>Groups</h3>
                <?php if ($result_groups->num_rows > 0): ?>
                    <ul>
                        <?php while ($row = $result_groups->fetch_assoc()): ?>
                            <li><?php echo htmlspecialchars($row['group_name']); ?></li>
                        <?php endwhile; ?>
                    </ul>
                <?php else: ?>
                    <p>No groups.</p>
                <?php endif; ?>
            </div>

            <div class="widget">
                <h3>Upcoming Activities</h3>
                <?php if ($result_activities->num_rows > 0): ?>
                    <ul>
                        <?php while ($row = $result_activities->fetch_assoc()): ?>
                            <li><?php echo htmlspecialchars($row['activity_date']) . ': ' . htmlspecialchars($row['activity_description']); ?></li>
                        <?php endwhile; ?>
                    </ul>
                <?php else: ?>
                    <p>No upcoming activities.</p>
                <?php endif; ?>
            </div>
        </div>
    </div>

    <div class="footer">
        <p>&copy; 2024 School Management System. All rights reserved.</p>
    </div>
</body>
</html>
