<?php
require_once('../database/header.php');
?>

<!-- partial -->
<div class="main-panel">
<div class="content-wrapper">
<div class="row">
<div class="col-lg-12 grid-margin stretch-card">
<div class="card">
<div class="card-body">

<?php
// PHP code to fetch data from the API
$apiUrl = "https://smartupi.in.net/merchantapi/bharatpe/account.php?token=$token";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);

// Check for cURL errors
if (curl_errno($ch)) {
echo '<script>
Swal.fire({
icon: "error",
title: "cURL 错误",
text: "' . curl_error($ch) . '",
});
  </script>';
curl_close($ch);
exit;
}
curl_close($ch);

// Decode the JSON response
$data = json_decode($response, true);

// Check if the API call was successful
if (isset($data['status']) && $data['status'] === 'success') {
// Success response handling
$accountData = $data['data'];
// You can now use $accountData as needed
// echo "Data fetched successfully:\n";
// print_r($accountData);
} else {
// Failed response handling
$errorMessage = $data['message'] ?? 'Unknown error occurred';
$statusCode = $data['status_code'] ?? 'No status code';

  echo '
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script>
Swal.fire({
title: "糟糕 ",
text: "未绑定商户。请先连接 BharatPe 账户，然后再查看支付报告",
icon: "error",
showConfirmButton: true,
timer: 2000,
willClose: () => {
window.location.href = "index.php";
}
});
</script>
'; 
exit;
}
?>

<h4 class="card-title">BharatPe 账户余额</h4>
<p class="card-description">
<code>实时</code> 报告
</p>
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th>详情</th>
<th>金额</th>
</tr>
</thead>
<tbody>
<tr>
<td>当前余额</td>
<td><label class="badge badge-success">Rs. <?php echo $accountData['current_balance']; ?></label></td>
</tr>
<tr>
<td>当前本金</td>
<td><label class="badge badge-success">Rs. <?php echo $accountData['current_principal']; ?></label></td>
</tr>
<tr>
<td>最大提现金额</td>
<td><label class="badge badge-success">Rs. <?php echo $accountData['max_withdrawal_amount']; ?></label></td>
</tr>
<tr>
<td>今日收款</td>
<td><label class="badge badge-success">Rs. <?php echo $accountData['today_collection']; ?></label></td>
</tr>
<tr>
<td>今日提现总额</td>
<td><label class="badge badge-success">Rs. <?php echo $accountData['today_total_withdrawal']; ?></label></td>
</tr>
<tr>
<td>冻结余额</td>
<td><label class="badge badge-success">Rs. <?php echo $accountData['hold_balance']; ?></label></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</div>

<?php
require_once('../database/footer.php');
?>