<?php
session_start();
require_once '../db_connect.php';

if (isset($_SESSION['teacher_logged_in'])) {
    header("Location: dashboard");
    exit;
}

$error = "";
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $email = trim($_POST['email']); 
    $password = $_POST['password'];

    $stmt = $pdo->prepare("SELECT * FROM teachers WHERE email = ? AND status = 'active'");
    $stmt->execute([$email]);
    $teacher = $stmt->fetch();

    if ($teacher) {
        if (password_verify($password, $teacher['password'])) {
            $_SESSION['teacher_logged_in'] = true;
            $_SESSION['teacher_id'] = $teacher['id'];
            $_SESSION['teacher_name'] = $teacher['name'];
            header("Location: dashboard.php");
            exit;
        } else {
            $error = "Invalid password!";
        }
    } else {
        $error = "Teacher account not found or inactive!";
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Teacher Login | Ahmad Academy</title>
    <link href="https://fonts.googleapis.com/css2?family=Outfit:wght@400;600;800&display=swap" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <style>
        :root { --theme-green: #064e3b; --theme-dark: #022c22; --theme-gold: #d4af37; }
        body { background: #f1f5f9; height: 100vh; display: flex; align-items: center; justify-content: center; font-family: 'Outfit', sans-serif; }
        .login-card { width: 100%; max-width: 420px; padding: 45px; border-radius: 25px; background: #fff; box-shadow: 0 15px 40px rgba(0,0,0,0.08); border-top: 6px solid var(--theme-green); position: relative; }
        .login-card::after { content: ''; position: absolute; top: -6px; left: 50%; transform: translateX(-50%); width: 60px; height: 6px; background: var(--theme-gold); border-radius: 0 0 10px 10px; }
        .btn-login { background: var(--theme-green); color: #fff; border: none; padding: 14px; border-radius: 12px; font-weight: 700; transition: 0.3s; }
        .btn-login:hover { background: var(--theme-dark); color: var(--theme-gold); transform: translateY(-2px); }
        .form-control { border-radius: 12px; padding: 12px 18px; border: 2px solid #f1f5f9; background: #f8fafc; font-weight: 500; }
        .form-control:focus { border-color: var(--theme-green); box-shadow: none; background: #fff; }
    </style>
</head>
<body>
    <div class="login-card fade-up">
        <div class="text-center mb-4">
            <h3 class="fw-bold text-dark mb-1">Teacher Login</h3>
            <p class="text-muted small fw-bold">Enter your credentials to access portal</p>
        </div>

        <?php if($error): ?> 
            <div class="alert alert-danger border-0 border-start border-4 border-danger py-2 small fw-bold bg-light text-danger">
                <i class="fas fa-exclamation-circle me-2"></i> <?= $error ?>
            </div> 
        <?php endif; ?>

        <form method="POST">
            <div class="mb-3">
                <label class="form-label small fw-bold text-muted text-uppercase">Email Address</label>
                <input type="email" name="email" class="form-control" placeholder="teacher@ahmadacademy.com" required>
            </div>
            <div class="mb-4">
                <label class="form-label small fw-bold text-muted text-uppercase">Password</label>
                <input type="password" name="password" class="form-control" placeholder="••••••••" required>
            </div>
            <button type="submit" class="btn btn-login w-100 shadow-sm">Login Now <i class="fas fa-sign-in-alt ms-2"></i></button>
        </form>
        
        <div class="text-center mt-4">
            <a href="../" class="text-decoration-none small fw-bold text-muted"><i class="fas fa-arrow-left me-1"></i> Back to Website</a>
        </div>
    </div>
</body>
</html>