<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Edit Group</title>
    <link rel="stylesheet" href="styles.css"> <!-- Assuming you have a styles.css file for styling -->
</head>
<body>
<div class="container">
    <div class="create-group-container">
        <h2>Edit Group</h2>
        <?php
        include('db_connection.php');
        session_start();

        // Check if group_id is passed and get group details
        if (isset($_GET['group_id'])) {
            $group_id = $_GET['group_id'];
            $stmt = $conn->prepare("SELECT * FROM student_groups WHERE group_id = ?");
            $stmt->bind_param("i", $group_id);
            $stmt->execute();
            $result = $stmt->get_result();

            if ($result->num_rows > 0) {
                $group = $result->fetch_assoc();
            } else {
                echo "Group not found.";
                exit;
            }

            $stmt->close();
        } else {
            echo "Invalid request.";
            exit;
        }
        ?>
        <form action="process_edit_create_group.php" method="post" enctype="multipart/form-data">
            <input type="hidden" name="group_id" value="<?php echo htmlspecialchars($group['group_id']); ?>">
            <div class="form-group">
                <label for="group-name">Group Name</label>
                <input type="text" id="group-name" name="group_name" value="<?php echo htmlspecialchars($group['group_name']); ?>" required>
            </div>
            <div class="form-group">
                <label for="group-description">Description</label>
                <textarea id="group-description" name="group_description" required><?php echo htmlspecialchars($group['description']); ?></textarea>
            </div>
            <div class="form-group">
                <label>Privacy</label>
                <div class="privacy-option">
                    <input type="radio" id="public" name="group_type" value="public" <?php echo ($group['group_type'] == 'public') ? 'checked' : ''; ?> required>
                    <label for="public">Public</label>
                </div>
                <div class="privacy-option">
                    <input type="radio" id="private" name="group_type" value="private" <?php echo ($group['group_type'] == 'private') ? 'checked' : ''; ?> required>
                    <label for="private">Private</label>
                </div>
            </div>
            <div class="form-group">
                <label for="group-category">Category</label>
                <select id="group-category" name="group_category">
                    <option value="">Select a category</option>
                    <option value="study" <?php echo ($group['group_category'] == 'study') ? 'selected' : ''; ?>>Study Group</option>
                    <option value="project" <?php echo ($group['group_category'] == 'project') ? 'selected' : ''; ?>>Project Team</option>
                    <option value="interest" <?php echo ($group['group_category'] == 'interest') ? 'selected' : ''; ?>>Interest Group</option>
                </select>
            </div>
            <div class="form-group">
                <label for="group-rules">Group Rules (Optional)</label>
                <textarea id="group-rules" name="group_rules"><?php echo htmlspecialchars($group['group_rules']); ?></textarea>
            </div>
            <div class="form-group">
                <label for="group-image">Group Icon/Image (Optional)</label>
                <input type="file" id="group-image" name="group_image">
                <?php if (!empty($group['group_image'])): ?>
                    <p>Current Image: <img src="uploads/<?php echo htmlspecialchars($group['group_image']); ?>" alt="Group Image" width="100"></p>
                <?php endif; ?>
            </div>
            <div class="form-group">
                <button type="submit">Update Group</button>
            </div>
        </form>
    </div>
</div>
</body>
</html>
