<%- include('../partials/header.ejs') -%>
<link rel="stylesheet" href="css/manageorders.css">
<br>
<br>
<div class="container table-responsive table-wrapper">
        <div class="table-title">
            <div class="row">
                <div class="col-sm-12">
                    <h2>User <b>Management</b></h2>
                </div>
              
            </div>
        </div>
        <table class="table table-striped table-hover"id="musers">
            <thead>
                <tr>
                    <th>#</th>
                    <th>first name</th>						
                    <th>last name</th>
                    <th>Role</th>
                    <th>Email</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tbody>
                <% users.forEach((user)=>{ %>
                <tr>
                    <td><%=user.user_id %></td>
                    <td><%=user.first_name %></td>
                    <td><%=user.last_name %></td>                        
                    <td><%=user.isAdmin %></td>
                    <td><%=user.email %></td>
                    <td>
                        <div class="form-check">
                            <label class="form-check-label">
                              <input type="checkbox" class="form-check-input" value="<%=user.user_id %>" id="check" <% if(user.isAdmin){ console.log(user.isAdmin)%> checked <% } %>>Is Admin
                            </label>
                          </div>
                          
                    </td>
                </tr> 
                <% }) %>
            </tbody>
        </table>        
</div>   
<script>
    $('input[type="checkbox"]').on('click', function(){

        var data = {};
        data.id = $(this).attr('value');
        data.value = $(this).is(':checked') ? 1 : 0;

        console.log(data);
        var xhttp = new XMLHttpRequest();
xhttp.open("POST", "/manageusers.html/checkUpdate", true); 
xhttp.setRequestHeader("Content-Type", "application/json");
xhttp.onreadystatechange = function() {
   if (this.readyState == 4 && this.status == 200) {
     // Response
     var response = this.responseText;
   }
};
// var data = {name:'yogesh',salary: 35000,email: 'yogesh@makitweb.com'};
xhttp.send(JSON.stringify(data));

    });

</script> 

    <%- include( '../partials/footer.ejs') -%>;