<?php
if (session_status() === PHP_SESSION_NONE) { session_start(); }
require 'config/db.php';

// ডাটাবেস থেকে সেটিংস্ ডাটা তুলে আনা
$set = $pdo->query("SELECT * FROM settings WHERE id = 1")->fetch();

// সাইট নামকে দুই ভাগে ভাগ করা
$site_name = $set['site_name'] ?? 'LUDO PRO';
$name_parts = explode(' ', $site_name, 2);
$first_part = $name_parts[0];
$second_part = $name_parts[1] ?? '';

// যদি ইউজার ইতিমধ্যে লগইন করা থাকে তবে ড্যাশবোর্ডে রিডাইরেক্ট করুন
if (isset($_SESSION['user_id'])) {
    header("Location: dashboard.php");
    exit();
}

$error = '';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $email = trim($_POST['email']);
    $password = $_POST['password'];

    if (empty($email) || empty($password)) {
        $error = "ইমেইল এবং পাসওয়ার্ড প্রয়োজন!";
    } else {
        $stmt = $pdo->prepare("SELECT * FROM users WHERE email = ?");
        $stmt->execute([$email]);
        $user = $stmt->fetch(PDO::FETCH_ASSOC);

        if ($user && password_verify($password, $user['password'])) {
            $_SESSION['user_id'] = $user['id'];
            $_SESSION['username'] = $user['username'];
            $_SESSION['role'] = $user['role'];

            if ($user['role'] === 'admin') {
                header("Location: admin.php");
            } else {
                header("Location: dashboard.php");
            }
            exit();
        } else {
            $error = "ভুল ইমেইল অথবা পাসওয়ার্ড!";
        }
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>Login - <?= htmlspecialchars($site_name) ?></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">
    
    <link href="https://fonts.googleapis.com/css2?family=Rajdhani:wght@500;600;700&display=swap" rel="stylesheet">

    <style>
        body { 
            background: #020617; 
            color: #f1f5f9; 
            font-family: 'Rajdhani', sans-serif;
            background-image: radial-gradient(circle at 50% 0%, #1e1b4b 0%, #020617 70%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        .glass-card { 
            background: rgba(30, 41, 59, 0.4); 
            backdrop-filter: blur(12px); 
            border: 1px solid rgba(255,255,255,0.05); 
            box-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.2);
        }
        .input-group {
            background: rgba(15, 23, 42, 0.6);
            border: 1px solid rgba(255,255,255,0.05);
            transition: all 0.3s ease;
        }
        .input-group:focus-within {
            border-color: #3b82f6;
            box-shadow: 0 0 15px rgba(59, 130, 246, 0.2);
        }
    </style>
</head>
<body class="p-4">

    <div class="fixed top-0 left-0 w-full h-full overflow-hidden -z-10 pointer-events-none">
        <div class="absolute top-[-10%] left-[-10%] w-64 h-64 bg-blue-600/20 rounded-full blur-[80px]"></div>
        <div class="absolute bottom-[-10%] right-[-10%] w-64 h-64 bg-indigo-600/20 rounded-full blur-[80px]"></div>
    </div>

    <div class="w-full max-w-md">
        
        <div class="text-center mb-8">
            <div class="inline-flex items-center justify-center mb-4">
                <?php if(!empty($set['logo'])): ?>
                    <img src="../assets/img/<?= $set['logo'] ?>" alt="Logo" class="h-16 w-auto">
                <?php else: ?>
                    <div class="w-16 h-16 bg-gradient-to-br from-blue-600 to-indigo-600 rounded-2xl shadow-lg shadow-blue-500/20 transform rotate-3 flex items-center justify-center">
                        <i class="fas fa-dice text-3xl text-white"></i>
                    </div>
                <?php endif; ?>
            </div>
            <h1 class="text-4xl font-black tracking-tighter text-white uppercase">
                <?= htmlspecialchars($first_part) ?><span class="text-blue-500"><?= htmlspecialchars($second_part) ?></span>
            </h1>
            <p class="text-slate-400 text-sm font-medium mt-1">Play, Win & Earn Real Cash</p>
        </div>

        <div class="glass-card p-8 rounded-[2rem] relative overflow-hidden">
            <h2 class="text-2xl font-bold text-white mb-6 text-center">Welcome Back!</h2>

            <?php if($error): ?>
                <div class="bg-red-500/10 border border-red-500/20 text-red-400 p-3 mb-6 rounded-xl text-xs font-bold flex items-center gap-2 animate-pulse">
                    <i class="fas fa-exclamation-circle"></i>
                    <?= $error ?>
                </div>
            <?php endif; ?>

            <form action="" method="POST" class="space-y-5">
                <div>
                    <label class="block text-xs font-bold text-slate-400 uppercase tracking-wider mb-2 ml-1">Email Address</label>
                    <div class="input-group flex items-center rounded-xl px-4 py-3">
                        <i class="fas fa-envelope text-slate-500 mr-3 text-lg"></i>
                        <input type="email" name="email" required 
                            class="bg-transparent border-none outline-none text-white w-full placeholder-slate-600 font-medium" 
                            placeholder="name@example.com">
                    </div>
                </div>

                <div>
                    <label class="block text-xs font-bold text-slate-400 uppercase tracking-wider mb-2 ml-1">Password</label>
                    <div class="input-group flex items-center rounded-xl px-4 py-3">
                        <i class="fas fa-lock text-slate-500 mr-3 text-lg"></i>
                        <input type="password" name="password" required 
                            class="bg-transparent border-none outline-none text-white w-full placeholder-slate-600 font-medium" 
                            placeholder="••••••••">
                    </div>
                    <div class="text-right mt-2">
                        <a href="#" class="text-[11px] font-bold text-blue-500 hover:text-blue-400 transition">Forgot Password?</a>
                    </div>
                </div>

                <button type="submit" class="w-full bg-gradient-to-r from-blue-600 to-indigo-600 hover:from-blue-500 hover:to-indigo-500 text-white py-3.5 rounded-xl font-bold text-sm uppercase tracking-wider shadow-lg shadow-blue-900/20 transition-all active:scale-95 flex items-center justify-center gap-2 mt-2">
                    <span>Login Securely</span>
                    <i class="fas fa-arrow-right"></i>
                </button>
            </form>

            <div class="mt-8 text-center border-t border-white/5 pt-6">
                <p class="text-slate-400 text-sm">
                    New to <?= htmlspecialchars($site_name) ?>? 
                    <a href="register.php" class="text-blue-500 font-bold hover:text-blue-400 transition ml-1">Create Account</a>
                </p>
            </div>
        </div>
        
        <p class="text-center text-[10px] text-slate-600 mt-6 font-bold uppercase tracking-widest">
            &copy; <?= date('Y') ?> <?= htmlspecialchars($site_name) ?>. Secure Gaming.
        </p>
    </div>
</body>
</html>