<?php
session_start(); 
$u_uname = '';
if (isset($_COOKIE['auth_token'])) {
    $u_uname = $_COOKIE['auth_token'];
} else {
    header("location:login.php");
    exit; 
}
?>
<style>
        .switch {
            position: relative;
            display: inline-block;
            width: 60px;
            height: 34px;
        }

        .switch input { 
            opacity: 0;
            width: 0;
            height: 0;
        }

        .slider {
            position: absolute;
            cursor: pointer;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background-color: #ccc;
            transition: .4s;
        }

        .slider:before {
            position: absolute;
            content: "";
            height: 26px;
            width: 26px;
            left: 4px;
            bottom: 4px;
            background-color: white;
            transition: .4s;
        }

        input:checked + .slider {
            background-color: #2196F3;
        }

        input:focus + .slider {
            box-shadow: 0 0 1px #2196F3;
        }

        input:checked + .slider:before {
            transform: translateX(26px);
        }

        .slider.round {
            border-radius: 34px;
        }

        .slider.round:before {
            border-radius: 50%;
        }
    </style>
<?php include 'include/header.php'; ?>
<?php include 'include/aside.php'; ?>
<?php include 'include/connection.php'; ?>

<!--start page wrapper -->
<div class="page-wrapper">
    <div class="page-content">
        <h6 class="mb-0 text-uppercase">All users</h6>
        <hr/>

        <div class="card">
            <div class="card-body">
                <div class="table-responsive">
                    <table id="example2" class="table table-striped table-bordered">
                        <thead>
                            <tr>
                                <th>SR. No</th>
                                <th>Full Name</th>
                                <th>Email ID</th>
                                <th>User ID</th>
                                <th>Mobile NO</th>
                                <th>Gender</th>
                                <th>Company Name</th>
                                <th>Company Address</th>
                                 <th>Blocking Status</th>
                                <th>Wallet Balance</th>
                                <th>Date</th>
                                <th>Password</th>
                                <th>Edit</th>
                                
                              
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $sql = "SELECT * FROM  users";
                            $result = $link->query($sql);
                            $sr = 1; // Initialize serial number
                            
                            if ($result->num_rows > 0) {
                                // Output data of each row
                                while($row = $result->fetch_assoc()) {
                                    ?>
                                    
                                    <tr>
                                    <td><?php echo  $row['id'] ?></td>
                                    <td><?php echo$row['full_name'] ?></td>
                                    <td><?php echo $row['email_id'] ?></td>
                                    
                                    <td><?php echo $row['user_id'] ?></td>
                                  
                                    <td><?php echo  $row['mobile_no'] ?></td>
                                    <td><?php echo$row['gender'] ?></td>
                                    <td><?php echo $row['company_name'] ?></td>
                                    <td><?php echo $row['company_address'] ?></td>
                                  
                                    <!--<td><?php echo  $row['blocking_status'] ?></td>-->
                                    
                                     <!--<td>-->
                                     <!--           <label class="switch">-->
                                     <!--               <input type="checkbox" <?php echo $row['blocking_status'] == 0 ? 'checked' : '' ?> -->
                                     <!--                      onclick="toggleBlockingStatus('<?php echo $row['user_id'] ?>', '<?php echo $row['blocking_status'] ?>');">-->
                                     <!--               <span class="slider round"></span>-->
                                     <!--           </label>-->
                                     <!--       </td>-->
                          <td>
    <label class="switch">
        <input type="checkbox" 
               <?php echo $row['blocking_status'] == 0 ? 'checked' : ''; ?> 
               onclick="toggleBlockingStatus('<?php echo $row['user_id']; ?>', this.checked);">
        <span class="slider round"></span>
    </label>
</td>


                                    <td><?php echo$row['wallet_balance'] ?></td>
                                    <td><?php echo $row['date_of_joining'] ?></td>
                                    <td><?php echo $row['password'] ?></td>
                                    <td>
                                        <button class="btn btn-primary"><a style="color:white" href="edit_client.php?id=<?php echo $row['id'] ?>">Edit</a></button>
                                    </td>

                                    </tr>
                                    <?php
                                    $sr++; 
                                }
                            } else {
                                echo "<tr><td colspan='8'>No data available</td></tr>";
                            }
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<!--end page wrapper -->

<?php include 'include/footer.php'; ?>





 <script>
        function toggleBlockingStatus(userId, isChecked) {
    var newStatus = isChecked ? 0 : 1; // If checked, set to 0; otherwise, set to 1

    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState === 4) {
            if (xmlhttp.status === 200) {
                alert("Status updated successfully.");
            } else {
                alert("Failed to update status. Please try again.");
                location.reload(); // Reload to correct visual state if update fails
            }
        }
    };

    xmlhttp.open("GET", "action/blocking_status.php?uid=" + userId + "&status=" + newStatus, true);
    xmlhttp.send();
}

    </script>
