<?php
session_start();
include('db_connection.php');

/* ===============================
   AUTH CHECK
================================ */
if (!isset($_SESSION['teacher_id'])) {
    header("Location: teacher_login.php");
    exit();
}

$teacher_id = $_SESSION['teacher_id'];

/* ===============================
   FETCH PROFILE
================================ */
$sql = "SELECT 
            t.name,
            u.email
        FROM teachers t
        JOIN users u ON t.user_id = u.user_id
        WHERE t.teacher_id = ?";

$stmt = $conn->prepare($sql);

if (!$stmt) {
    die("SQL Error: " . $conn->error);
}

$stmt->bind_param("i", $teacher_id);
$stmt->execute();
$result = $stmt->get_result();
$teacher = $result->fetch_assoc();

$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>Profile</title>

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">

<style>
body { background:#f4f6f9; font-family:Arial; }

.sidebar {
    width:240px;
    height:100vh;
    background:#2c3e50;
    position:fixed;
    padding-top:20px;
}

.sidebar a {
    display:block;
    color:#ecf0f1;
    padding:12px 20px;
    text-decoration:none;
}

.sidebar a:hover { background:#34495e; }

.main {
    margin-left:240px;
    padding:20px;
}

.card-box {
    background:#fff;
    padding:20px;
    border-radius:10px;
    box-shadow:0 2px 8px rgba(0,0,0,0.05);
}
</style>
</head>

<body>

<div class="sidebar">
    <a href="teacher_dashboard.php">Dashboard</a>
    <a href="feedback_grading.php">Feedback</a>
    <a href="attendance.php">Attendance</a>
    <a href="add_homework.php">Assignments</a>
    <a href="class_timetable.php">Timetable</a>
    <a href="profile.php">Profile</a>
    <a href="logout.php">Logout</a>
</div>

<div class="main">
    <div class="card-box">
        <h4>My Profile</h4>

        <form action="process_profile.php" method="POST">

            <div class="mb-3">
                <label>Name</label>
                <input type="text" name="name"
                    class="form-control"
                    value="<?= htmlspecialchars($teacher['name']) ?>" required>
            </div>

            <div class="mb-3">
                <label>Email</label>
                <input type="email"
                    class="form-control"
                    value="<?= htmlspecialchars($teacher['email']) ?>" disabled>
            </div>

            <button class="btn btn-primary">Update Profile</button>

        </form>
    </div>
</div>

</body>
</html>