<?php
session_start();
include "../include/config.php";

if (!isset($_SESSION['email'])) {
    echo "Unauthorized access";
    exit;
}

$class_id = intval($_POST['class_id']);
$userlogin = $_SESSION['email'];

$query = "SELECT * FROM online_classes WHERE id = ? AND created_by = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param("is", $class_id, $userlogin);
$stmt->execute();
$result = $stmt->get_result();

if ($row = $result->fetch_assoc()):
?>
    <div class="row">
        <div class="col-md-6">
            <div class="form-group">
                <label for="edit_class_name">Class Name</label>
                <input type="hidden" name="class_id" value="<?php echo $row['id']; ?>">
                <input type="text" class="form-control" id="edit_class_name" name="class_name" 
                       value="<?php echo htmlspecialchars($row['class_name']); ?>" required>
            </div>
        </div>
        <div class="col-md-6">
            <div class="form-group">
                <label for="edit_course_id">Select Course</label>
                <select class="form-control" id="edit_course_id" name="course_id" required>
                    <option value="">-- Select Course --</option>
                    <?php
                    $courses = $conn->query("SELECT id, course FROM course ORDER BY course");
                    while($course = $courses->fetch_assoc()):
                        $selected = ($course['id'] == $row['course_id']) ? 'selected' : '';
                    ?>
                        <option value="<?php echo $course['id']; ?>" <?php echo $selected; ?>>
                            <?php echo htmlspecialchars($course['course']); ?>
                        </option>
                    <?php endwhile; ?>
                </select>
            </div>
        </div>
    </div>
    
    <div class="form-group">
        <label for="edit_description">Description</label>
        <textarea class="form-control" id="edit_description" name="description" rows="3"><?php 
            echo htmlspecialchars($row['description']); 
        ?></textarea>
    </div>
    
    <div class="row">
        <div class="col-md-6">
            <div class="form-group">
                <label for="edit_start_time">Start Time</label>
                <input type="datetime-local" class="form-control" id="edit_start_time" name="start_time" 
                       value="<?php echo date('Y-m-d\TH:i', strtotime($row['start_time'])); ?>" required>
            </div>
        </div>
        <div class="col-md-6">
            <div class="form-group">
                <label for="edit_end_time">End Time</label>
                <input type="datetime-local" class="form-control" id="edit_end_time" name="end_time" 
                       value="<?php echo date('Y-m-d\TH:i', strtotime($row['end_time'])); ?>" required>
            </div>
        </div>
    </div>
    
    <div class="form-group">
        <label for="edit_meeting_link">Meeting Link</label>
        <input type="text" class="form-control" id="edit_meeting_link" 
               value="<?php echo htmlspecialchars($row['meeting_link']); ?>" readonly>
    </div>
<?php else: ?>
    <div class="alert alert-danger">Class not found or you don't have permission to edit it.</div>
<?php endif; ?>