<?php
session_start();
include "../db.php";

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

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

    $stmt = $conn->prepare("SELECT * FROM admins WHERE username=? LIMIT 1");
    $stmt->bind_param("s",$username);
    $stmt->execute();
    $result = $stmt->get_result();

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

        $admin = $result->fetch_assoc();

        if(password_verify($password,$admin['password'])){
            $_SESSION['admin'] = $admin['id'];
            header("Location: dashboard.php");
            exit;
        } else {
            $error="Password Incorrect";
        }

    } else {
        $error="Username Not Found";
    }
}
?>

<!DOCTYPE html>
<html>
<head>
<title>Admin Login</title>
<meta name="viewport" content="width=device-width, initial-scale=1">

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">

<style>

body{
    background: linear-gradient(135deg,#141e30,#243b55);
    min-height:100vh;
    display:flex;
    justify-content:center;
    align-items:center;
}

.login-card{
    width:100%;
    max-width:420px;
    padding:40px;
    border-radius:20px;
}

</style>
</head>
<body>

<div class="card shadow-lg login-card">

<h3 class="text-center mb-4">🔐 Admin Login</h3>

<?php if(isset($error)){ ?>
<div class="alert alert-danger text-center">
<?php echo $error; ?>
</div>
<?php } ?>

<form method="POST">

<div class="mb-3">
<label class="form-label">Username</label>
<input name="username" class="form-control" placeholder="Enter username" required>
</div>

<div class="mb-3">
<label class="form-label">Password</label>
<input type="password" name="password" class="form-control" placeholder="Enter password" required>
</div>

<div class="d-grid">
<button type="submit" name="login" class="btn btn-primary btn-lg">
Login
</button>
</div>

</form>

</div>

</body>
</html>