<?php
require_once("../../connection/db.php"); // This gives you getDB()
$pdo = getDB();
?>
<style>
    .highlight {
        background-color: yellow;
    }
</style>
<div class="row clearfix">
    <div class="col-lg-12 col-md-12 col-sm-12">
        <div class="card">
            <div class="header">
                <h2>Search Member Result</h2>
            </div>
            <div class="body">
                <div class="table-responsive">
                    <table class="table table-hover js-basic-example dataTable table-custom mb-0">
                        <thead>
                            <tr>
                                <th>#</th>
                                <th>User Id</th>
                                <th>Wallet Address</th>
                                <th>Phone</th>
                                <th>Sponsor ID</th>
                                <th>Joining Date</th>
                                <th>More Details</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            // Helper functions using PDO
                            function getName(PDO $pdo, $member_id)
                            {
                                $stmt = $pdo->prepare("SELECT walletAddress FROM meddolic_user_details WHERE member_id=?");
                                $stmt->execute([$member_id]);
                                return $stmt->fetchColumn();
                            }

                            function getUserId(PDO $pdo, $member_id)
                            {
                                $stmt = $pdo->prepare("SELECT user_id FROM meddolic_user_details WHERE member_id=?");
                                $stmt->execute([$member_id]);
                                return $stmt->fetchColumn();
                            }

                            $i = 1;

                            if (!empty($_GET['search_value'])) {
                                $s_value = $_GET['search_value'];

                                $stmt = $pdo->prepare("SELECT * FROM meddolic_user_details WHERE CONCAT(user_id, walletAddress) LIKE ? AND user_type = 2");
                                $stmt->execute(['%' . $s_value . '%']);
                                $results = $stmt->fetchAll(PDO::FETCH_ASSOC);

                                if (count($results) === 0) {
                                    echo '<tr><td colspan="9"><center><b>No Record Found</b></center></td></tr>';
                                } else {
                                    foreach ($results as $val) {
                                        echo "<tr>";
                                        echo "<td>{$i}</td>";
                                        echo "<td>" . str_ireplace($s_value, "<span class='highlight'>{$s_value}</span>", $val['user_id']) . "</td>";
                                        echo "<td>" . str_ireplace($s_value, "<span class='highlight'>{$s_value}</span>", $val['walletAddress']) . "</td>";
                                        echo "<td>" . str_ireplace($s_value, "<span class='highlight'>{$s_value}</span>", getUserId($pdo, $val['sponser_id'])) . "</td>";
                                        echo "<td>" . str_ireplace($s_value, "<span class='highlight'>{$s_value}</span>", getName($pdo, $val['sponser_id'])) . "</td>";
                                        echo "<td>" . date("d-m-Y H:i:s", strtotime($val['date_time'])) . "</td>";
                                        echo "<td>";
                                        echo "<a class='btn btn-success btn-sm' href='viewMemberDetails.php?user_id={$val['user_id']}'>More</a> &nbsp;";

                                        echo "</td>";
                                        echo "</tr>";
                                        $i++;
                                    }
                                }
                            }
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>