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

<div class="main-panel">
    <div class="content-wrapper">
        <div class="row">
            <div class="col-md-12 stretch-card">
                <!-- 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">HDFC SmartHub 交易记录</h6>
                    </div>
                    <div class="card-body">
                        
                        <?php
                        // Fetch merchant_id and access_token based on userdata['id']
                        $stmt = $conn->prepare("SELECT terminal_id, session_id FROM add_merchant WHERE user = ? AND merchant_name = 'HDFC' ");
                        $stmt->execute([$userdata['username']]);
                        $merchantDetails = $stmt->fetch(PDO::FETCH_ASSOC);

                        if ($merchantDetails) {
                            $terminal_id = $merchantDetails['terminal_id'];
                            $session_id = $merchantDetails['session_id'];

                            // Make the API call
                            $api_url = "https://smartupi.in.net/merchantapi/HDFCSoft/merchant-txn-detail.php?tidList=$terminal_id&sessionid=$session_id&count=100";
                            $response = file_get_contents($api_url);
                           // echo $response;
                            if ($response === FALSE) {
                                echo "<div class='container-fluid'>
                                        <div class='alert alert-danger'>
                                            <strong>错误！</strong> 无法从 API 获取数据。
                                        </div>
                                      </div>";
                            } else {
                                $responseData = json_decode($response, true);

                                // Check for specific error response
                                if (isset($responseData['status']) && $responseData['status'] === 'error' && $responseData['message'] === 'HTTP Error: 401') {
                                    echo "<div class='container-fluid'>
                                            <div class='alert alert-danger'>
                                                <strong>错误！</strong> 未经授权的访问。未找到商户。
                                            </div>
                                          </div>";
                                } elseif (isset($responseData['status']) && $responseData['status'] === 'Success') {
                                    if (empty($responseData['transactionParams'])) {
                                        // Show message when no transactions found
                                        echo "<div class='container-fluid'>
                                                <div class='alert alert-info'>
                                                    <strong>信息！</strong> 未找到交易。
                                                </div>
                                              </div>";
                                    } else {
                                        // Display transactions in the DataTable
                                        ?>
                                        <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'>金额</th>
                                                        <th class='text-dark'>客户手机</th>
                                                        <th class='text-dark'>UTR 编号</th>
                                                        <th class='text-dark'>支付类型</th>
                                                        <th class='text-dark'>状态</th>
                                                        <th class='text-dark'>消息</th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    <?php
                                                    $sl = 1;
                                                    foreach ($responseData['transactionParams'] as $transaction) {
                                                        $status = '';
                                                        $statusClass = '';
                                                        
                                                        switch($transaction['status']) {
                                                            case '3': // Success
                                                                $status = '成功';
                                                                $statusClass = 'text-success';
                                                                break;
                                                            case '4': // Failure
                                                                $status = '失败';
                                                                $statusClass = 'text-danger';
                                                                break;
                                                            case '2': // Pending
                                                                $status = '待处理';
                                                                $statusClass = 'text-warning';
                                                                break;
                                                            default:
                                                                $status = $transaction['status'];
                                                                $statusClass = 'text-info';
                                                        }
                                                        
                                                        echo "<tr>
                                                                <td style='display:none;'>" . $sl . "</td>
                                                                <td class='text-dark'>" . $transaction['description'] . "</td>
                                                                <td class='text-dark'>Rs." . $transaction['amount'] . "</td>
                                                                <td class='text-dark'>" . $transaction['customerMobile'] . "</td>
                                                                <td class='text-dark'>" . ($transaction['issuerRefNo'] ? $transaction['issuerRefNo'] : 'N/A') . "</td>
                                                                <td class='text-dark'>" . ($transaction['paymentType'] == '3' ? 'UPI' : '其他') . "</td>
                                                                <td class='text-dark'><b class='$statusClass'>" . $status . "</b></td>
                                                                <td class='text-dark'>" . $transaction['txnmessage'] . "</td>
                                                              </tr>";
                                                        $sl++;
                                                    }
                                                    ?>
                                                </tbody>
                                            </table>
                                        </div>
                                        <?php
                                    }
                                } else {
                                    echo "<div class='container-fluid'>
                                            <div class='alert alert-warning'>
                                                <strong>警告！</strong> 无效的 API 响应: " . htmlspecialchars($response) . "
                                            </div>
                                          </div>";
                                }
                            }
                        } else {
                            // If no merchant details are found, show a message and provide an option to add a merchant
                            echo "<div class='container-fluid'>
                                    <div class='alert alert-danger'>
                                        <strong>错误！</strong> 未找到商户详情。
                                    </div>
                                    <a href='add-merchant.php' class='btn btn-dark'>添加商户</a>
                                  </div>";
                        }
                        ?>
                    </div>
                </div>
            </div>
        </div>
    </div>

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