<?php
session_start();
require '../includes/db.php';

if (!isset($_SESSION['role']) || $_SESSION['role'] !== 'admin') {
    die("Access Denied");
}

// সেটিংস সেভ করা
$msg = "";
if (isset($_POST['save_settings'])) {
    $settings = [
        'api_endpoint' => $_POST['endpoint'],
        'api_token'    => $_POST['token'],
        'secret_key'   => $_POST['secret'],
        'agent_code'   => $_POST['agent']
    ];

    foreach ($settings as $key => $val) {
        $val = $conn->real_escape_string($val);
        $conn->query("UPDATE game_settings SET setting_value='$val' WHERE setting_key='$key'");
    }
    $msg = "✅ API Settings Updated Successfully!";
}

// বর্তমান সেটিংস ফেচ করা
$config = [];
$res = $conn->query("SELECT * FROM game_settings");
while ($row = $res->fetch_assoc()) {
    $config[$row['setting_key']] = $row['setting_value'];
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Game API Configuration</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
</head>
<body class="bg-gray-900 text-gray-200 p-8">
    <div class="max-w-2xl mx-auto bg-gray-800 p-8 rounded-xl border border-gray-700 shadow-2xl">
        <h2 class="text-2xl font-bold text-yellow-500 mb-6 flex items-center gap-2">
            <i class="fas fa-cogs"></i> Game Provider API Settings
        </h2>

        <?php if($msg) echo "<div class='bg-green-900/50 text-green-400 p-3 rounded mb-6 border border-green-600'>$msg</div>"; ?>

        <form method="POST" class="space-y-5">
            
            <div>
                <label class="block text-sm font-bold text-gray-400 mb-1">API Endpoint URL</label>
                <input type="text" name="endpoint" value="<?php echo $config['api_endpoint'] ?? ''; ?>" class="w-full bg-gray-900 border border-gray-600 rounded p-3 text-white focus:border-yellow-500 outline-none font-mono text-sm">
            </div>

            <div>
                <label class="block text-sm font-bold text-gray-400 mb-1">Agent Code / User ID (Provider)</label>
                <input type="text" name="agent" value="<?php echo $config['agent_code'] ?? ''; ?>" class="w-full bg-gray-900 border border-gray-600 rounded p-3 text-white focus:border-yellow-500 outline-none">
            </div>

            <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                <div>
                    <label class="block text-sm font-bold text-gray-400 mb-1">API Token</label>
                    <input type="text" name="token" value="<?php echo $config['api_token'] ?? ''; ?>" class="w-full bg-gray-900 border border-gray-600 rounded p-3 text-green-400 focus:border-yellow-500 outline-none font-mono text-sm">
                </div>
                <div>
                    <label class="block text-sm font-bold text-gray-400 mb-1">Secret Key</label>
                    <input type="text" name="secret" value="<?php echo $config['secret_key'] ?? ''; ?>" class="w-full bg-gray-900 border border-gray-600 rounded p-3 text-red-400 focus:border-yellow-500 outline-none font-mono text-sm">
                </div>
            </div>

            <div class="pt-4">
                <button type="submit" name="save_settings" class="w-full bg-gradient-to-r from-yellow-600 to-yellow-500 hover:from-yellow-500 hover:to-yellow-400 text-black font-bold py-3 rounded shadow-lg transition transform active:scale-95">
                    SAVE CONFIGURATION
                </button>
            </div>
        </form>
    </div>
</body>
</html>