<?php
session_start();
include('db_connection.php');
$group_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if ($group_id <= 0) {
    die("Invalid Group ID.");
}

// Assume the student ID is stored in the session
$student_id = isset($_SESSION['student_id']) ? (int)$_SESSION['student_id'] : 0;
if ($student_id <= 0) {
    die("Invalid Student ID.");
}

  // Fetch student profile information
    $student_id = $_SESSION['student_id']; // Assuming student ID is stored in session
    $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
    $profile_stmt->close();
    $group_stmt->close();
    $conn->close();
    ?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Create Post</title>
    <link rel="stylesheet" href="create_post.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <style type="text/css">
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .create-post-container {
            width: 400px;
            background-color: #fff;
            border-radius: 10px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            padding: 20px;
        }

        .create-post-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            border-bottom: 1px solid #eaeaea;
            padding-bottom: 10px;
            margin-bottom: 10px;
        }

        .create-post-header h3 {
            margin: 0;
        }

        .close-btn {
            background: none;
            border: none;
            font-size: 24px;
            cursor: pointer;
        }

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

        .user-info {
            display: flex;
            align-items: center;
            margin-bottom: 10px;
        }

        .profile-img {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            margin-right: 10px;
            display: block; /* Default to visible */
        }

        .user-details p {
            margin: 0;
            font-weight: bold;
        }

        .group-type {
            font-size: 12px;
            color: #888;
        }

        textarea {
            width: 100%;
            border: 1px solid #eaeaea;
            border-radius: 10px;
            padding: 10px;
            margin-bottom: 10px;
            resize: none;
        }

        .post-options {
            display: flex;
            justify-content: space-between;
            margin-bottom: 10px;
        }

        .post-options button,
        .post-options label {
            background: none;
            border: none;
            cursor: pointer;
            font-size: 20px;
            color: #888;
        }

        .post-options label {
            cursor: pointer;
        }

        .post-btn {
            width: 100%;
            padding: 10px;
            background-color: #007bff;
            color: #fff;
            border: none;
            border-radius: 10px;
            cursor: pointer;
            font-size: 16px;
        }

        .post-btn:hover {
            background-color: #0056b3;
        }
    </style>
</head>
<body>
    <div class="create-post-container">
        <div class="create-post-header">
            <h3>Create post</h3>
            <button class="close-btn" onclick="window.location.href='group_details.php?id=<?= $group_id ?>'">&times;</button>
        </div>
        <form action="create_post.php?id=<?= $group_id ?>" method="post" enctype="multipart/form-data">

            <input type="hidden" name="group_id" value="<?= $group_id ?>">
            <div class="anonymous-toggle">
                <label for="anonymous">Post anonymously</label>
                <input type="checkbox" id="anonymous" name="anonymous" onchange="toggleAnonymous()">
            </div>
            <div class="user-info" id="user-info">
                <img src="<?= $profile_picture ?>" alt="User Profile Picture" class="profile-img" id="profile-pic">
                <div class="user-details">
                    <p id="username"><?= $student_name ?></p>
                    <span class="group-type"><?= $group_type ?></span>
                </div>
            </div>
            <textarea name="post_content" placeholder="Create a <?= $group_type ?> Post..."></textarea>
            <div class="post-options">
                <label for="group_image"><i class="fas fa-images"></i></label>
                <input type="file" id="group_image" name="group_image" accept="image/*" style="display: none;">
                <button type="button"><i class="fas fa-user-friends"></i></button>
                <button type="button"><i class="fas fa-map-marker-alt"></i></button>
                <button type="button"><i class="fas fa-smile"></i></button>
            </div>
            <button type="submit" class="post-btn">Post</button>
        </form>
    </div>

    <script>
function toggleAnonymous() {
    const anonymousCheckbox = document.getElementById('anonymous');
    const username = document.getElementById('username');
    const profilePic = document.getElementById('profile-pic');
    const userInfo = document.getElementById('user-info');
    
    if (anonymousCheckbox.checked) {
        username.textContent = 'Anonymous sender';
        profilePic.style.display = 'none'; // Hide profile picture
    } else {
        username.textContent = '<?= htmlspecialchars($student_name) ?>';
        profilePic.style.display = 'block'; // Show profile picture
    }
}
</script>

</body>
</html>
