<!-- Add a search input field -->
<input type="text" id="searchInput" placeholder="Rechercher un utilisateur..." class="form-control" style="margin-bottom: 20px;">

<div class="row" id="dataToMap"></div>

<script>
    // Function to filter users based on search query
    function filterUsers(query) {
        const filteredUsers = allUsers.filter(user =>
            user.username.toLowerCase().includes(query.toLowerCase()) ||
            user.email.toLowerCase().includes(query.toLowerCase())
        );
        renderUsers(filteredUsers); // Render filtered users
    }

    // Add event listener to the search input
    document.getElementById('searchInput').addEventListener('input', function (e) {
        const query = e.target.value; // Get the search query
        filterUsers(query); // Filter and render users
    });

    // Render all users initially
    renderUsers(allUsers);
</script>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>User List</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <style>
        .small-box {
            padding: 20px;
            border-radius: 5px;
            margin-bottom: 20px;
        }
    </style>
</head>
<body>
    <div class="container">
        <!-- Search Input -->
        <input type="text" id="searchInput" placeholder="Rechercher un utilisateur..." class="form-control" style="margin-bottom: 20px;">

        <!-- User List Container -->
        <div class="row" id="dataToMap"></div>
    </div>

    <script>
        // Convert PHP array to JavaScript array
        const allUsers = <?php echo json_encode($allusers); ?>;

        // Function to render users
        function renderUsers(users) {
            const container = document.getElementById('dataToMap');
            container.innerHTML = ''; // Clear existing content

            users.forEach(user => {
                const userHtml = `
                    <div class="col-lg-3 col-xs-6">
                        <div class="small-box" style="background-color: pink;">
                            <div class="inner">
                                <h3>${user.username}</h3>
                                <p>Produits vendues <b style="font-size: 20px;">${user.totalVente}</b></p>
                            </div>
                            <div class="icon">
                                <i class="ion ion-stats-bars"></i>
                            </div>
                            <a href="<?php echo base_url('products/') ?>" class="small-box-footer">
                                Plus d'information <i class="fa fa-arrow-circle-right"></i>
                            </a>
                        </div>
                    </div>
                `;
                container.innerHTML += userHtml; // Append the user HTML to the container
            });
        }

        // Function to filter users based on search query
        function filterUsers(query) {
            const filteredUsers = allUsers.filter(user =>
                user.username.toLowerCase().includes(query.toLowerCase()) ||
                user.email.toLowerCase().includes(query.toLowerCase())
            );
            renderUsers(filteredUsers); // Render filtered users
        }

        // Add event listener to the search input
        document.getElementById('searchInput').addEventListener('input', function (e) {
            const query = e.target.value; // Get the search query
            filterUsers(query); // Filter and render users
        });

        // Render all users initially
        renderUsers(allUsers);
    </script>
</body>
</html>