<?php
session_start();
require_once(__DIR__ . "/../config/db.php");
require_once(__DIR__ . "/../session.php");

header('Content-Type: application/json');

if (isset($user['extplayer'])) {
    $extplayer = $user['extplayer'];

    $saldo = (int) $saldo_aktif;

    $query = "UPDATE balance SET total_balance = :total_balance WHERE user_id = :user_id";
    $stmt = $db->prepare($query);
    $stmt->bindValue(':total_balance', $saldo, PDO::PARAM_INT);
    $stmt->bindValue(':user_id', $user_id, PDO::PARAM_INT);

    if ($stmt->execute()) {
        echo json_encode(["saldo" => $saldo]);
    } else {
        echo json_encode(["error" => "Gagal memperbarui saldo."]);
    }
} else {
    echo json_encode(["error" => "User tidak ditemukan."]);
}
?>