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

<div class="page-breadcrumb">
    <div class="row">
        <div class="col-5 align-self-center">
            <h4 class="page-title">GoldSlotPalace API Configuration</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">Pengaturan API GoldSlotPalace</h4>
                    <hr>

                    <?php
                    $query = mysqli_query($koneksi, "SELECT * FROM tb_goldslot_config LIMIT 1");
                    $config = mysqli_fetch_assoc($query);

                    // Cek apakah ada data, jika belum ada insert default
                    if (!$config) {
                        mysqli_query($koneksi, "INSERT INTO tb_goldslot_config (api_token, callback_token, callback_url, status) 
                            VALUES ('dc076dab-5076-4e76-96eb-e3587fdadaaf', 'fe5c7c62-9495-41d4-9834-2d19575f81fb', '', 1)");
                        $config = mysqli_fetch_assoc(mysqli_query($koneksi, "SELECT * FROM tb_goldslot_config LIMIT 1"));
                    }
                    ?>

                    <form class="form p-t-20" action="?page=goldslot&sub=update_config" method="POST">
                        <input type="hidden" name="id" value="<?= $config['id']; ?>">

                        <div class="form-group">
                            <label>API Token (Bearer Token)</label>
                            <div class="input-group mb-3">
                                <span class="input-group-text"><i class="ti-key"></i></span>
                                <input type="text" class="form-control" name="api_token" 
                                       value="<?= htmlspecialchars($config['api_token']); ?>" required
                                       style="font-family: monospace; letter-spacing: 1px;">
                            </div>
                            <small class="text-muted">Token untuk Authorization: Bearer {API_TOKEN}</small>
                        </div>

                        <div class="form-group">
                            <label>Callback Token</label>
                            <div class="input-group mb-3">
                                <span class="input-group-text"><i class="ti-shield"></i></span>
                                <input type="text" class="form-control" name="callback_token" 
                                       value="<?= htmlspecialchars($config['callback_token']); ?>" required
                                       style="font-family: monospace; letter-spacing: 1px;">
                            </div>
                            <small class="text-muted">Token untuk verifikasi callback dari GoldSlotPalace</small>
                        </div>

                        <div class="form-group">
                            <label>Callback URL</label>
                            <div class="input-group mb-3">
                                <span class="input-group-text"><i class="ti-link"></i></span>
                                <input type="text" class="form-control" name="callback_url" 
                                       value="<?= htmlspecialchars($config['callback_url']); ?>" required>
                            </div>
                            <small class="text-muted">Contoh: https://yourdomain.com/function/goldslot_callback.php</small>
                        </div>

                        <div class="form-group">
                            <label>Status API</label>
                            <select name="status" class="form-select">
                                <option value="1" <?= $config['status'] == 1 ? 'selected' : ''; ?>>Active</option>
                                <option value="0" <?= $config['status'] == 0 ? 'selected' : ''; ?>>Inactive</option>
                            </select>
                        </div>

                        <button type="submit" name="upload" class="btn btn-success">
                            <i class="ti-save"></i> Simpan Konfigurasi
                        </button>
                        <a href="?page=goldslot&sub=test_connection" class="btn btn-info">
                            <i class="ti-check"></i> Test Connection
                        </a>
                    </form>
                </div>
            </div>

            <!-- Provider List Card -->
            <div class="card mt-3">
                <div class="card-body">
                    <h4 class="card-title">Daftar Provider GoldSlotPalace</h4>
                    <hr>
                    
                    <?php
                    // Tampilkan provider yang sudah di-sync
                    $provQuery = mysqli_query($koneksi, "SELECT * FROM tb_goldslot_providers ORDER BY provider_name ASC");
                    $providerCount = mysqli_num_rows($provQuery);
                    ?>
                    
                    <p>Total Provider Terdaftar: <strong><?= $providerCount; ?></strong></p>
                    
                    <a href="?page=goldslot&sub=sync_providers" class="btn btn-warning mb-3">
                        <i class="ti-reload"></i> Sync Provider dari API
                    </a>
                    
                    <div class="table-responsive">
                        <table class="table table-striped table-bordered">
                            <thead>
                                <tr>
                                    <th>Provider ID</th>
                                    <th>Nama Provider</th>
                                    <th>Nama Lokal</th>
                                    <th>Status</th>
                                    <th>Last Sync</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php if ($providerCount > 0): ?>
                                    <?php while ($prov = mysqli_fetch_assoc($provQuery)): ?>
                                    <tr>
                                        <td><?= $prov['provider_id']; ?></td>
                                        <td><?= htmlspecialchars($prov['provider_name']); ?></td>
                                        <td><?= htmlspecialchars($prov['locale_name']); ?></td>
                                        <td>
                                            <?php if ($prov['status'] == 1): ?>
                                                <span class="badge bg-success">Normal</span>
                                            <?php else: ?>
                                                <span class="badge bg-danger">Maintenance</span>
                                            <?php endif; ?>
                                        </td>
                                        <td><?= $prov['last_sync']; ?></td>
                                    </tr>
                                    <?php endwhile; ?>
                                <?php else: ?>
                                    <tr>
                                        <td colspan="5" class="text-center">
                                            Belum ada provider. Klik <strong>Sync Provider dari API</strong> untuk mengambil data.
                                        </td>
                                    </tr>
                                <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>

            <!-- Agent Info Card -->
            <div class="card mt-3">
                <div class="card-body">
                    <h4 class="card-title">Informasi Agent GoldSlotPalace</h4>
                    <hr>
                    <a href="?page=goldslot&sub=agent_info" class="btn btn-info">
                        <i class="ti-info-alt"></i> Cek Info Agent
                    </a>
                </div>
            </div>

        </div>
    </div>
</div>

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