<?php
ob_start();
require '../config/db.php';

// সেশন ডাবল স্টার্ট রোধ
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// অলরেডি লগইন থাকলে ড্যাশবোর্ডে পাঠিয়ে দেবে
if (isset($_SESSION['admin_logged_in'])) {
    header("Location: dashboard.php");
    exit;
}

if (isset($_POST['login'])) {
    $user = trim($_POST['user']);
    $pass = $_POST['pass'];
    
    $stmt = $pdo->prepare("SELECT * FROM admin WHERE username = ?");
    $stmt->execute([$user]);
    $admin = $stmt->fetch();

    // পাসওয়ার্ড ভেরিফিকেশন (Hash এবং Plain টেক্সট উভয়ই কাজ করবে)
    if ($admin && (password_verify($pass, $admin['password']) || $pass === $admin['password'])) {
        session_regenerate_id(true);
        $_SESSION['admin_logged_in'] = true;
        $_SESSION['admin_id'] = $admin['id'];
        header("Location: dashboard.php");
        exit;
    }
    $error = "Wrong Username or Password!";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin Login - Ludo Pro</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://fonts.googleapis.com/css2?family=Rajdhani:wght@600;700&display=swap" rel="stylesheet">
    <style>body{font-family:'Rajdhani',sans-serif; background:#0f172a;}</style>
</head>
<body class="flex items-center justify-center min-h-screen p-4">
    <div class="bg-slate-800 p-8 rounded-2xl w-full max-w-sm border border-slate-700 shadow-2xl">
        <div class="text-center mb-6">
            <h1 class="text-3xl font-bold text-white tracking-widest">ADMIN<span class="text-blue-500">PANEL</span></h1>
        </div>
        <?php if(isset($error)) echo "<div class='bg-red-500/10 border border-red-500/50 text-red-400 p-3 rounded-lg mb-4 text-center text-sm font-bold'>$error</div>"; ?>
        <form method="POST" class="space-y-4">
            <input type="text" name="user" placeholder="Username" class="w-full bg-slate-900 p-3 rounded-xl text-white border border-slate-600 outline-none focus:border-blue-500" required>
            <input type="password" name="pass" placeholder="Password" class="w-full bg-slate-900 p-3 rounded-xl text-white border border-slate-600 outline-none focus:border-blue-500" required>
            <button name="login" class="w-full bg-blue-600 hover:bg-blue-500 text-white py-3 rounded-xl font-bold uppercase transition shadow-lg shadow-blue-600/20">Login</button>
        </form>
    </div>
</body>
</html>