<?php
include __DIR__ . "/deepanshu/conn.php";
?>
<!DOCTYPE html>
<html lang="bn">
<head>
<meta charset="UTF-8">
<title>সব Game - CV111</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
    * { margin: 0; padding: 0; box-sizing: border-box; }
    body { background: #F6F6F6; font-family: 'Segoe UI', sans-serif; color: #333; }

    #header { background: #ffffff; padding: 14px 16px; display: flex; align-items: center; gap: 12px; box-shadow: 0 2px 6px rgba(0,0,0,.06); position: sticky; top: 0; z-index: 100; }
    #header a { color: #333; text-decoration: none; font-size: 18px; }
    #header h1 { font-size: 16px; }

    /* Category Tabs */
    #tabs { display: flex; overflow-x: auto; background: #fff; padding: 10px 12px; gap: 8px; border-bottom: 1px solid #e6e8e8; position: sticky; top: 53px; z-index: 99; }
    #tabs::-webkit-scrollbar { display: none; }
    .tab-btn { flex-shrink: 0; padding: 8px 16px; border-radius: 20px; border: 1px solid #e6e8e8; background: #F6F6F6; color: #666; font-size: 12.5px; font-weight: 700; cursor: pointer; white-space: nowrap; }
    .tab-btn.active { background: #F5CC2C; color: #333; border-color: #F5CC2C; }

    #games-section { max-width: 1000px; margin: 16px auto; padding: 0 16px; }
    #games-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; }
    .game-card { background: #fff; border: 1px solid #e6e8e8; border-radius: 10px; overflow: hidden; cursor: pointer; text-align: center; }
    .game-card:active { transform: scale(0.96); }
    .game-card img { width: 100%; aspect-ratio: 1; object-fit: cover; background: #f0f0f0; display: block; }
    .game-card .name { padding: 6px 4px; font-size: 10.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

    #load-more-btn { display: block; margin: 20px auto; padding: 10px 28px; border-radius: 24px; border: none; background: #333; color: #fff; font-weight: 700; cursor: pointer; }
    #empty-msg { text-align: center; color: #999; padding: 40px 0; font-size: 13px; display: none; }
</style>
</head>
<body>

<div id="header">
    <a href="/">←</a>
    <h1>🎮 সব Game</h1>
</div>

<div id="tabs">
    <button class="tab-btn active" data-cat="popular">জনপ্রিয়</button>
    <button class="tab-btn" data-cat="slot">Slot</button>
    <button class="tab-btn" data-cat="fish">Fish</button>
    <button class="tab-btn" data-cat="lottery">Lottery</button>
    <button class="tab-btn" data-cat="sport">Sport</button>
    <button class="tab-btn" data-cat="video">Video</button>
    <button class="tab-btn" data-cat="flash">Flash</button>
    <button class="tab-btn" data-cat="chess">Chess</button>
</div>

<div id="games-section">
    <div id="games-grid"></div>
    <div id="empty-msg">এই Category-তে এখনো কোনো Game যোগ করা হয়নি।</div>
    <button id="load-more-btn">আরও দেখাও</button>
</div>

<script>
(function() {
    let currentPage = 1;
    let currentCategory = 'popular';
    const grid = document.getElementById('games-grid');
    const loadMoreBtn = document.getElementById('load-more-btn');
    const emptyMsg = document.getElementById('empty-msg');
    const tabs = document.querySelectorAll('.tab-btn');

    async function loadGames(reset) {
        if (reset) {
            currentPage = 1;
            grid.innerHTML = '';
            emptyMsg.style.display = 'none';
        }

        loadMoreBtn.textContent = 'লোড হচ্ছে...';
        try {
            const res = await fetch('/get_all_games_merged.php?category=' + currentCategory + '&page=' + currentPage + '&limit=12');
            const data = await res.json();

            if (reset && data.games.length === 0) {
                emptyMsg.style.display = 'block';
            }

            data.games.forEach(function(game) {
                const card = document.createElement('div');
                card.className = 'game-card';
                card.innerHTML =
                    '<img src="' + (game.icon || game.image || '') + '" onerror="this.style.display=\'none\'">' +
                    '<div class="name">' + (game.gameName || game.name || 'Game') + '</div>';
                grid.appendChild(card);
            });

            currentPage++;
            loadMoreBtn.textContent = 'আরও দেখাও';
            loadMoreBtn.style.display = data.has_more ? 'block' : 'none';
        } catch (e) {
            loadMoreBtn.textContent = 'আবার চেষ্টা করো';
        }
    }

    tabs.forEach(function(tab) {
        tab.addEventListener('click', function() {
            tabs.forEach(t => t.classList.remove('active'));
            tab.classList.add('active');
            currentCategory = tab.dataset.cat;
            loadGames(true);
        });
    });

    loadMoreBtn.addEventListener('click', function() { loadGames(false); });
    loadGames(true);
})();
</script>

</body>
</html>
