<?php
include('header.php');

// Check if the user is logged in
if (!isset($_SESSION['phone'])) {
    header("Location: index.php");
    exit();
}

// Initialize variables
$transactions = [];
$searchPerformed = false;
$noDataFound = false;
$dataFetchedSuccessfully = false;

// Process form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['mobile'])) {
    // Get the provided mobile number
    $mobile = mysqli_real_escape_string($conn, $_POST['mobile']);

    // Query to fetch transactions by mobile number
    $query = "SELECT * FROM `upitxn` WHERE userid = '$mobile' ORDER BY `date` DESC";
    $result = mysqli_query($conn, $query);

    if (!$result) {
        die("Error: " . mysqli_error($conn));
    }

    // Fetch transactions into an array
    while ($row = mysqli_fetch_assoc($result)) {
        $transactions[] = $row;
    }
    
    // Check if no transactions were found
    if (empty($transactions)) {
        $noDataFound = true;
    } else {
        $dataFetchedSuccessfully = true;
    }

    $searchPerformed = true;
}
?>

<div class="content-wrapper">
    <section class="content">
        <div class="container-fluid">
            <div class="row">
                <div class="col-12">
                    <div class="card">
                        <div class="card-header">
                            <h3 class="card-title">Find Wallet Transactions</h3>
                        </div>
                        <!-- /.card-header -->
                        <div class="card-body">
                            <!-- Search Form -->
                            <form method="POST" action="">
                                <div class="row justify-content-right">
                                    <div class="col-md-4">
                                        <div class="form-group">
                                            <!-- Input field for mobile number -->
                                            <input type="text" name="mobile" class="form-control mb-2" placeholder="Enter Mobile Number" required>
                                            <!-- Search button, placed below the input field -->
                                            <button class="btn btn-primary btn-block" type="submit">Search</button>
                                        </div>
                                    </div>
                                </div>
                            </form>
                            <!-- /.Search Form -->

                            <?php if ($searchPerformed && !$noDataFound): ?>
                                 <table id="transactionTable" class="table table-bordered table-hover">
                                    <thead>
                                        <tr>
                                            <th>SL. No.</th>
                                            <th>Amount</th>
                                            <th>New Balance</th>
                                          <th>UPI ID</th>
                                            <th>Status</th>
                                           
                                            <th>Date</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                        // Initialize a counter variable
                                        $serial = 1;
                                        // Display transaction history in table rows
                                        foreach ($transactions as $row) {
                                            echo "<tr>";
                                            echo "<td>" . $serial . "</td>";
                                            echo "<td>" . htmlspecialchars($row['amount']) . "</td>";
                                            echo "<td>" . htmlspecialchars($row['walletamount']) . "</td>";
                                            echo "<td>" . htmlspecialchars($row['utr']) . "</td>";
                                            echo "<td>" . htmlspecialchars($row['status']) . "</td>";
                                           
                                            echo "<td>" . htmlspecialchars($row['txn_date']) . "</td>";
                                            echo "</tr>";
                                            // Increment the counter
                                            $serial++;
                                        }
                                        ?>
                                    </tbody>
                                </table>
                            <?php elseif ($searchPerformed && $noDataFound): ?>
                                <p>No transactions found. Please enter a mobile number to search.</p>
                            <?php endif; ?>
                        </div>
                        <!-- /.card-body -->
                    </div>
                    <!-- /.card -->
                </div>
            </div>
        </div><!-- /.container-fluid -->
    </section>
    <!-- /.content -->
</div>
<!-- /.content-wrapper -->

<!-- Include footer if it's not already included -->
<?php include('footer.php'); ?>

<!-- Include SweetAlert library -->
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

<script>
$(document).ready(function() {
    $('#transactionTable').DataTable({
        dom: 'Bfrtip',
        responsive: true,
        pageLength: 25,
        buttons: ['copy', 'csv', 'excel', 'pdf', 'print']
    });

    <?php if ($noDataFound): ?>
        Swal.fire({
            icon: 'info',
            title: 'No Data Found',
            text: 'No transactions found for the entered mobile number.',
            confirmButtonText: 'OK'
        });
    <?php elseif ($dataFetchedSuccessfully): ?>
        Swal.fire({
            icon: 'success',
            title: 'Data Fetched Successfully',
            text: 'Transactions have been successfully retrieved.',
            confirmButtonText: 'OK'
        });
    <?php endif; ?>
});
</script>
