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

// Start the session
session_start();

// Fetch student details
if (isset($_SESSION['student_id'])) {
    $student_id = $_SESSION['student_id'];

    // Fetch student data
    $stmt = $conn->prepare("SELECT * FROM students WHERE student_id = ?");
    $stmt->bind_param("i", $student_id);
    $stmt->execute();
    $result = $stmt->get_result();
    $student = $result->fetch_assoc();

    if (!$student) {
        echo "Student not found.";
        exit();
    }
} else {
    echo "No student ID found.";
    exit();
}

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = $_POST['name'];
    $regNumber = $_POST['regNumber'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $address = $_POST['address'];
    $dob = $_POST['dob'];
    $profile_picture = $_FILES['profile_picture'];

    // Validate inputs
    if (empty($name) || empty($regNumber) || empty($email) || empty($phone) || empty($address) || empty($dob)) {
        echo "All fields are required.";
        exit();
    }

    // Handle profile picture upload
    if ($profile_picture['error'] == UPLOAD_ERR_OK) {
        $upload_dir = 'uploads/profile_pictures/';
        $file_ext = pathinfo($profile_picture['name'], PATHINFO_EXTENSION);
        $file_name = $student_id . '.' . $file_ext;
        $upload_file = $upload_dir . $file_name;

        if (!move_uploaded_file($profile_picture['tmp_name'], $upload_file)) {
            echo "Error uploading profile picture.";
            exit();
        }
    } else {
        // If no new profile picture is uploaded, use the existing one
        $file_name = $student['profile_picture'];
    }

    // Update student details
    $stmt = $conn->prepare("UPDATE students SET name = ?, regNumber = ?, email = ?, phone = ?, address = ?, dob = ?, profile_picture = ? WHERE student_id = ?");
    $stmt->bind_param("sssssssi", $name, $regNumber, $email, $phone, $address, $dob, $file_name, $student_id);

    if ($stmt->execute()) {
        echo "Profile updated successfully!";
    } else {
        echo "Error: " . $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="#"> <!-- Add your CSS file -->
    <style type="text/css">
        /* form_styles.css */
        form {
            max-width: 600px;
            margin: 50px auto;
            padding: 20px;
            background: #f4f7f8;
            border-radius: 8px;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
        }

        form h2 {
            margin-bottom: 20px;
            font-size: 24px;
            font-weight: 600;
            color: #333;
        }

        label {
            display: block;
            margin-bottom: 10px;
            font-weight: 500;
            color: #666;
        }

        input[type="text"],
        input[type="email"],
        input[type="tel"],
        input[type="date"],
        textarea {
            width: 100%;
            padding: 10px;
            margin-bottom: 20px;
            border: 1px solid #ccc;
            border-radius: 4px;
            box-sizing: border-box;
        }

        input[type="file"] {
            margin-bottom: 20px;
        }

        button[type="submit"] {
            width: 100%;
            padding: 10px;
            background: #007bff;
            border: none;
            border-radius: 4px;
            color: #fff;
            font-size: 16px;
            cursor: pointer;
        }

        button[type="submit"]:hover {
            background: #0056b3;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .error {
            color: red;
            margin-bottom: 20px;
            font-size: 14px;
        }
    </style>
</head>
<body>
    <?php include 'header.php'; ?>
    <h2>Edit Profile</h2>
    <form method="post" action="" enctype="multipart/form-data">
        <label for="name">Full Name:</label>
        <input type="text" id="name" name="name" value="<?php echo htmlspecialchars($student['name']); ?>" required>

        <label for="regNumber">Registration Number:</label>
        <input type="text" id="regNumber" name="regNumber" value="<?php echo htmlspecialchars($student['regNumber']); ?>" required>

        <label for="email">Email:</label>
        <input type="email" id="email" name="email" value="<?php echo htmlspecialchars($student['email']); ?>" required>

        <label for="phone">Phone Number:</label>
        <input type="text" id="phone" name="phone" value="<?php echo htmlspecialchars($student['phone']); ?>" required>

        <label for="address">Address:</label>
        <textarea id="address" name="address" rows="4" required><?php echo htmlspecialchars($student['address']); ?></textarea>

        <label for="dob">Date of Birth:</label>
        <input type="date" id="dob" name="dob" value="<?php echo htmlspecialchars($student['dob']); ?>" required>

        <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>
