<?php 
ob_start();
session_start();
include("include/connection.php");

$userid = $_SESSION['frontuserid'];
$amount = $_POST['amount'];
$index = $_POST['index'];

// Get latest betting record
$chkwallet = mysqli_query($con, "SELECT * FROM tbl_betting WHERE userid='$userid' ORDER BY id DESC LIMIT 1");
$chkwalletResult = mysqli_fetch_assoc($chkwallet);

$id = $chkwalletResult['id'];

// Update betting result
$sql = mysqli_query($con, "UPDATE `tbl_betting` SET `result`='Win', `status`='1', `totalamount`='$amount', `cashOut`='$index' WHERE id='$id'");

// Update wallet
$walletbalance = wallet($con, 'amount', $userid);
$finalbalanceDebit = $walletbalance + $amount;

$sqlwallet = mysqli_query($con, "UPDATE `tbl_wallet` SET `amount` = '$finalbalanceDebit' WHERE `userid` = '$userid'");

// Return updated balance
echo number_format($finalbalanceDebit, 2);
?>
