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

if (!isset($_SESSION['user_id'])) { header("Location: login.php"); exit(); }
$user_id = $_SESSION['user_id'];

// Fetch Settings Data
$set = $pdo->query("SELECT * FROM settings WHERE id = 1")->fetch();
$site_name = $set['site_name'] ?? 'LUDO PRO';
$currency = $set['currency'] ?? '৳';
$site_logo = $set['logo'] ?? ''; // ডাটাবেস থেকে লোগো ফেচ করা হলো

// ১. ডাটাবেস থেকে স্লাইডার ডাটা আনা
try {
    $stmt_slider = $pdo->prepare("SELECT * FROM sliders WHERE status = 'active' ORDER BY id DESC");
    $stmt_slider->execute();
    $db_sliders = $stmt_slider->fetchAll();
} catch (PDOException $e) { 
    $db_sliders = []; 
    error_log($e->getMessage()); 
}

// ২. যদি ডাটাবেসে স্লাইডার না থাকে তবে ডিফল্ট স্লাইডার ব্যবহার করা
if (empty($db_sliders)) {
    $db_sliders = [
        [
            'title' => 'Mega Tournament',
            'subtitle' => 'Join & Win ' . htmlspecialchars($currency) . '50,000 Prize Pool!',
            'image_url' => '', 
            'link_url' => 'tournaments.php',
            'bg_color' => 'from-violet-600 to-indigo-600',
            'btn_text' => 'Join Now'
        ],
        [
            'title' => '50% Bonus',
            'subtitle' => 'On First Deposit above ' . htmlspecialchars($currency) . '500',
            'image_url' => '',
            'link_url' => 'deposit.php',
            'bg_color' => 'from-pink-600 to-rose-500',
            'btn_text' => 'Deposit'
        ]
    ];
}

// Split Site Name for styling (Logo না থাকলে এটি কাজে লাগবে)
$name_parts = explode(' ', $site_name, 2);
$first_part = $name_parts[0];
$second_part = $name_parts[1] ?? '';

try {
    $stmt = $pdo->prepare("SELECT * FROM users WHERE id = ?");
    $stmt->execute([$user_id]);
    $user = $stmt->fetch();

    $stmt_n = $pdo->prepare("SELECT COUNT(*) FROM notifications WHERE user_id = ? AND status = 'unread'");
    $stmt_n->execute([$user_id]);
    $notif_count = $stmt_n->fetchColumn();

    $stmt_m = $pdo->prepare("SELECT * FROM matches WHERE user_id = ? ORDER BY id DESC LIMIT 3");
    $stmt_m->execute([$user_id]);
    $my_matches = $stmt_m->fetchAll();
} catch (PDOException $e) { error_log($e->getMessage()); }
?>

<!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><?= htmlspecialchars($site_name) ?> - Premium Dashboard</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">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swiper@10/swiper-bundle.min.css" />
    <style>
        body { background: #020617; color: #f1f5f9; font-family: 'Rajdhani', sans-serif; background-image: radial-gradient(circle at 50% 0%, #1e1b4b 0%, #020617 70%); -webkit-tap-highlight-color: transparent; }
        .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); }
        .nav-btn { color: #64748b; transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1); }
        .nav-btn.active { color: #3b82f6; text-shadow: 0 0 10px rgba(59, 130, 246, 0.5); }
        .swiper-pagination-bullet { background: #475569; opacity: 1; }
        .swiper-pagination-bullet-active { background: #3b82f6; width: 20px; border-radius: 5px; transition: width 0.3s; }
    </style>
</head>
<body class="pb-24 antialiased">

    <header class="p-5 flex justify-between items-center bg-slate-900/60 backdrop-blur-xl sticky top-0 z-40 border-b border-white/5">
        <div class="flex items-center gap-3 cursor-pointer" onclick="location.href='dashboard.php'">
            <div class="flex items-center gap-2">
                <?php if(!empty($site_logo)): ?>
                    <img src="assets/img/<?= htmlspecialchars($site_logo) ?>" alt="Logo" class="h-8 w-auto object-contain">
                <?php else: ?>
                    <div class="bg-gradient-to-br from-blue-600 to-indigo-600 p-1.5 rounded-lg shadow-lg">
                        <i class="fas fa-dice text-lg text-white"></i>
                    </div>
                <?php endif; ?>

                <h1 class="text-xl font-black tracking-tighter text-white uppercase">
                    <?= htmlspecialchars($first_part) ?><span class="text-blue-500"><?= htmlspecialchars($second_part) ?></span>
                </h1>
            </div>
        </div>
        
        <div class="flex items-center gap-5">
            <div class="relative cursor-pointer hover:text-white transition" onclick="location.href='notifications.php'">
                <i class="fas fa-bell text-xl text-slate-400"></i>
                <?php if($notif_count > 0): ?><span class="absolute -top-1.5 -right-1.5 w-4 h-4 bg-red-500 text-[9px] flex items-center justify-center rounded-full text-white font-bold ring-2 ring-[#020617]"><?= $notif_count ?></span><?php endif; ?>
            </div>
            <img src="assets/avatars/<?= !empty($user['avatar']) ? $user['avatar'] : 'default.png' ?>" class="w-9 h-9 rounded-full object-cover border-2 border-blue-500/30" onclick="location.href='profile.php'">
        </div>
    </header>

    <div class="max-w-md mx-auto px-5 mt-5 space-y-6">
        <div class="swiper mySwiper w-full rounded-2xl overflow-hidden shadow-2xl shadow-black/20">
            <div class="swiper-wrapper">
                <?php foreach($db_sliders as $slide): ?>
                <div class="swiper-slide" onclick="location.href='<?= $slide['link_url'] ?>'">
                    <div class="w-full h-40 bg-gradient-to-r <?= !empty($slide['bg_color']) ? $slide['bg_color'] : 'from-blue-600 to-indigo-600' ?> relative p-5 flex items-center justify-between">
                        <div class="z-10 w-2/3">
                            <h3 class="text-xl font-black text-white leading-tight mb-1"><?= htmlspecialchars($slide['title']) ?></h3>
                            <p class="text-[10px] text-white/90 mb-3"><?= htmlspecialchars($slide['subtitle']) ?></p>
                            <button class="bg-white text-slate-900 text-[10px] font-bold px-4 py-1.5 rounded-full shadow-lg"><?= !empty($slide['btn_text']) ? htmlspecialchars($slide['btn_text']) : 'Play Now' ?></button>
                        </div>
                        <?php if(!empty($slide['image_url'])): ?>
                            <img src="<?= htmlspecialchars($slide['image_url']) ?>" class="h-28 w-auto absolute right-2 bottom-2 object-contain opacity-80">
                        <?php else: ?>
                            <i class="fas fa-trophy text-6xl text-white/20 absolute -right-2 -bottom-4 rotate-12"></i>
                        <?php endif; ?>
                    </div>
                </div>
                <?php endforeach; ?>
            </div>
            <div class="swiper-pagination"></div>
        </div>

        <div class="glass-card p-6 rounded-[1.5rem] relative overflow-hidden group border border-white/10">
            <div class="absolute -right-12 -top-12 w-40 h-40 bg-blue-600/20 rounded-full blur-[50px]"></div>
            <div class="relative z-10 flex justify-between items-start">
                <div>
                    <p class="text-[10px] font-bold text-slate-400 uppercase tracking-widest mb-1">Total Balance</p>
                    <h2 class="text-4xl font-black text-white tracking-tight"><?= htmlspecialchars($currency) ?><?= number_format($user['balance'] + $user['win_balance'], 2) ?></h2>
                </div>
                <div class="bg-gradient-to-br from-blue-500/20 to-purple-500/20 p-3 rounded-xl border border-white/5"><i class="fas fa-wallet text-blue-400 text-lg"></i></div>
            </div>
            <div class="grid grid-cols-2 gap-3 mt-6 relative z-10">
                <button onclick="location.href='deposit.php'" class="bg-blue-600 hover:bg-blue-500 text-white py-3 rounded-xl font-bold text-xs uppercase tracking-wide transition-all active:scale-95 shadow-lg shadow-blue-900/20 flex items-center justify-center gap-2"><i class="fas fa-plus-circle"></i> Add Cash</button>
                <button onclick="location.href='withdraw.php'" class="bg-slate-800 hover:bg-slate-700 text-slate-200 py-3 rounded-xl font-bold text-xs uppercase tracking-wide transition-all active:scale-95 border border-white/5 flex items-center justify-center gap-2"><i class="fas fa-arrow-down"></i> Withdraw</button>
            </div>
        </div>

        <section>
            <div class="flex justify-between items-center mb-3">
                <h3 class="text-xs font-bold text-slate-500 uppercase tracking-widest">Play & Earn</h3>
            </div>
            <div class="grid grid-cols-2 gap-3">
                <div onclick="location.href='refer.php'" class="glass-card p-4 rounded-2xl border-l-4 border-teal-500 hover:bg-slate-800/40 transition-all active:scale-95 cursor-pointer relative overflow-hidden">
                    <div class="bg-teal-500/10 w-10 h-10 rounded-lg flex items-center justify-center text-teal-400 text-lg mb-3"><i class="fas fa-gift"></i></div>
                    <h4 class="text-sm font-bold text-white">Refer & Earn</h4>
                    <p class="text-[10px] text-slate-400 mt-0.5">Get <?= htmlspecialchars($currency) ?>50 Bonus</p>
                    <i class="fas fa-chevron-right text-slate-600 absolute right-3 top-1/2 -translate-y-1/2 text-xs"></i>
                </div>
                <div onclick="location.href='tournaments.php'" class="glass-card p-4 rounded-2xl border-l-4 border-purple-500 hover:bg-slate-800/40 transition-all active:scale-95 cursor-pointer relative overflow-hidden">
                    <div class="bg-purple-500/10 w-10 h-10 rounded-lg flex items-center justify-center text-purple-400 text-lg mb-3"><i class="fas fa-trophy"></i></div>
                    <h4 class="text-sm font-bold text-white">Tournament</h4>
                    <p class="text-[10px] text-slate-400 mt-0.5">Big Rewards</p>
                    <i class="fas fa-chevron-right text-slate-600 absolute right-3 top-1/2 -translate-y-1/2 text-xs"></i>
                </div>
                <div onclick="location.href='playfriend.php'" class="glass-card p-4 rounded-2xl border-l-4 border-emerald-500 hover:bg-slate-800/40 transition-all active:scale-95 cursor-pointer relative overflow-hidden">
                    <div class="bg-emerald-500/10 w-10 h-10 rounded-lg flex items-center justify-center text-emerald-400 text-lg mb-3"><i class="fas fa-user-friends"></i></div>
                    <h4 class="text-sm font-bold text-white">Play Friends</h4>
                    <p class="text-[10px] text-slate-400 mt-0.5">Private Room</p>
                    <i class="fas fa-chevron-right text-slate-600 absolute right-3 top-1/2 -translate-y-1/2 text-xs"></i>
                </div>
                <div onclick="location.href='game.php?mode=bot'" class="glass-card p-4 rounded-2xl border-l-4 border-orange-500 hover:bg-slate-800/40 transition-all active:scale-95 cursor-pointer relative overflow-hidden">
                    <div class="bg-orange-500/10 w-10 h-10 rounded-lg flex items-center justify-center text-orange-400 text-lg mb-3"><i class="fas fa-robot"></i></div>
                    <h4 class="text-sm font-bold text-white">Computer</h4>
                    <p class="text-[10px] text-slate-400 mt-0.5">Practice Mode</p>
                    <i class="fas fa-chevron-right text-slate-600 absolute right-3 top-1/2 -translate-y-1/2 text-xs"></i>
                </div>
            </div>
        </section>

        <section class="pb-6">
            <div class="flex justify-between items-center mb-3">
                <h3 class="text-xs font-bold text-slate-500 uppercase tracking-widest">Recent Activity</h3>
                <a href="history.php" class="text-[10px] font-bold text-blue-500 hover:text-blue-400 transition">VIEW ALL</a>
            </div>
            <div class="space-y-2.5">
                <?php if(empty($my_matches)): ?>
                    <div class="glass-card rounded-xl p-8 text-center border-dashed border-2 border-slate-700">
                        <i class="fas fa-history text-3xl text-slate-600 mb-2"></i>
                        <p class="text-xs text-slate-400">No match history yet.</p>
                    </div>
                <?php else: ?>
                    <?php foreach($my_matches as $m): ?>
                    <div class="glass-card p-3.5 rounded-xl flex justify-between items-center group hover:bg-slate-800/30 transition">
                        <div class="flex items-center gap-3">
                            <div class="w-9 h-9 bg-slate-800 rounded-lg flex items-center justify-center text-base shadow-inner"><?= $m['result'] == 'Win' ? '🏆' : '🎲' ?></div>
                            <div>
                                <h5 class="text-xs font-bold text-slate-200"><?= ucfirst($m['mode']) ?> Match</h5>
                                <p class="text-[9px] text-slate-500 font-medium"><?= date('d M, h:i A', strtotime($m['created_at'])) ?></p>
                            </div>
                        </div>
                        <div class="text-right">
                            <p class="text-xs font-bold <?= $m['result'] == 'Win' ? 'text-green-400' : 'text-red-400' ?>"><?= $m['result'] == 'Win' ? '+' : '-' ?><?= htmlspecialchars($currency) ?><?= number_format($m['amount']) ?></p>
                            <p class="text-[9px] font-bold <?= $m['result'] == 'Win' ? 'text-green-500/50' : 'text-red-500/50' ?> uppercase tracking-wide"><?= $m['result'] ?></p>
                        </div>
                    </div>
                    <?php endforeach; ?>
                <?php endif; ?>
            </div>
        </section>
    </div>

    <nav class="fixed bottom-0 left-0 right-0 max-w-md mx-auto bg-[#0f172a]/90 backdrop-blur-xl border-t border-white/5 px-6 py-2 flex justify-between items-center z-50">
        <a href="dashboard.php" class="nav-btn active flex flex-col items-center gap-1 p-2"><i class="fas fa-home text-lg"></i><span class="text-[9px] font-bold uppercase">Home</span></a>
        <a href="tournaments.php" class="nav-btn flex flex-col items-center gap-1 p-2"><i class="fas fa-trophy text-lg"></i><span class="text-[9px] font-bold uppercase">Events</span></a>
        <div class="relative -top-6"><div onclick="location.href='tournaments.php'" class="bg-gradient-to-br from-blue-500 to-blue-600 w-14 h-14 rounded-2xl flex items-center justify-center shadow-2xl border-4 border-[#020617] cursor-pointer"><i class="fas fa-gamepad text-white text-xl"></i></div></div>
        <a href="wallet.php" class="nav-btn flex flex-col items-center gap-1 p-2"><i class="fas fa-wallet text-lg"></i><span class="text-[9px] font-bold uppercase">Wallet</span></a>
        <a href="profile.php" class="nav-btn flex flex-col items-center gap-1 p-2"><i class="fas fa-user text-lg"></i><span class="text-[9px] font-bold uppercase">Profile</span></a>
    </nav>

    <script src="https://cdn.jsdelivr.net/npm/swiper@10/swiper-bundle.min.js"></script>
    <script>
        new Swiper(".mySwiper", {
            spaceBetween: 10,
            centeredSlides: true,
            autoplay: { delay: 3500, disableOnInteraction: false },
            pagination: { el: ".swiper-pagination", clickable: true },
            effect: 'fade'
        });
    </script>
</body>
</html>