<?php
session_start();

/* DB CONNECTION */
$conn = new mysqli("localhost","root","","rs_lab");

if($conn->connect_error){
    die("DB Connection Failed");
}

$error = "";

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

$username = trim($_POST['username'] ?? '');
$password = trim($_POST['password'] ?? '');

if($username=="" || $password==""){
$error = "Username and password required";
}

else{

$stmt = $conn->prepare("
SELECT id,username,password,temp_password
FROM teachers
WHERE username=?
LIMIT 1
");

$stmt->bind_param("s",$username);
$stmt->execute();

$res = $stmt->get_result();

if($res && $res->num_rows==1){

$teacher = $res->fetch_assoc();

$loginOk = false;

// temp password
if (!empty($teacher['temp_password']) && $password === $teacher['temp_password']) {
    $loginOk = true;
}

// normal password (plain text)
if (!empty($teacher['password']) && $password === $teacher['password']) {
    $loginOk = true;
}
// Case 2: hashed password
if (!empty($teacher['password']) && password_verify($password, $teacher['password'])) {
    $loginOk = true;
}

if ($loginOk) {

    session_regenerate_id(true);

    // clear institution session
    unset($_SESSION['institution_id']);
    unset($_SESSION['institution_type']);
    unset($_SESSION['institution_name']);

    // set teacher session
    $_SESSION['teacher_id']       = $teacher['id'];
    $_SESSION['teacher_username'] = $teacher['username'];
    $_SESSION['role']             = 'teacher';

    // redirect
    if ((int)$teacher['must_change_password'] === 1) {
        header("Location: /rs_lab/teacher/change_password.php");
    } else {
        header("Location: /rs_lab/teacher_dashboard.php");
    }

    exit;
}

session_regenerate_id(true);

/* clear institution session */
unset($_SESSION['institution_id']);
unset($_SESSION['institution_name']);
unset($_SESSION['institution_type']);

/* teacher session */
$_SESSION['teacher_id'] = $teacher['id'];
$_SESSION['teacher_username'] = $teacher['username'];
$_SESSION['role'] = "teacher";

/* DEBUG CHECK */
header("Location: /rs_lab/school/dashboard.php");
exit;

}

}

$error="Invalid login credentials";

}
?>

<!DOCTYPE html>
<html>
<head>
<title>Teacher Login | RS Learning Lab</title>

<style>

body{
background:radial-gradient(circle at top,#020617,#0f172a);
font-family:Arial;
color:#e5e7eb;
display:flex;
align-items:center;
justify-content:center;
height:100vh;
}

.card{
background:#020617;
padding:40px;
border-radius:20px;
width:360px;
box-shadow:0 0 30px rgba(0,0,0,.8)
}

input{
width:100%;
padding:12px;
margin-top:12px;
border-radius:10px;
border:none;
background:#0f172a;
color:#e5e7eb
}

button{
width:100%;
margin-top:20px;
padding:12px;
border-radius:999px;
border:none;
font-weight:bold;
background:linear-gradient(135deg,#2563eb,#22c55e);
color:#020617;
cursor:pointer
}

.error{
color:#ef4444;
text-align:center;
margin-top:10px
}

</style>

</head>

<body>

<div class="card">

<h2 style="text-align:center">Teacher Login</h2>

<form method="POST">

<input type="text" name="username" placeholder="Username" required>

<input type="password" name="password" placeholder="Password" required>

<button type="submit">Login</button>

</form>

<?php if($error): ?>

<div class="error">

<?= htmlspecialchars($error) ?>

</div>

<?php endif; ?>

</div>

</body>
</html>