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

                        if ($merchantDetails) {
                            $access_token = $merchantDetails['access_token'];

                            // Make the API call to the new Mobikwik API
                            $api_url = "https://smartupi.in.net/merchantapi/mobikwik/history-v3.php?Authorization=$access_token";
                            $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 if the API response is successful
                                if (isset($responseData['success']) && $responseData['success'] === true) {
                                    $transactions = $responseData['data']['historyData'];

                                    if (empty($transactions)) {
                                        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 ($transactions as $transaction) {
                                                        echo "<tr>
                                                            <td style='display:none;'>" . $sl . "</td>
                                                            <td class='text-dark'>" . $transaction['transactionId'] . "</td>
                                                            <td class='text-dark'>Rs." . $transaction['amount'] . "</td>
                                                            <td class='text-dark'>" . $transaction['beneficiaryName'] . "</td>
                                                            <td class='text-dark'>" . $transaction['rrn'] . "</td>
                                                            <td class='text-dark'><b>" . strtoupper($transaction['status']) . "</b></td>
                                                            <td class='text-dark'>" . date('Y-m-d H:i:s', $transaction['date'] / 1000) . "</td>
                                                        </tr>";
                                                        $sl++;
                                                    }
                                                    ?>
                                                </tbody>
                                            </table>
                                        </div>
                                        <?php
                                    }
                                } else {
                                    // Handle API error response
                                    $errorMessage = isset($responseData['message']['text']) ? $responseData['message']['text'] : 'Unknown error occurred.';
                                    echo "<div class='container-fluid'>
                                        <div class='alert alert-warning'>
                                            <strong>警告！</strong> $errorMessage
                                        </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> 未找到 Mobikwik 商户详情。
                                </div>
                                <a href='add-mobikwik.php' class='btn btn-dark'>添加商户</a>
                            </div>";
                        }
                        ?>
                    </div>
                </div>
            </div>
        </div>
    </div>


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