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

<?php
  // Retrieve data
  $sql = "SELECT drive_package_rqst.id, student.username, student.number, 
  drive_package_rqst.offer_rqst, drive_package_rqst.mobile_no, 
  drive_package_rqst.status, all_drive_package.offer_name, 
  sim_entry.sim_id,sim_entry.sim_name, district.dist_id,district.dist_name, all_drive_package.drive_total 
  FROM drive_package_rqst 
  LEFT JOIN student ON drive_package_rqst.userid = student.userid
  LEFT JOIN all_drive_package ON drive_package_rqst.offer_rqst = all_drive_package.offer_id
  LEFT JOIN sim_entry ON all_drive_package.offer_sim = sim_entry.sim_id
  LEFT JOIN district ON all_drive_package.offer_area = district.dist_id
  ORDER BY FIELD(drive_package_rqst.status, 'Pending', 'Accepted', 'Rejected') ASC";
$result = $con->query($sql);
$sl=0;

if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['delete_id'])) {
$delete_id = $_POST['delete_id'];
$delete_sql = "DELETE FROM drive_package_rqst WHERE id = ?";
$stmt = $con->prepare($delete_sql);
$stmt->bind_param("i", $delete_id);
$stmt->execute();
header("Location: offer-rqst.php"); 
exit();
}
?>


<main>
    <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-duotone fa-solid fa-envelope-open-dollar"></i>
                            </div>

                            <h6 class="card-title text-center my-3"> Offer Recharge (<?php echo $BuyPack; ?>) </h6>
                           
                            

                            <input type="text" class="form-control mb-4" id="searchInput" placeholder="Search User..." onkeyup="filterOffers()">


                            <div class="offer-list" id="offerList">
                                <?php if ($result->num_rows > 0): ?>
                                <?php while($row = $result->fetch_assoc()): ?>
                                    <div class="offer-card mb-5 p-3" data-sim="<?php echo $offer['offer_sim']; ?>">
                                        <div class="row">
                                            <div class="col">
                                                <small class="fw-bold">A/C Name: <?php echo $row['username']; ?></small><br>
                                                <small>A/C Contact: <?php echo $row['number']; ?></small><br>
                                                
                                                <small> Status : <?php echo $row['status']; ?></small>
                                            </div>
                                            <div class="col text-end">
                                                <small class="fw-bold">Offer: <?php echo $row['offer_name']; ?> </small><br>
                                                <small>Sim: <?php echo $row['sim_name']; ?> </small><br>
                                                <small class="fw-bold"> Ammount : <?php echo $row['drive_total']; ?></small><br>

                                                <small> Mob No: <?php echo $row['mobile_no']; ?></small><br>
                                                <small> Area : <?php echo $row['dist_name']; ?></small>
                                            </div>
                                        </div>
                                        <div class="d-grid text-center">
                                        <a class="btn custom-button mx-3" href="edit-offer-rqst.php?id=<?php echo $row['id']; ?>"> <i
                                        class="font fa-solid fa-eye"></i> View </a>  
                                        </div>
                                    </div>
                                <?php endwhile; ?>
                                <?php else: ?>
									<small>No records found.</small>
									<?php endif; ?>
                            </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 cardText = card.textContent.toLowerCase();
        
        if (cardText.includes(searchInput)) {
            card.style.display = ''; // Show the card
        } else {
            card.style.display = 'none'; // Hide the card
        }
    });
}
</script>


</body>
</html>
