<?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'];

// Handle search query
$search_query = "";
if (isset($_POST['search_query'])) {
    $search_query = $_POST['search_query'];
    $search_query = "%{$search_query}%";
}

// Fetch students based on search query
$sql_search = "SELECT student_id, name, profile_picture FROM students WHERE name LIKE ? AND student_id != ?";
$stmt_search = $conn->prepare($sql_search);
$stmt_search->bind_param("si", $search_query, $student_id);
$stmt_search->execute();
$result_search = $stmt_search->get_result();

// Fetch random students for "People You May Know"
$sql_random = "SELECT student_id, name, profile_picture FROM students WHERE student_id != ? ORDER BY RAND() LIMIT 5";
$stmt_random = $conn->prepare($sql_random);
$stmt_random->bind_param("i", $student_id);
$stmt_random->execute();
$result_random = $stmt_random->get_result();
$pending_requests_count = getPendingFriendRequestsCount($student_id);
$unread_messages_count = getUnreadMessagesCount($student_id);
$conn->close();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Search Friends</title>
    <link rel="stylesheet" href="styles.css"> <!-- Link to your CSS file -->
    <style>
        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;
        }
        .search-bar {
            margin-bottom: 20px;
        }
        .search-bar input[type="text"] {
            width: 100%;
            padding: 10px;
            border-radius: 5px;
            border: 1px solid #ccc;
        }
        .search-results, .people-you-may-know {
            background: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            margin-bottom: 20px;
        }
        .search-results img, .people-you-may-know img {
            width: 50px;
            height: 50px;
            border-radius: 50%;
        }
        .search-results ul, .people-you-may-know ul {
            list-style: none;
            padding: 0;
        }
        .search-results ul li, .people-you-may-know ul li {
            display: flex;
            align-items: center;
            margin-bottom: 15px;
        }
        .search-results ul li form, .people-you-may-know ul li form {
            margin-left: auto;
            display: flex;
            gap: 10px;
        }
        .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 $unread_messages_count; ?></span>
                    <?php endif; ?>
                </a>
                <div class="dropdown-content">
                    <a href="messages.php">View Messages</a>
                    <a href="search_friends.php">Search Friends</a>
                </div>
            </div>
            <div class="dropdown">
                <a href="#" class="dropdown-toggle">Friends
                    <?php if ($pending_requests_count > 0): ?>
                        <span class="notification-badge"><?php echo $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>
            <a href="groups.php">Groups</a>
            <a href="logout.php">Logout</a>
        </div>
    </div>
    <div class="main-content">
        <div class="search-bar">
            <form action="search_friends.php" method="post">
                <input type="text" name="search_query" placeholder="Search for friends..." value="<?php echo htmlspecialchars($search_query); ?>">
                <button type="submit">Search</button>
            </form>
        </div>

        <div class="search-results">
            <h2>Search Results</h2>
            <?php if ($result_search->num_rows > 0): ?>
                <ul>
                    <?php while ($row = $result_search->fetch_assoc()): ?>
                        <li>
                            <img src="<?php echo htmlspecialchars('/csms/student/uploads/profile_pictures/' . $row['profile_picture']); ?>" alt="Profile Picture">
                            <span><?php echo htmlspecialchars($row['name']); ?></span>
                            <form action="send_friend_request.php" method="post">
                                <input type="hidden" name="friend_id" value="<?php echo htmlspecialchars($row['student_id']); ?>">
                                <button type="submit">Add Friend</button>
                            </form>
                        </li>
                    <?php endwhile; ?>
                </ul>
            <?php else: ?>
                <p>No results found.</p>
            <?php endif; ?>
        </div>

        <div class="people-you-may-know">
            <h2>People You May Know</h2>
            <?php if ($result_random->num_rows > 0): ?>
                <ul>
                    <?php while ($row = $result_random->fetch_assoc()): ?>
                        <li>
                            <img src="<?php echo htmlspecialchars('/csms/student/uploads/profile_pictures/' . $row['profile_picture']); ?>" alt="Profile Picture">
                            <span><?php echo htmlspecialchars($row['name']); ?></span>
                            <form action="send_friend_request.php" method="post">
                                <input type="hidden" name="friend_id" value="<?php echo htmlspecialchars($row['student_id']); ?>">
                                <button type="submit">Add Friend</button>
                            </form>
                        </li>
                    <?php endwhile; ?>
                </ul>
            <?php else: ?>
                <p>No recommendations available.</p>
            <?php endif; ?>
        </div>
    </div>
</body>
</html>
