<?php
require '../includes/db.php';
session_start();

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

$error = "";

if(isset($_POST['login'])){

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

    $stmt = $pdo->prepare("SELECT * FROM admin WHERE username = ?");
    $stmt->execute([$username]);
    $admin = $stmt->fetch();

    if($admin && password_verify($password, $admin['password_hash'])){

        $_SESSION['admin_id'] = $admin['id'];
        $_SESSION['admin_name'] = $admin['username'];

        header("Location: dashboard.php");
        exit;

    } else {
        $error = "Invalid login credentials";
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Admin Login</title>

<style>
body{
    margin:0;
    font-family:Inter, sans-serif;
    background:linear-gradient(135deg,#0f172a,#1e3a8a);
    height:100vh;
    display:flex;
    justify-content:center;
    align-items:center;
}

.box{
    width:340px;
    background:rgba(255,255,255,0.08);
    padding:30px;
    border-radius:18px;
    backdrop-filter:blur(12px);
    color:white;
}

h2{
    text-align:center;
    margin-bottom:20px;
}

input{
    width:100%;
    padding:12px;
    margin-bottom:12px;
    border:none;
    border-radius:10px;
    outline:none;
}

button{
    width:100%;
    padding:12px;
    background:#2563eb;
    border:none;
    color:white;
    font-weight:bold;
    border-radius:10px;
    cursor:pointer;
}

button:hover{
    background:#1d4ed8;
}

.error{
    background:#ef4444;
    padding:10px;
    border-radius:8px;
    margin-bottom:12px;
    text-align:center;
}
</style>

</head>

<body>

<div class="box">

    <h2>Admin Login</h2>

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

    <form method="POST">
        <input type="text" name="username" placeholder="Username" required>
        <input type="password" name="password" placeholder="Password" required>
        <button name="login">Login</button>
    </form>

</div>

</body>
</html>