<?php
require('top.inc.php');

if (isset($_GET['type']) && $_GET['type'] != '') {
    $type = get_safe_value($con, $_GET['type']);
    if ($type == 'delete') {
        $id = get_safe_value($con, $_GET['id']);
        $delete_sql = "delete from 500_ap where id='$id'";
        mysqli_query($con, $delete_sql);
    }
}

$search = "";
$where = "";
if (isset($_GET['search']) && $_GET['search'] != '') {
    $search = get_safe_value($con, $_GET['search']);
    $where = " WHERE Contact_No LIKE '%$search%' ";
}

$result = $con->query("SELECT * FROM 500_ap $where ORDER BY id DESC");
?>
<div class="row mt-4">
    <div class="col-xl-12">
        <div class="card">
            <div class="card-body">
                <div class="row mb-3">
                    <div class="col-md-6">
                        <h4 class="box-title">Manage User Data </h4>
                    </div>
                    <div class="col-md-6 text-right">
                        <form method="get" class="form-inline float-right">
                            <input type="text" name="search" class="form-control mr-sm-2" placeholder="Search Mobile No" value="<?php echo $search ?>">
                            <button type="submit" class="btn btn-primary">Search</button>
                            <?php if($search != "") { ?>
                                <a href="show.php" class="btn btn-secondary ml-1">Clear</a>
                            <?php } ?>
                        </form>
                    </div>
                </div>
                <div class="table-responsive">
                    <table class="table table-striped">
                        <thead>
                            <tr>
                                <th>Contact No</th>
                                <th>Buyer Name</th>
                                <th>Product</th>
                                <th>Price</th>
                                <th>UPI ID</th>
                                <th>Amount</th>
                                <th width="15%">Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            if ($result->num_rows > 0) {
                                while ($row = $result->fetch_assoc()) {
                                    echo "<tr>";
                                    echo "<td>" . $row["Contact_No"] . "</td>";
                                    echo "<td>" . $row["Buyer_name"] . "</td>";
                                    echo "<td>" . $row["Product"] . "</td>";
                                    echo "<td>" . $row["price"] . "</td>";
                                    echo "<td>" . $row["upi_id"] . "</td>";
                                    echo "<td>" . $row["payment_amount"] . "</td>";
                                    echo "<td>";
                                    echo "<a href='manage_user.php?id=" . $row["id"] . "' class='btn btn-sm btn-info mr-1'>Edit</a>";
                                    echo "<a href='?type=delete&id=" . $row["id"] . "' class='btn btn-sm btn-danger' onclick='return confirm(\"Are you sure?\")'>Delete</a>";
                                    echo "</td>";
                                    echo "</tr>";
                                }
                            } else {
                                echo "<tr><td colspan='7' class='text-center'>No data found</td></tr>";
                            }
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<?php
require('footer.inc.php');
?>
