<?php
// ======================================
// DemoBank Admin Login (Secure & Role-Checked)
// ======================================

ini_set('display_errors', 1);
error_reporting(E_ALL);

session_start();

require_once __DIR__ . '/../includes/db.php';
require_once __DIR__ . '/../includes/functions.php';

// Redirect if already logged in as admin
if (isset($_SESSION['admin_id'])) {
    header("Location: index.php");
    exit;
}

$error = "";

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

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

    if ($email === "" || $password === "") {
        $error = "Please fill in all fields.";
    } else {

        $conn = db();

        // Fetch user where role = 'admin'
        $stmt = $conn->prepare("SELECT id, password, first_name, last_name FROM users WHERE email = ? AND role='admin' LIMIT 1");

        if (!$stmt) {
            $error = "Database error: " . $conn->error;
        } else {

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

            if ($stmt->num_rows === 1) {

                $stmt->bind_result($admin_id, $hashed_password, $first_name, $last_name);
                $stmt->fetch();

                if (password_verify($password, $hashed_password)) {
                    // Successful login
                    $_SESSION['admin_id'] = $admin_id;
                    $_SESSION['admin_name'] = $first_name . ' ' . $last_name;
                    $_SESSION['admin_email'] = $email;

                    header("Location: index.php");
                    exit;
                } else {
                    $error = "Invalid email or password.";
                }

            } else {
                $error = "Invalid email or password.";
            }

            $stmt->close();
        }
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Admin Login - Lombard Odier</title>
<style>
body { margin:0; font-family:Arial,sans-serif; background:linear-gradient(135deg,#0f172a,#1e293b); height:100vh; display:flex; justify-content:center; align-items:center; }
.login-box { width:380px; background:#fff; padding:35px; border-radius:12px; box-shadow:0 20px 60px rgba(0,0,0,0.4); }
.login-logo { text-align:center; font-size:22px; font-weight:bold; color:#1e293b; margin-bottom:20px; }
.login-title { text-align:center; font-size:18px; margin-bottom:25px; color:#475569; }
input { width:100%; padding:12px; margin:10px 0; border:1px solid #cbd5e1; border-radius:8px; outline:none; transition:0.2s; }
input:focus { border-color:#2563eb; box-shadow:0 0 0 3px rgba(37,99,235,0.2); }
button { width:100%; padding:12px; background:#2563eb; color:white; border:none; border-radius:8px; font-weight:bold; cursor:pointer; margin-top:10px; transition:0.2s; }
button:hover { background:#1d4ed8; }
.error { background:#fee2e2; color:#b91c1c; padding:10px; border-radius:8px; margin-bottom:10px; font-size:14px; text-align:center; }
.footer-text { text-align:center; margin-top:15px; font-size:12px; color:#94a3b8; }
</style>
</head>
<body>

<div class="login-box">
    <div class="login-logo">Lombard Odier Admin</div>
    <div class="login-title">Secure Access Portal</div>

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

    <form method="POST">
        <input type="email" name="email" placeholder="Admin Email" required>
        <input type="password" name="password" placeholder="Password" required>
        <button type="submit">Sign In</button>
    </form>

    <div class="footer-text">
        © <?= date('Y') ?> DemoBank Secure System
    </div>
</div>

</body>
</html>