<?php
session_start();

$db_path = __DIR__ . '/includes/db.php'; 
if (file_exists($db_path)) {
    require $db_path;
} else {
    $conn = new mysqli('localhost', 'root', '', 'bajipari');
}

$settings = [];
if (isset($conn) && !$conn->connect_error) {
    $res = $conn->query("SELECT * FROM settings WHERE id=1");
    if ($res && $res->num_rows > 0) {
        $settings = $res->fetch_assoc();
    }
}
$site_name = !empty($settings['site_name']) ? $settings['site_name'] : 'Bet365';

$sliders = isset($conn) && !$conn->connect_error ? $conn->query("SELECT * FROM sliders WHERE status='active' ORDER BY sort_order ASC") : false;

$is_logged_in = isset($_SESSION['user_id']);
$balance = 0.00;
if ($is_logged_in && isset($conn) && !$conn->connect_error) {
    $uid = $_SESSION['user_id'];
    $u_res = $conn->query("SELECT balance FROM users WHERE id=$uid");
    if($u_res && $u_res->num_rows > 0){
        $balance = $u_res->fetch_assoc()['balance'];
    }
}

$current_page = basename($_SERVER['PHP_SELF']);
?>

<!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><?php echo $site_name; ?></title>
    
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swiper@10/swiper-bundle.min.css" />
    <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700;900&display=swap" rel="stylesheet">

    <style>
        body { 
            font-family: 'Roboto', sans-serif;
            background-color: #126E51; 
            color: #ffffff;
            padding-bottom: 70px;
            -webkit-tap-highlight-color: transparent;
            user-select: none;
        }

        .header-bg {
            background-color: #126E51; 
            border-bottom: 1px solid #0e5640;
        }
        
        .custom-hamburger {
            display: flex; flex-direction: column; justify-content: center; gap: 5px; width: 24px; cursor: pointer;
        }
        .ham-line { display: block; height: 2px; background-color: #ffffff; border-radius: 4px; }
        .line-1 { width: 100%; } .line-2 { width: 100%; } .line-3 { width: 60%; } 

        .brand-icon-box {
            background: white; border-radius: 4px; width: 24px; height: 24px;
            display: flex; align-items: center; justify-content: center; margin-right: 6px;
        }

        .swiper-pagination-bullet {
            width: 6px; height: 6px; background: rgba(255,255,255,0.4); border-radius: 50%;
            opacity: 1; margin: 0 3px !important; transition: all 0.3s;
        }
        .swiper-pagination-bullet-active { background: #FFDF1B; width: 16px; border-radius: 4px; } 
        
        .sticky-cat-nav {
            position: sticky; top: 60px; z-index: 40;
            background-color: #126e51;
            padding: 10px 0;
            border-bottom: 1px solid #333;
        }

        .cat-scroll::-webkit-scrollbar { display: none; }
        .cat-item { 
            min-width: 70px; display: flex; flex-direction: column; align-items: center; gap: 6px; text-decoration: none;
            transition: all 0.2s;
        }
        
        .cat-icon-box {
            width: 45px; height: 45px; border-radius: 8px;
            display: flex; align-items: center; justify-content: center;
            font-size: 20px; color: #ffdf1b; 
            background: #126e51 !important; transition: all 0.2s;
            border: 2px solid #ffdf1b;
        }
        
        .cat-item:active .cat-icon-box, .cat-item.active .cat-icon-box { 
            border-color: #FFDF1B; color: #FFDF1B; background: #2A2A2A !important;
            transform: scale(0.95);
        }
        .cat-item.active span { color: white; font-weight: bold; }

        .game-card-new {
            background: #126e51; 
            border-radius: 8px;
            overflow: hidden;
            display: flex; flex-direction: column; align-items: center;
            padding: 6px; 
            border: 1px solid #FFDF1B;
            position: relative;
            transition: all 0.15s ease-out;
        }
        
        .game-card-new:active { 
            transform: scale(0.95); 
            border-color: #FFDF1B;
            box-shadow: 0 0 10px rgba(255, 223, 27, 0.4);
        }

        .game-img-wrapper {
            width: 100%; aspect-ratio: 1/1; margin-bottom: 6px; position: relative; overflow: hidden; border-radius: 6px;
        }
        .game-img-wrapper img {
            width: 100%; height: 100%; object-fit: cover; object-position: center;
            box-shadow: 0 4px 10px rgba(0,0,0,0.5);
        }
        .game-title-text {
            font-size: 10px; color: #ccc; text-align: center;
            white-space: nowrap; overflow: hidden; text-overflow: ellipsis; width: 100%; font-weight: 500;
        }

        .cat-slider-container { padding-bottom: 30px; }
        .cat-slider-container .swiper-pagination { bottom: 0px !important; }

        .view-all-link {
            font-size: 11px; color: #FFDF1B; font-weight: bold; 
            display: flex; align-items: center; gap: 4px;
            padding: 4px 8px;
        }
        .view-all-link:active { opacity: 0.7; }

        .bottom-nav { background: #126E51; border-top: 1px solid #0e5640; } 
        .nav-link {
            display: flex; flex-direction: column; align-items: center; justify-content: center;
            color: #a4d6c7; height: 100%; width: 25%; transition: all 0.2s; border-radius: 8px;
        }
        .nav-link.active {
            background-color: #0e5640; color: #FFDF1B; margin: 5px; width: calc(25% - 10px); height: calc(100% - 10px);
        }
        .home-dot {
            position: absolute; bottom: 2px; width: 6px; height: 6px; background-color: #FFDF1B; border-radius: 50%; z-index: 10;
        }

        .guest-bottom-nav {
            background: #333333; border-top: 1px solid #444; height: 60px;
            display: flex; position: fixed; bottom: 0; left: 0; width: 100%; z-index: 50;
        }
        .guest-nav-lang {
            width: 30%; background: #126E51; display: flex; align-items: center; justify-content: center; gap: 5px; font-size: 12px; font-weight: bold; color: #ccc; cursor: pointer;
        }
        .guest-nav-btn {
            width: 35%; display: flex; align-items: center; justify-content: center; font-size: 14px; font-weight: bold; cursor: pointer;
        }
        .guest-signup { background-color: #FFDF1B; color: black; } 
        .guest-login { background-color: #126E51; color: white; } 
        
        /* ফিক্সড: পতাকার জন্য সাদা বর্ডার যোগ করা হয়েছে */
        .flag-circle {
            width: 18px; height: 18px; background: #006a4e; border-radius: 50%; position: relative; display: inline-block;
            border: 1px solid #ffffff; 
        }
        .flag-circle::after {
            content: ''; width: 8px; height: 8px; background: #f42a41; border-radius: 50%; position: absolute; top: 4px; left: 4px;
        }

        .goog-te-banner-frame.skiptranslate, iframe.skiptranslate { display: none !important; }
        body { top: 0px !important; }
    </style>
</head>
<body>

    <header class="header-bg sticky top-0 z-50 px-3 py-3 flex items-center justify-between h-[60px]">
        <div onclick="toggleSidebar()" class="custom-hamburger active:opacity-60">
            <span class="ham-line line-1"></span>
            <span class="ham-line line-2"></span>
            <span class="ham-line line-3"></span>
        </div>

        <div class="flex items-center gap-2">
            <span class="text-white font-bold italic text-xl tracking-tight">
                <?php 
                    $formatted_name = str_replace('365', '<span class="text-[#FFDF1B]">365</span>', $site_name);
                    echo $formatted_name; 
                ?>
            </span>
            <div class="flag-circle"></div>
        </div>

        <div class="flex items-center gap-3">
            <?php if($is_logged_in): ?>
                <span class="text-[#FFDF1B] text-xs font-bold">৳<?php echo $balance; ?></span>
            <?php else: ?>
                <a href="player/login.php" class="text-[#FFDF1B] text-xs font-bold border border-[#FFDF1B]/50 px-2 py-1 rounded active:bg-[#FFDF1B] active:text-black transition">Login</a>
            <?php endif; ?>
            
            <a href="player/support_chat.php" class="flex flex-col items-center text-[#FFDF1B] active:scale-90 transition">
                <i class="fas fa-headset text-lg"></i>
                <span class="text-[8px] font-bold mt-[-2px]">Chat Us</span>
            </a>
        </div>
    </header>

    <div class="mt-0">
        <div class="swiper mainSlider w-full pb-6 relative">
            <div class="swiper-wrapper">
                <?php if($sliders && $sliders->num_rows > 0): ?>
                    <?php while($slide = $sliders->fetch_assoc()): ?>
                        <div class="swiper-slide px-3 pt-3">
                            <img src="<?php echo $slide['image_path']; ?>" class="w-full rounded-lg shadow-lg">
                        </div>
                    <?php endwhile; ?>
                <?php else: ?>
                    <div class="swiper-slide px-3 pt-3"><img src="https://placehold.co/600x250/222/FFDF1B?text=WELCOME+BONUS" class="w-full rounded-lg"></div>
                <?php endif; ?>
            </div>
            <div class="swiper-pagination !bottom-0"></div>
        </div>
    </div>

    <div class="px-3">
        <div class="bg-[#FFDF1B] text-black rounded px-2 py-1.5 flex items-center gap-2 mb-2">
            <i class="fas fa-bullhorn"></i>
            <marquee class="text-xs font-bold">
                <?php echo $settings['marquee_text'] ?? 'বোনাস ৫% টাকা পর্যন্ত! এখনই ডিপোজিট করুন।'; ?>
            </marquee>
        </div>
    </div>

    <?php 
    $nav_cats = $conn->query("SELECT * FROM front_categories WHERE status=1 ORDER BY priority ASC");
    ?>
    <div class="sticky-cat-nav px-2">
        <div class="flex overflow-x-auto cat-scroll gap-3 px-1">
            <a href="index.php" class="cat-item active" onclick="activateCat(this)">
                <div class="cat-icon-box"><i class="fas fa-home"></i></div>
                <span class="text-[10px] text-gray-400 uppercase tracking-wide">Home</span>
            </a>
            
            <?php 
            if($nav_cats->num_rows > 0):
                while($nc = $nav_cats->fetch_assoc()):
                    $icon = 'fa-gamepad';
                    $n = strtolower($nc['name']);
                    if(strpos($n, 'live') !== false) $icon = 'fa-video';
                    elseif(strpos($n, 'slot') !== false) $icon = 'fa-dice';
                    elseif(strpos($n, 'sport') !== false) $icon = 'fa-futbol';
                    elseif(strpos($n, 'crash') !== false) $icon = 'fa-rocket';
                    elseif(strpos($n, 'hot') !== false) $icon = 'fa-fire';
            ?>
            <a href="#cat-<?php echo $nc['id']; ?>" class="cat-item" onclick="activateCat(this)">
                <div class="cat-icon-box"><i class="fas <?php echo $icon; ?>"></i></div>
                <span class="text-[10px] text-gray-400 uppercase tracking-wide"><?php echo $nc['name']; ?></span>
            </a>
            <?php endwhile; endif; ?>
        </div>
    </div>

    <div class="px-3 mt-4 space-y-8">
        <?php 
        $cats_content = $conn->query("SELECT * FROM front_categories WHERE status=1 ORDER BY priority ASC");

        if($cats_content->num_rows > 0):
            while($cat = $cats_content->fetch_assoc()):
                $cat_id = $cat['id'];
                
                $games_query = $conn->query("
                    SELECT g.* FROM front_category_games fcg 
                    JOIN games g ON fcg.game_uid = g.game_uid 
                    WHERE fcg.category_id = $cat_id
                    ORDER BY fcg.id DESC LIMIT 36
                ");

                if($games_query->num_rows > 0):
                    $games_list = [];
                    while($row = $games_query->fetch_assoc()){
                        $games_list[] = $row;
                    }
                    $game_chunks = array_chunk($games_list, 12);
        ?>
            
            <div id="cat-<?php echo $cat_id; ?>" class="scroll-mt-24 border-b border-[#333] pb-4">
                
                <div class="flex items-center justify-between mb-3 px-1">
                    <div class="flex items-center">
                        <div class="w-1 h-4 bg-[#FFDF1B] mr-2"></div>
                        <h2 class="text-white font-bold text-sm uppercase">
                            <?php echo htmlspecialchars($cat['name']); ?>
                        </h2>
                    </div>
                    <a href="player/all_category_games.php?cat_id=<?php echo $cat_id; ?>" class="view-all-link">
                        View All <i class="fas fa-chevron-right text-[9px]"></i>
                    </a>
                </div>

                <div class="swiper cat-slider-container category-slider">
                    <div class="swiper-wrapper">
                        <?php foreach($game_chunks as $chunk): ?>
                        <div class="swiper-slide">
                            <div class="grid grid-cols-4 gap-2">
                                <?php foreach($chunk as $g): 
                                    if ($is_logged_in) {
                                        $link = "player/launch.php?game_id=" . $g['game_uid'];
                                    } else {
                                        $redirect = urlencode("player/launch.php?game_id=" . $g['game_uid']);
                                        $link = "player/login.php?redirect=" . $redirect;
                                    }
                                ?>
                                <a href="<?php echo $link; ?>" class="game-card-new group">
                                    <div class="game-img-wrapper">
                                        <img src="<?php echo $g['image']; ?>" onerror="this.src='https://placehold.co/100x100/126e51/FFF?text=GAME'">
                                    </div>
                                    <div class="game-title-text"><?php echo $g['name']; ?></div>
                                </a>
                                <?php endforeach; ?>
                            </div>
                        </div>
                        <?php endforeach; ?>
                    </div>
                    <div class="swiper-pagination"></div>
                </div>

            </div>

        <?php 
                endif; 
            endwhile; 
        else:
        ?>
            <div class="text-center py-10">
                <p class="text-gray-400 text-sm">No games available.</p>
            </div>
        <?php endif; ?>
    </div>

    <div id="dlBanner" class="fixed bottom-[65px] left-0 right-0 bg-[#222] border-t-2 border-[#126E51] p-2 px-3 z-40 flex justify-between items-center shadow-2xl">
        <div class="flex items-center gap-2">
            <button onclick="document.getElementById('dlBanner').style.display='none'" class="text-gray-400 active:text-white"><i class="fas fa-times"></i></button>
            <div class="bg-white p-0.5 rounded flex items-center justify-center">
                <img src="assets/img/app_logo.jpeg" class="h-4 w-auto object-contain" alt="Logo">
            </div>
            <div>
                <p class="text-[11px] font-bold text-white"><?php echo $site_name; ?> Web App</p>
                <div class="text-[#FFDF1B] text-[9px]"><i class="fas fa-star"></i><i class="fas fa-star"></i><i class="fas fa-star"></i><i class="fas fa-star"></i><i class="fas fa-star"></i></div>
            </div>
        </div>
        <a href="#" class="bg-[#FFDF1B] text-black text-[10px] font-bold px-4 py-1.5 rounded active:scale-95 transition">Download</a>
    </div>

    <?php if($is_logged_in): ?>
        <div class="fixed bottom-0 left-0 w-full h-[60px] bottom-nav z-50 flex justify-between items-center px-2">
            <a href="index.php" class="nav-link active">
                <div class="home-icon-container">
                    <i class="fas fa-home text-lg mb-1"></i>
                    <div class="home-dot"></div> 
                </div>
                <span class="text-[10px] font-medium">Home</span>
            </a>
            <a href="player/promotions.php" class="nav-link">
                <i class="fas fa-gift text-lg mb-1"></i>
                <span class="text-[10px] font-medium">Promotions</span>
            </a>
            <a href="player/deposit.php" class="nav-link">
                <i class="fas fa-wallet text-lg mb-1"></i>
                <span class="text-[10px] font-medium">Deposit</span>
            </a>
            <a href="player/account.php" class="nav-link">
                <i class="fas fa-user-circle text-lg mb-1"></i>
                <span class="text-[10px] font-medium">My Account</span>
            </a>
        </div>
    <?php else: ?>
        <div class="guest-bottom-nav">
            <div class="guest-nav-lang active:opacity-80 transition">
                <div class="flag-circle mr-1"></div> 
                <div class="flex flex-col leading-none">
                    <span>BDT</span>
                    <span class="font-normal text-[10px]">English</span>
                </div>
            </div>
            <div onclick="window.location.href='player/signup.php'" class="guest-nav-btn guest-signup active:bg-[#e5c918] transition">
                Sign Up
            </div>
            <div onclick="window.location.href='player/login.php'" class="guest-nav-btn guest-login active:bg-[#0e5640] transition">
                Log In
            </div>
        </div>
    <?php endif; ?>

    <?php include 'includes/sidebar_player.php'; ?>
    <?php include 'player/sectionlast.php'; ?>

    <script src="https://cdn.jsdelivr.net/npm/swiper@10/swiper-bundle.min.js"></script>
    <script>
        new Swiper(".mainSlider", { loop: true, autoplay: { delay: 3500 }, pagination: { el: ".swiper-pagination", clickable: true } });

        function toggleSidebar() {
            const sb = document.getElementById('sidebar');
            if(sb) sb.classList.toggle('-translate-x-full');
        }

        function activateCat(element) {
            document.querySelectorAll('.cat-item').forEach(el => el.classList.remove('active'));
            element.classList.add('active');
        }

        new Swiper(".category-slider", { 
            slidesPerView: 1, 
            spaceBetween: 20,
            pagination: { el: ".swiper-pagination", clickable: true }
        });
    </script>
</body>
</html>