<?php
session_start();
require_once(__DIR__ . "/../config/db.php");

/* 🔒 Only teacher */
if (!isset($_SESSION['teacher_id'])) {
    header("Location: /rs_lab/teacher/login.php");
    exit;
}

$error = "";
$success = "";

if ($_SERVER["REQUEST_METHOD"] === "POST") {

    $newPassword = $_POST['new_password'] ?? '';
    $confirmPassword = $_POST['confirm_password'] ?? '';

    if ($newPassword === "" || $confirmPassword === "") {
        $error = "All fields required";
    } elseif ($newPassword !== $confirmPassword) {
        $error = "Passwords do not match";
    } else {

        /* ✅ HASH PASSWORD */
        $password_hash = password_hash($newPassword, PASSWORD_DEFAULT);

        /* ✅ UPDATE */
        $stmt = $pdo->prepare("
            UPDATE teachers 
            SET password_hash = ?, must_change_password = 0
            WHERE id = ?
        ");

        $stmt->execute([
            $password_hash,
            $_SESSION['teacher_id']
        ]);

        header("Location: /rs_lab/teacher/dashboard.php");
        exit;
    }
}
?>

<!DOCTYPE html>
<html>
<head>
<title>Change Password</title>

<style>
body{
    background:#020617;
    color:white;
    font-family:Arial;

    display:flex;
    flex-direction:column;   /* 🔥 footer fix */
    min-height:100vh;
}

/* CENTER CONTENT */
.main{
    flex:1;
    display:flex;
    justify-content:center;
    align-items:center;
}

/* BOX */
.box{
    background:#0f172a;
    padding:30px;
    border-radius:12px;
    width:350px;
    box-shadow:0 0 20px rgba(0,0,0,0.5);
    animation:fadeIn 0.6s ease;
}

/* INPUTS */
input{
    width:100%;
    padding:12px;
    margin-top:12px;
    border:none;
    border-radius:8px;
    outline:none;
}

/* BUTTON */
button{
    width:100%;
    padding:12px;
    margin-top:15px;
    border:none;
    border-radius:8px;
    background:linear-gradient(90deg,#0b8c9f,#22c55e);
    color:white;
    font-weight:bold;
    cursor:pointer;
    transition:0.3s;
}

button:hover{
    transform:scale(1.03);
}

/* ERROR */
.error{
    color:#ef4444;
    margin-top:10px;
}

/* FOOTER */
.footer{
    text-align:center;
    padding:15px;
    font-size:14px;
    color:#94a3b8;
    border-top:1px solid #1e293b;
}

/* ANIMATION */
@keyframes fadeIn{
    from{opacity:0; transform:translateY(20px);}
    to{opacity:1; transform:translateY(0);}
}
</style>

</head>

<body>

<div class="main">
    <div class="box">
        <h2 style="text-align:center;">Change Password</h2>

        <?php if($error): ?>
        <p class="error"><?= $error ?></p>
        <?php endif; ?>

        <form method="POST">
            <input type="password" name="new_password" placeholder="New Password" required>
            <input type="password" name="confirm_password" placeholder="Confirm Password" required>
            <button type="submit">Update Password</button>
        </form>
    </div>
</div>

<!-- 🔥 FOOTER -->
<div class="footer">
    © 2026 RS Learning Lab • Built with ❤️ by Gokula Krishnan
</div>

</body>
</html>