<?php
session_start();
include "../connection.php"; 

if (!isset($_SESSION['userid'])) {
    if (isset($_COOKIE['userid'])) {
        $_SESSION['userid'] = $_COOKIE['userid'];
    } else {
        header("Location: ../login.php");
        exit();
    }
}

// Fetch all offers with SIM names and area names

$userid = $_SESSION['userid'];

$sql = " SELECT * FROM student WHERE refer_id = $userid ";
	
$result = mysqli_query($con, $sql);
$offers = [];
if ($result) {
    while ($row = mysqli_fetch_assoc($result)) {
        $offers[] = $row;
    }
}




?>

<?php include 'header-link.php' ?>

<main>

	<?php include 'top-bar.php' ?>

    <section>
        <div class="container mt-2 pb-5">
            <div class="row justify-content-center">
                <div class="col-md-6">
                    <div class="card custom-card">
                        <div class="card-body">
							
							<div class="text-center">
								<i class="profile-icon fa-solid fa-users"></i>
							</div>
						
                            <h6 class="card-title text-center my-3"> আপনার রিসেলার </h6>

                            

                            <input type="text" class="form-control mb-4" id="searchInput" placeholder=" রিসেলার খুঁজুন ..." onkeyup="filterOffers()">

                            <div class="offer-list" id="offerList">
                                <?php foreach ($offers as $offer): ?>
                                    <div class="offer-card mb-3 p-3">
                                        <div class="row">
                                            <div class="col">
                                                <small class="fw-bold"> নাম : <?php echo $offer['username']; ?></small><br>
                                                <small> নাম্বার : <?php echo $offer['number']; ?></small><br>
                                                
                                            </div>
                                            <div class="col text-end">
                                               <a href="send-money-reseller.php?userid=<?php echo $offer['userid']; ?>" class="btn custom-button w-100 mx-auto"> সেন্ড মানি  <i class="fa-duotone fa-solid fa-paper-plane"></i> </a>
                                            </div>
                                        </div>
                                        
                                    </div>
                                <?php endforeach; ?>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>
</main>

<script>
function filterOffers() {
    const searchInput = document.getElementById('searchInput').value.toLowerCase();
    const offerCards = document.querySelectorAll('.offer-card');

    offerCards.forEach(card => {
        const offerText = Array.from(card.querySelectorAll('small')).map(small => small.textContent.toLowerCase()).join(' ');

        const matchesSearch = offerText.includes(searchInput);

        if (matchesSearch) {
            card.style.display = '';
        } else {
            card.style.display = 'none';
        }
    });
}

</script>


</body>
</html>
