<?php
include 'db_connection.php'; // Include your database connection

session_start();
$studentId = $_SESSION['student_id'];

// Fetch existing profile information
$stmt = $conn->prepare("SELECT * FROM profiles WHERE student_id = ?");
$stmt->bind_param("i", $studentId);
$stmt->execute();
$result = $stmt->get_result();
$profile = $result->fetch_assoc();
$stmt->close();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Update profile information
    $name = $_POST['name'];
    $bio = $_POST['bio'];
    
    // Handle file upload
    $profilePicturePath = $profile['profile_picture']; // Default to current picture
    if (isset($_FILES['profile_picture']) && $_FILES['profile_picture']['error'] === UPLOAD_ERR_OK) {
        $fileTmpPath = $_FILES['profile_picture']['tmp_name'];
        $fileName = $_FILES['profile_picture']['name'];
        $fileSize = $_FILES['profile_picture']['size'];
        $fileType = $_FILES['profile_picture']['type'];
        $fileNameCmps = explode('.', $fileName);
        $fileExtension = strtolower(end($fileNameCmps));

        $allowedExtensions = array('jpg', 'jpeg', 'png', 'gif');

        if (in_array($fileExtension, $allowedExtensions)) {
            $uploadFileDir = __DIR__ . '/uploads/'; // Absolute path to the uploads directory
            $dest_path = $uploadFileDir . $fileName;

            if (move_uploaded_file($fileTmpPath, $dest_path)) {
                // Successfully uploaded file
                $profilePicturePath = $fileName; // Relative path for storing in DB
            } else {
                echo 'There was an error moving the uploaded file.';
            }
        } else {
            echo 'Upload failed. Allowed file types: jpg, jpeg, png, gif.';
        }
    }

    // Update profile information in the database
    $stmt = $conn->prepare("UPDATE profiles SET name = ?, bio = ?, profile_picture = ? WHERE student_id = ?");
    $stmt->bind_param("sssi", $name, $bio, $profilePicturePath, $studentId);
    if ($stmt->execute()) {
        echo 'Profile updated successfully!';
    } else {
        echo 'Error updating profile: ' . $stmt->error;
    }
    $stmt->close();
    $conn->close();
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Edit Profile</title>
    <link rel="stylesheet" href="path/to/your/styles.css"> <!-- Ensure CSS path is correct -->
</head>
<body>
    <h1>Edit Profile</h1>
    <form action="edit_profile.php" method="post" enctype="multipart/form-data">
        <label for="name">Name:</label>
        <input type="text" id="name" name="name" value="<?php echo htmlspecialchars($profile['name']); ?>" required>
        
        <label for="bio">Bio:</label>
        <textarea id="bio" name="bio"><?php echo htmlspecialchars($profile['bio']); ?></textarea>
        
        <label for="profile_picture">Profile Picture:</label>
        <input type="file" id="profile_picture" name="profile_picture">
        
        <button type="submit">Update Profile</button>
    </form>
</body>
</html>
