<?php
session_start();
include '../config.php';

// যদি অলরেডি লগইন থাকে তবে প্যানেলে পাঠিয়ে দিবে
if (isset($_SESSION['user_id']) && isset($_SESSION['role']) && $_SESSION['role'] == 'admin') {
    header("Location: panel.php");
    exit();
}

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $username = $conn->real_escape_string($_POST['username']);
    $password = $_POST['password'];

    $result = $conn->query("SELECT * FROM users WHERE username='$username' AND role='admin'");
    if ($result->num_rows > 0) {
        $row = $result->fetch_assoc();
        // পাসওয়ার্ড চেক
        if (password_verify($password, $row['password'])) {
            $_SESSION['user_id'] = $row['id'];
            $_SESSION['role'] = $row['role'];
            header("Location: panel.php");
            exit();
        }
    }
    $error = "প্রবেশাধিকার সংরক্ষিত! সঠিক তথ্য দিন।";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin Login | Secure Access</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        @import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@700&family=Rajdhani:wght@600&display=swap');
        body { background: #000; background-image: radial-gradient(circle at center, #2d0000 0%, #000 100%); font-family: 'Rajdhani', sans-serif; }
        .orbitron { font-family: 'Orbitron', sans-serif; }
    </style>
</head>
<body class="flex items-center justify-center min-h-screen p-4">
    <div class="w-full max-w-sm bg-gray-950 border border-red-900/40 p-8 rounded-[2rem] shadow-2xl">
        <div class="text-center mb-8">
            <i class="fas fa-user-lock text-5xl text-red-600 mb-4"></i>
            <h2 class="text-2xl font-black orbitron text-red-600 italic">ADMIN PANEL</h2>
            <p class="text-gray-500 text-[9px] uppercase tracking-[4px]">Authentication Required</p>
        </div>

        <?php if(isset($error)): ?>
            <div class="bg-red-600/10 border border-red-600/20 text-red-500 text-xs p-3 rounded-xl text-center mb-6">
                <?php echo $error; ?>
            </div>
        <?php endif; ?>

        <form method="POST" class="space-y-5">
            <input type="text" name="username" placeholder="Admin Username" required class="w-full p-4 bg-black border border-gray-800 rounded-2xl text-white outline-none focus:border-red-600 transition-all">
            <input type="password" name="password" placeholder="Key Pass" required class="w-full p-4 bg-black border border-gray-800 rounded-2xl text-white outline-none focus:border-red-600 transition-all">
            <button type="submit" class="w-full bg-red-600 hover:bg-red-700 text-white font-black orbitron py-4 rounded-2xl transition-all shadow-lg shadow-red-600/20">
                UNLOCK SYSTEM
            </button>
        </form>
    </div>
</body>
</html>