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

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Get student_id from session
    session_start();
    $studentId = $_SESSION['student_id'];
    
    // Get form data
    $name = $_POST['name'];
    $profilePicture = $_FILES['profile_picture']['name']; // Handle file upload separately
    $bio = $_POST['bio'];
    
    // Handle file upload (optional)
    if (!empty($_FILES['profile_picture']['tmp_name'])) {
        $targetDir = "uploads/"; // Directory where files will be uploaded
        $targetFile = $targetDir . basename($_FILES["profile_picture"]["name"]);
        move_uploaded_file($_FILES["profile_picture"]["tmp_name"], $targetFile);
    } else {
        // If no new picture is uploaded, keep the old one
        $stmt = $conn->prepare("SELECT profile_picture FROM profiles WHERE student_id = ?");
        $stmt->bind_param("i", $studentId);
        $stmt->execute();
        $result = $stmt->get_result();
        $row = $result->fetch_assoc();
        $profilePicture = $row['profile_picture'];
    }
    
    // Insert or update profile information
    $stmt = $conn->prepare("INSERT INTO profiles (student_id, name, profile_picture, bio) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE name=?, profile_picture=?, bio=?");
    $stmt->bind_param("issssss", $studentId, $name, $profilePicture, $bio, $name, $profilePicture, $bio);
    $stmt->execute();

    // Redirect or handle success
    echo "Profile updated successfully.";
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Update Profile</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f0f0f0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }
        .form-container {
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 500px;
        }
        .form-container h2 {
            margin-bottom: 20px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        .form-group label {
            display: block;
            margin-bottom: 5px;
        }
        .form-group input[type="text"],
        .form-group textarea {
            width: 100%;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 4px;
        }
        .form-group input[type="file"] {
            padding: 10px 0;
        }
        .form-group button {
            background-color: #34495e;
            color: white;
            padding: 10px 15px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        .form-group button:hover {
            background-color: #2c3e50;
        }
    </style>
</head>
<body>
    <div class="form-container">
        <h2>Update Profile</h2>
        <form action="update_profile.php" method="post" enctype="multipart/form-data">
            <div class="form-group">
                <label for="name">Name:</label>
                <input type="text" id="name" name="name" required>
            </div>
            <div class="form-group">
                <label for="profile_picture">Profile Picture:</label>
                <input type="file" id="profile_picture" name="profile_picture">
            </div>
            <div class="form-group">
                <label for="bio">Bio:</label>
                <textarea id="bio" name="bio" rows="4" required></textarea>
            </div>
            <div class="form-group">
                <button type="submit">Update Profile</button>
                
            </div>
        </form>
    </div>
</body>
</html>
