<?php
require_once('../database/header.php');
?>
<div class="main-panel">
<div class="content-wrapper">

<!-- Modal -->
<div class="modal fade" id="dataModal" tabindex="-1" role="dialog" aria-labelledby="dataModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header bg-primary">
                <h5 class="modal-title text-white" id="dataModalLabel">交易详情</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <div class="mb-3">
                    <h6>回调 URL：</h6>
                    <div class="p-2 bg-light rounded" id="modalCallbackUrl"></div>
                </div>
                <div class="mb-3">
                    <h6>HTTP 代码：</h6>
                    <div class="p-2 bg-light rounded" id="modalHttpCode"></div>
                </div>
                <div>
                    <h6>交易数据：</h6>
                    <pre id="modalDataContent" style="white-space: pre-wrap; word-wrap: break-word; background-color: #f8f9fa; padding: 10px; border-radius: 5px;"></pre>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>

<!-- 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">回调交易记录</h6>
</div>
<div class="card-body">
<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>
<?php if($userdata['usertype']=="mainadmin") {?>
<th class='text-dark'>用户</th>
<?php }?>
<th class='text-dark'>日期</th>
<th class='text-dark'>订单 ID</th>


<th class='text-dark'>金额</th>
<th class='text-dark'>UTR</th>
<th class='text-dark'>数据</th>
<th class='text-dark'> 状态</th>
</tr>
</thead>
<tbody>
<?php if($userdata['usertype']=="mainadmin") {?>					
<?php
$stmt = $conn->prepare("select * from callback_logs ORDER BY `id` DESC");
$stmt->execute();
$sl=1;
while($row=$stmt->fetch()) {
$txn_status = strtoupper(trim($row['txn_status'])); 

// Format the sent_data for display
$sent_data = json_decode($row['sent_data'], true);
$display_data = is_array($sent_data) ? json_encode($sent_data, JSON_PRETTY_PRINT) : $row['sent_data'];

// Fix: Use JSON status if available as source of truth
if(is_array($sent_data) && isset($sent_data['txn_status'])) {
    $txn_status = strtoupper(trim($sent_data['txn_status']));
}

$status = '<span class="badge badge-info">' . $txn_status . '</span>'; 

if ($txn_status == 'ACTIVE') {
    $status = '<span class="badge badge-success">活跃</span>';
}
if ($txn_status == 'SUCCESS') {
    $status = '<span class="badge badge-success">成功</span>';
}
if ($txn_status == 'PENDING') {
    $status = '<span class="badge badge-info">待处理</span>';
}
if ($txn_status == 'FAILED') {
    $status = '<span class="badge badge-danger">失败</span>';
}

// Fix: Replace RSPAY with pay2inr in display
$rec_from = strtoupper($row['received_from'] ?? '');
if($rec_from == 'RSPAY') $rec_from = 'pay2inr';

echo "<tr>
<td style='display:none;'>".$sl."</td>
<td class='text-dark'>".$row['user_name']."</td>
<td class='text-dark'>".$row['created_at']."</td>
<td class='text-dark'>".$row['order_id']."</td>
<td style='font-size:13px' class='text-dark'>Rs. ".strtoupper($row['amount'])."</td>
<td style='font-size:13px' class='text-dark'>".strtoupper($row['utr_number'] ?? '')." - ".$rec_from."</td>
<td style='font-size:13px' class='text-dark'>
    <button class='badge badge-primary view-data-btn' 
            data-url='".htmlspecialchars($row['callback_url'], ENT_QUOTES)."'
            data-httpcode='".htmlspecialchars($row['http_code'], ENT_QUOTES)."'
            data-data='".htmlspecialchars($display_data, ENT_QUOTES)."'>查看数据</button>
</td>
<td class='text-primary'><i>".$status."</i></td>
</tr>";
$sl++;}							
?>					
<?php
}else{
?>
<?php
$stmt = $conn->prepare("select * from callback_logs WHERE user_name = ? ORDER BY `id` DESC");
$stmt->execute([$userdata['username']]);
$sl=1;
while($row=$stmt->fetch()) {
$txn_status = strtoupper(trim($row['txn_status'])); 

// Format the sent_data for display
$sent_data = json_decode($row['sent_data'], true);
$display_data = is_array($sent_data) ? json_encode($sent_data, JSON_PRETTY_PRINT) : $row['sent_data'];

// Fix: Use JSON status if available as source of truth
if(is_array($sent_data) && isset($sent_data['txn_status'])) {
    $txn_status = strtoupper(trim($sent_data['txn_status']));
}

$status = '<span class="badge badge-info">' . $txn_status . '</span>'; 

if ($txn_status == 'ACTIVE') {
    $status = '<span class="badge badge-success">活跃</span>';
}
if ($txn_status == 'SUCCESS') {
    $status = '<span class="badge badge-success">成功</span>';
}
if ($txn_status == 'PENDING') {
    $status = '<span class="badge badge-info">待处理</span>';
}
if ($txn_status == 'FAILED') {
    $status = '<span class="badge badge-danger">失败</span>';
}

// Fix: Replace RSPAY with pay2inr in display
$rec_from = strtoupper($row['received_from'] ?? '');
if($rec_from == 'RSPAY') $rec_from = 'pay2inr';

echo "<tr>
<td style='display:none;'>".$sl."</td>
<td class='text-dark'>".$row['created_at']."</td>
<td class='text-dark'>".$row['order_id']."</td>
<td style='font-size:13px' class='text-dark'>Rs. ".strtoupper($row['amount'])."</td>
<td style='font-size:13px' class='text-dark'>".strtoupper($row['utr_number'] ?? '')." - ".$rec_from."</td>
<td style='font-size:13px' class='text-dark'>
    <button class='badge badge-primary view-data-btn' 
            data-url='".htmlspecialchars($row['callback_url'], ENT_QUOTES)."'
            data-httpcode='".htmlspecialchars($row['http_code'], ENT_QUOTES)."'
            data-data='".htmlspecialchars($display_data, ENT_QUOTES)."'>查看数据</button>
</td>
<td class='text-primary'><i>".$status."</i></td>
</tr>";
$sl++;}							
?>
<?php
}
?>

</tbody>
</table>
</div>
</div>
</div>

</div>
<!-- /.container-fluid -->
<!-- End of Main Content -->

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

<!-- Add this script just before the closing body tag -->
<script>
$(document).ready(function() {
    // Handle click on view data button
    $(document).on('click', '.view-data-btn', function() {
        var callbackUrl = $(this).attr('data-url');
        var httpCode = $(this).attr('data-httpcode');
        var data = $(this).attr('data-data');
        
        // Set callback URL and HTTP code
        $('#modalCallbackUrl').text(callbackUrl);
        $('#modalHttpCode').text(httpCode);
        
        try {
            // Try to parse as JSON and pretty print if successful
            var parsedData = JSON.parse(data);
            var prettyData = JSON.stringify(parsedData, null, 4);
            // Fix: Replace RSPay with pay2inr in display
            prettyData = prettyData.replace(/RSPay/g, 'pay2inr');
            $('#modalDataContent').text(prettyData);
        } catch (e) {
            // If not JSON, display as is
            // Fix: Replace RSPay with pay2inr in display
            var displayData = data.replace(/RSPay/g, 'pay2inr');
            $('#modalDataContent').text(displayData);
        }
        $('#dataModal').modal('show');
    });
});
</script>
</body>
</html>