<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

include 'koneksi.php';

$providers = [];
$query = 'SELECT provider_code, provider_name FROM tb_provider';
if ($stmt = $call->prepare($query)) {
    $stmt->execute();
    $result = $stmt->get_result();

    while ($row = $result->fetch_assoc()) {
        $providers[] = $row;
    }

    $stmt->close();
} else {
    echo "Gagal mengambil data provider: " . $call->error;
}

// Ambil provider_code dari query string jika ada
$provider = isset($_GET['provider']) ? strtoupper($call->real_escape_string($_GET['provider'])) : 'PR';

// Kunci rahasia
$secret_key = 'f3c865e04f2f3ebf08269f1d278389fb';

// Generate signature
$operatorcode = strtolower('oaci');
$signature = md5($operatorcode . $provider . $secret_key);
$signature_uppercase = strtoupper($signature);

// URL API
$url = "http://gsmd.336699bet.com/getGameList.ashx?operatorcode={$operatorcode}&providercode={$provider}&reformatjson=yes&signature={$signature_uppercase}";

// Ambil data dari API
$jsonData = file_get_contents($url);

if ($jsonData === FALSE) {
    $error = "Gagal mengambil data, Cek kode anda lagi.";
    $filteredData = [];
} else {
    // Decode data JSON
    $outerData = json_decode($jsonData, true);

    if ($outerData === NULL) {
        $error = isset($outerData['errMsg']) ? htmlspecialchars($outerData['errMsg'], ENT_QUOTES, 'UTF-8') : 'Gagal mendecode data JSON.';
        $filteredData = [];
    } else {
        // Decode gamelist JSON
        $gamelistJson = isset($outerData['gamelist']) ? $outerData['gamelist'] : '';
        $gamelist = json_decode($gamelistJson, true);

        if ($gamelist === NULL) {
            $error = isset($outerData['errMsg']) ? htmlspecialchars($outerData['errMsg'], ENT_QUOTES, 'UTF-8') : 'Gagal mendecode gamelist JSON.';
            $filteredData = [];
        } else {
            // Filter data
            $filteredData = [];
            foreach ($gamelist as $game) {
                $filteredData[] = [
                    'g_code' => isset($game['g_code']) ? htmlspecialchars($game['g_code'], ENT_QUOTES, 'UTF-8') : 'N/A',
                    'g_type' => isset($game['g_type']) ? htmlspecialchars($game['g_type'], ENT_QUOTES, 'UTF-8') : 'N/A',
                    'p_code' => isset($game['p_code']) ? htmlspecialchars($game['p_code'], ENT_QUOTES, 'UTF-8') : 'N/A',
                    'p_type' => isset($game['p_type']) ? htmlspecialchars($game['p_type'], ENT_QUOTES, 'UTF-8') : 'N/A',
                    'status' => isset($game['status']) ? htmlspecialchars($game['status'], ENT_QUOTES, 'UTF-8') : 'N/A',
                    'imgFileName' => isset($game['imgFileName']) ? htmlspecialchars($game['imgFileName'], ENT_QUOTES, 'UTF-8') : 'N/A',
                    'gameName' => isset($game['gameName']['gameName_enus']) ? htmlspecialchars($game['gameName']['gameName_enus'], ENT_QUOTES, 'UTF-8') : 'N/A'
                ];
            }
        }
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Daftar Game</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <style>
        .img-thumbnail {
            width: 100px;
            height: auto;
        }
    </style>
</head>
<body>
<div class="container mt-4">
    <h1>Daftar Game</h1>
    
    <!-- Form untuk memilih provider -->
    <form method="GET" action="">
        <div class="form-group">
            <label for="provider">Pilih Provider:</label>
            <select name="provider" id="provider" class="form-control" onchange="this.form.submit()">
                <?php foreach ($providers as $prov) : ?>
                    <option value="<?php echo htmlspecialchars($prov['provider_code'], ENT_QUOTES, 'UTF-8'); ?>" <?php echo ($prov['provider_code'] === $provider) ? 'selected' : ''; ?>>
                        <?php echo htmlspecialchars($prov['provider_name'], ENT_QUOTES, 'UTF-8'); ?>
                    </option>
                <?php endforeach; ?>
            </select>
        </div>
    </form>

    <!-- Tabel untuk menampilkan data game -->
    <div class="table-responsive">
        <table class="table table-striped">
            <thead>
                <tr>
                    <th>Game Code</th>
                    <th>Game Type</th>
                    <th>Provider Code</th>
                    <th>Provider Type</th>
                    <th>Status</th>
                    <th>Image</th>
                    <th>Game Name</th>
                </tr>
            </thead>
            <tbody>
                <?php if (!empty($filteredData)) : ?>
                    <?php foreach ($filteredData as $game) : ?>
                        <tr>
                            <td><?php echo htmlspecialchars($game['g_code'], ENT_QUOTES, 'UTF-8'); ?></td>
                            <td><?php echo htmlspecialchars($game['g_type'], ENT_QUOTES, 'UTF-8'); ?></td>
                            <td><?php echo htmlspecialchars($game['p_code'], ENT_QUOTES, 'UTF-8'); ?></td>
                            <td><?php echo htmlspecialchars($game['p_type'], ENT_QUOTES, 'UTF-8'); ?></td>
                            <td><?php echo htmlspecialchars($game['status'], ENT_QUOTES, 'UTF-8'); ?></td>
                            <td>
                                <?php if (!empty($game['imgFileName'])) : ?>
                                    <img src="<?php echo htmlspecialchars($game['imgFileName'], ENT_QUOTES, 'UTF-8'); ?>" alt="<?php echo htmlspecialchars($game['gameName'], ENT_QUOTES, 'UTF-8'); ?>" class="img-thumbnail">
                                <?php else : ?>
                                    No Image
                                <?php endif; ?>
                            </td>
                            <td><?php echo htmlspecialchars($game['gameName'], ENT_QUOTES, 'UTF-8'); ?></td>
                        </tr>
                    <?php endforeach; ?>
                <?php else : ?>
                    <tr>
                        <td colspan="7"><?php echo isset($error) ? htmlspecialchars($error, ENT_QUOTES, 'UTF-8') : 'No game data available.'; ?></td>
                    </tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<!-- Optional JavaScript; choose one of the two! -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.6/dist/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</body>
</html>
