<?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">Amazon 交易记录</h6>
                    </div>
                    <div class="card-body">
                        
                        <?php
                        // Fetch merchant_id and access_token based on userdata['id']
                        $stmt = $conn->prepare("SELECT ubid_acbin, x_acbin, at_acbin FROM add_merchant WHERE user_id = ? AND merchant_name = 'Amazon' ");
                        $stmt->execute([$userdata['id']]);
                        $merchantDetails = $stmt->fetch(PDO::FETCH_ASSOC);

                        if ($merchantDetails) {
                            $ubid_acbin = $merchantDetails['ubid_acbin'];
                            $x_acbin = $merchantDetails['x_acbin'];
                            $at_acbin = $merchantDetails['at_acbin'];

                            // Make the API call
                            $api_url = "https://smartupi.in.net/merchantapi/amazon/tranjection.php?ubid_acbin=$ubid_acbin&x_acbin=$x_acbin&at_acbin=$at_acbin";
                            $response = file_get_contents($api_url);

                            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['success']) && $responseData['success'] === true) {
                                    if (empty($responseData['transactions'])) {
                                        // 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>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    <?php
                                                    $sl = 1;
                                                    foreach ($responseData['transactions'] as $transaction) {
                                                        echo "<tr>
                                                                <td style='display:none;'>" . $sl . "</td>
                                                                <td class='text-dark'>" . $transaction['orderId'] . "</td>
                                                                <td class='text-dark'>Rs." . $transaction['amountReceived'] . "</td>
                                                                <td class='text-dark'>" . $transaction['sender'] . "</td>
                                                                <td class='text-dark'>" . $transaction['bankReferenceId'] . "</td>
                                                                <td class='text-dark'>" . $transaction['paymentMethod'] . "</td>
                                                                <td class='text-dark'><b>" . strtoupper($transaction['status']) . "</b></td>
                                                              </tr>";
                                                        $sl++;
                                                    }
                                                    ?>
                                                </tbody>
                                            </table>
                                        </div>
                                        <?php
                                    }
                                } else {
                                    echo "<div class='container-fluid'>
                                            <div class='alert alert-warning'>
                                                <strong>警告！</strong> 无效的 API 响应。
                                            </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');
?>