<%- include('../include/header'); %>
    <link rel="stylesheet" href="modules/datatables/datatables.min.css">
    <link rel="stylesheet" href="modules/datatables/DataTables-1.10.16/css/dataTables.bootstrap4.min.css">
    <!-- Main Content -->
    <div class="main-content">
        <section class="section">
            <div class="section-header">
                <h1>Users</h1>
                <div class="section-header-breadcrumb">
                    <div class="breadcrumb-item active"><a href="dashbOard">Dashboard</a></div>
                    <div class="breadcrumb-item"><a href="javascript:void (0)">Users</a></div>
                    <div class="breadcrumb-item">Users List</div>
                </div>
            </div>

            <div class="section-body">
                <div class="row">
                    <div class="col-12 col-md-12 col-lg-12">
                        <div class="card">
                            <div class="card-header">
                                <h4>Users List</h4>
                            </div>
                            <div class="card-body">
                                <div class="table-responsive">
                                    <table class="table table-striped" id="table-1">
                                        <thead>
                                            <tr>
                                                <th>#</th>
                                                <th>Fullname</th>
                                                <th>Mobile</th>
                                                <th>Email</th>
                                                <th>D.o.b</th>
                                                <th>Gender</th>
                                                <th>Last Login</th>
                                                <th>Register Date</th>
                                                <th>Status</th>
                                                <th>Action</th>
                                                <th>All Orders</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <% if(users.length> 0){ users.forEach((user,index) => { %>

                                                <tr>
                                                    <td>
                                                        <%= index +1 %>
                                                    </td>
                                                    <td>
                                                        <%= user.fullname %>
                                                    </td>
                                                    <td>
                                                        <%= user.mobile %>
                                                    </td>
                                                    <td>
                                                        <%= user.email %>
                                                    </td>
                                                    <td><%= user.dob != null ? moment(user.dob).format('Do MMMM YYYY') : '---'  %>
                                                    </td>
                                                    <td>
                                                        <%= user.gender == 1 ? 'Male' : user.gender == 0 ? 'Female' : '---' %>
                                                    </td>
                                                    <td>
                                                        <%= user.last_logged_in != null ? moment(user.last_logged_in).format('Do MMMM YYYY, h:mm:ss a') : '---'  %>
                                                    </td>
                                                    <td>
                                                        <%= moment(user.created_date).format('Do MMMM YYYY, h:mm:ss a') %>
                                                    </td>
                                                    <td><span
                                                            class="badge status-<%= user.id %> badge-<%= user.status == '1' ? 'success' : 'danger' %>">
                                                            <%= user.status == '1' ? 'Active' : 'Inactive' %>
                                                        </span>
                                                    </td>
                                                    <td><button class="btn btn-primary"
                                                        onclick="updateStatus(this,'<%= user.id %>','status')"
                                                        data-value="<%= user.status == 1 ? '0' : '1' %>">Change Status</button>
                                                    </td>
                                                    <td>
                                                        <a class="btn btn-primary" href="allOrders?user_id=<%= user.id%>">Show Orders</a>
                                                    </td>
                                                </tr>

                                                <% }); }else{ %>
                                                    <tr class="text-center">
                                                        <th colspan="5">No userss Found</th>
                                                    </tr>
                                                <% } %>
                                        </tbody>
                                    </table>

                                    <!-- pagination Custom Code -->
                                    <div class="d-flex float-right">
                                        <nav aria-label="Page navigation example">
                                            <ul class="pagination">
                                                <% if(current_page> 1){ %>
                                                    <li class="page-item"><a class="page-link"
                                                            href="allUsers?page=<%= current_page == 1 ? 1 : current_page-1 %>">Previous</a>
                                                    </li>
                                                    <li class="page-item"><a class="page-link"
                                                            href="allUsers?page=1">First</a>
                                                    </li>
                                                    <% } %>

                                                        <% var j=1; var leftnum=total_page_count-current_page; var
                                                            startNum=leftnum <=3 && current_page> 1 ? current_page - 2 :
                                                            current_page;
                                                            for(var i = startNum || 1; i <= total_page_count; i++ ){
                                                                if(j <=3 ){ %>
                                                                <li
                                                                    class="page-item <%= current_page == i ? 'active' : '' %>">
                                                                    <a class="page-link"
                                                                        href="allUsers?page=<%= i %>">
                                                                        <%= i %>
                                                                    </a>
                                                                </li>
                                                                <% j++; } }%>


                                                                    <% if(total_page_count> 1 && current_page < total_page_count){ %>
                                                                    <% if(total_page_count> 3){ %>
                                                                                <li class="page-item">...</li>
                                                                    <% } %>
                                                                    <li class="page-item"><a class="page-link" href="allUsers?page=<%= total_page_count %>">Last</a>
                                                                    </li>
                                                                    <li class="page-item"><a
                                                                            class="page-link"
                                                                            href="allUsers?page=<%= current_page+1 %>">Next</a>
                                                                    </li>
                                                                    <% } %>
                                            </ul>
                                        </nav>
                                    </div>
                                    <!-- pagination Custom Code -->
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>
    </div>


    <%- include('../include/footer') %>
        <script src="modules/datatables/datatables.min.js"></script>
        <script src="modules/datatables/DataTables-1.10.16/js/dataTables.bootstrap4.min.js"></script>
        <script>
            $(document).ready(function () {
                $('#table-1').DataTable({
                    paging: false
                });
            });


            // update users Status
            function updateStatus(btn, id, updateColumn) {

                var value = $(btn).attr('data-value');
                var new_value = value == 1 ? 0 : 1


                if (updateColumn == 'status') {
                    $(btn).html('Please Wait...');
                    var new_text = value == 1 ? 'Active' : 'Inactive'
                    var add_class = value == 1 ? 'badge-success' : 'badge-danger'
                    var remove_class = value == 1 ? 'badge-danger' : 'badge-success'
                }
                else{
                    $(btn).after('<span class="fa fa-spinner fa-spin spinner"></span>');
                    $(btn).attr('disabled',true)
                }

                var verified = updateColumn == 'status' && value == 0 ? 0 : 1;
                if (updateColumn == 'status' && value == 0) {
                    if(confirm("Are you sure you want to deactivate?")){
                        verified = 1;
                    }else{
                        $(btn).html('Change Status');
                    }
                }

                if (verified == 1 || updateColumn != 'status') {
                    // axios
                    try {
                        axios.post(`/admin/updateStatus`, { id, status: value, column: updateColumn,table:'users' }).then(res => {
                            show_Toaster(res.data.message, res.data.type);
                            if (res.data.type === 'success') {

                                if (updateColumn == 'status') {
                                    $(btn).html('Change Status');
                                    $(`.status-${id}`).removeClass(remove_class);
                                    $(`.status-${id}`).addClass(add_class);
                                    $(`.status-${id}`).html(new_text);
                                }else{
                                    $('.spinner').remove();
                                    $(btn).attr('disabled',false)
                                }


                                $(btn).attr('data-value', new_value);
                            }

                        }).catch(err => {
                            if (err.response.data.errors) {
                                Object.values(err.response.data.errors).map(item => {
                                    show_Toaster(item[0], 'error');
                                })
                            }
                        });

                    }
                    catch (err) {
                        show_Toaster('Something went wrong, Try again later!!', 'error');
                    }
                }
            }
            
        </script>