<?php
if ($c['level'] == "superadmin") {

    require_once __DIR__ . '/../../../../function/class.goldslot.php';

    // Ambil config
    $config = mysqli_fetch_assoc(mysqli_query($koneksi, "SELECT api_token, callback_token FROM tb_goldslot_config LIMIT 1"));

    if (!$config || empty($config['api_token'])) {
        echo "<script>alert('Konfigurasi GoldSlotPalace belum diatur!'); window.location='?page=goldslot&sub=config';</script>";
        exit;
    }

    $goldslot = new GoldSlotPalace($config['api_token'], $config['callback_token']);
    $res = $goldslot->getAgentInfo();

    if (!GoldSlotPalace::isSuccess($res)) {
        $errorMsg = GoldSlotPalace::getErrorMessage($res);
        echo "<script>alert('Gagal mendapatkan info agent: $errorMsg'); window.location='?page=goldslot&sub=config';</script>";
        exit;
    }

    $agent = $res['data'] ?? [];
    ?>

    <div class="page-breadcrumb">
        <div class="row">
            <div class="col-5 align-self-center">
                <h4 class="page-title">GoldSlotPalace - Agent Info</h4>
            </div>
        </div>
    </div>

    <div class="container-fluid">
        <div class="row">
            <div class="col-lg-12">
                <div class="card">
                    <div class="card-body">
                        <h4 class="card-title">Informasi Agent GoldSlotPalace</h4>
                        <hr>

                        <table class="table table-bordered">
                            <tr>
                                <th width="200">Nama Agent</th>
                                <td><?= htmlspecialchars($agent['name'] ?? '-'); ?></td>
                            </tr>
                            <tr>
                                <th>Currency</th>
                                <td><?= htmlspecialchars($agent['currency'] ?? '-'); ?></td>
                            </tr>
                            <tr>
                                <th>Balance (Points)</th>
                                <td><strong><?= number_format($agent['balance'] ?? 0, 2); ?></strong></td>
                            </tr>
                            <tr>
                                <th>RTP (Winning Rate)</th>
                                <td><?= $agent['rtp'] ?? '-'; ?></td>
                            </tr>
                            <tr>
                                <th>Client IP</th>
                                <td><?= htmlspecialchars($agent['client_ip'] ?? '-'); ?></td>
                            </tr>
                            <tr>
                                <th>Whitelist IPs</th>
                                <td>
                                    <?php if (!empty($agent['whitelist'])): ?>
                                        <?php foreach ($agent['whitelist'] as $ip): ?>
                                            <span class="badge bg-info"><?= htmlspecialchars($ip); ?></span>
                                        <?php endforeach; ?>
                                    <?php else: ?>
                                        <span class="text-muted">Tidak ada</span>
                                    <?php endif; ?>
                                </td>
                            </tr>
                        </table>

                        <a href="?page=goldslot&sub=config" class="btn btn-secondary">
                            <i class="ti-arrow-left"></i> Kembali ke Konfigurasi
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <?php

} else {
    echo "<script>window.location='?page=dashboard';</script>";
}
?>