<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, maximum-scale=1, minimum-scale=1">
    <title>JILI Games</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #fff;
            margin: 0;
            padding: 0;
            overflow-x: hidden;
        }
        .app {
            width: 100%;
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
            box-sizing: border-box;
        }
        nav {
            background-color: #fff;
            color: #fff;
            padding: 10px 20px;
            text-align: center;
        }
        .page_title {
            margin: 0;
            color: #666;
            font-size: 24px;
        }
        main {
            display: flex;
            flex-wrap: wrap;
            justify-content: space-between;
            padding: 20px 0;
            box-sizing: border-box;
        }
        .jili_games_card {
            flex: 1 0 30%;
            max-width: 30%;
            height: 100px;
            margin: 10px 1%;
            background-color: #fff;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            text-align: center;
            text-decoration: none;
        }
        .jili_games_card img {
            width: 100px;
            height: 100px;
            cursor: pointer;
            object-fit: cover;
        }
    </style>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
</head>
<body>
    <div class="app">
        <nav>
            <h4 class="page_title">Table Games</h4>
        </nav>
        <main id="games-container">
            <!-- Game cards will be injected here -->
        </main>
    </div>
    <script>
        const userInfoTypes = {
            phoneNumber: "phone_user",
            username: "name_user",
            userId: "id_user",
        };
        let userData = null;

        const fetchUser = async () => {
            try {
                const response = await axios.get('/api/webapi/GetUserInfo');
                userData = response?.data?.data;
                if (!userData) throw Error("User data not found!");
                displayGames();
            } catch (error) {
                console.log(error);
            }
        };

        const displayGames = () => {
            const mobile = encodeURIComponent(userData[userInfoTypes.phoneNumber]);

            const agentId = '<%= process.env.AGENT_ID %>';
            const agentKey = '<%= process.env.AGENT_KEY %>';
            const referrerUrl = '<%= process.env.CALLBACK_URL %>';

            const baseUrl = "https://fusion.fluxhost.live/post";
            const imageBaseUrl = "https://fluxapis.live/images";
            const gameIds = [
                64, 61, 62, 63, 66, 118, 111, 112, 113, 123, 122, 124, 139, 125, 151, 148, 179, 150, 174, 197, 178, 149, 200,
                173, 177, 182, 216, 204, 217, 195, 114, 297, 389, 224, 229, 232, 233, 235, 236, 254, 241, 242, 261, 407
            ];

            const container = document.getElementById('games-container');
            container.innerHTML = '';

            gameIds.forEach(gameId => {
                const paddedGameId = gameId < 10 ? `0${gameId}` : gameId;
                const imagePath = `${imageBaseUrl}/GAMEID_${paddedGameId}.png`;
                const gameUrl = `${baseUrl}?gameId=${gameId}&mobile=${mobile}&agentId=${agentId}&agentKey=${agentKey}&referrerUrl=${referrerUrl}`;
                const cardHtml = `
                    <a href="${gameUrl}" class="jili_games_card">
                        <img src="${imagePath}" alt="Game ${gameId}" onerror="this.style.display='none'">
                    </a>
                `;
                container.innerHTML += cardHtml;
            });
        };

        fetchUser();
    </script>
</body>
</html>