<?php
// Enable error reporting for debugging
// ini_set('display_errors', 1);
// ini_set('display_startup_errors', 1);
// error_reporting(E_ALL);

// Include your database connection or other necessary files
require_once('../database/header.php');
?>

<div class="main-panel">
<div class="content-wrapper">
<!-- DataTales Example -->
<div class="card shadow mb-4">
<div class="card-header py-3 bg-primary">
<h6 class="m-0 font-weight-bold text-white">BharatPe 支付记录</h6>
</div>
<div class="card-body">


<?php
// Check user status (if needed)
if ($userdata['status'] == 'paywait') {
echo '<script>window.location = "paywait.php"</script>';
exit;
}

// Step 1: Fetch Data from the API
$api_url = "https://smartupi.in.net/merchantapi/bharatpe/payout-details.php?token=$token";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
//echo $response;
if (curl_errno($ch)) {
echo 'cURL error: ' . curl_error($ch);
exit;
}
curl_close($ch);

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

// Check if the API call was successful
if ($data && isset($data['status']) && $data['status'] === 'success' && $data['status_code'] == 200) {
$payouts = $data['data']['data'];
} else {
    
echo '<div class="alert alert-danger" role="alert">
<strong>糟糕！</strong> 未绑定商户。请先连接 BharatPe 账户，然后再查看支付记录
  </div>';
  
//echo "Failed to fetch data from the API.";
exit;
}
?>


<div class="table-responsive">
<table class="table table-bordered" id="order-listing" width="100%" cellspacing="0">
<thead>
<tr>
<th style='display:none;'>SL No.</th>
<th class='text-dark'>商户 ID</th>
<th class='text-dark'>参考 ID</th>
<th class='text-dark'>日期</th>
<th class='text-dark'>类型</th>
<th class='text-dark'>金额</th>
<th class='text-dark'>状态</th>
</tr>
</thead>
<tbody>
<?php
$sl = 1;
foreach ($payouts as $payout) {
$status = '<span class="badge badge-info">待处理</span>';
if ($payout['status'] == 'SUCCESS') {
$status = '<span class="badge badge-success">成功</span>';
} elseif ($payout['status'] == 'FAILED') {
$status = '<span class="badge badge-danger">失败</span>';
}

echo "<tr>
<td style='display:none;'>" . $sl . "</td>
<td class='text-dark'>" . $payout['merchant_id'] . "</td>
<td style='font-size:13px' class='text-dark'>" . $payout['reference_id'] . "</td>
<td style='font-size:13px' class='text-dark'>" . date('Y-m-d H:i:s', $payout['timestamp']) . "</td>
<td style='font-size:13px' class='text-dark'>" . $payout['type'] . "</td>
<td style='font-size:13px' class='text-dark'>Rs. " . $payout['amount'] . "</td>
<td class='text-primary'><i>" . $status . "</i></td>
</tr>";
$sl++;
}
?>
</tbody>
</table>
</div>
</div>
</div>
</div>


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