<%- include('../partials/head.ejs') %>
  <!--
`body` tag options:

  Apply one or more of the following classes to to the body tag
  to get the desired effect

  * sidebar-collapse
  * sidebar-mini
-->

  <body class="hold-transition sidebar-mini">
    <div class="wrapper">
      <!-- Navbar -->
      <%- include('../partials/navigation.ejs') %>
        <!-- /.navbar -->

        <!-- Main Sidebar Container -->

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

          <!-- Content Wrapper. Contains page content -->
          <div class="content-wrapper">
            <!-- Content Header (Page header) -->
            <div class="content-header">
              <div class="container-fluid">
                <div class="row mb-2">
                  <div class="col-sm-6">
                    <h1 class="m-0">Manage Users</h1>
                  </div><!-- /.col -->
                  <div class="col-sm-6">
                    <ol class="breadcrumb float-sm-right">
                      <li class="breadcrumb-item"><a href="/admin">Home</a></li>
                      <li class="breadcrumb-item active">Manage Users</li>
                    </ol>
                  </div><!-- /.col -->
                </div><!-- /.row -->
              </div><!-- /.container-fluid -->
            </div>
            <!-- /.content-header -->

            <!-- Main content -->
            <div class="content">
              <div class="container-fluid">
                <div class="row">

                  <!-- /.col-md-6 -->
                  <div class="col-lg-12">
                    <div class="pt-20">
                      <% if(errors.length> 0){ %>
                        <div class="alert alert-danger">
                          <% errors.forEach(function(error){ %>
                            <% if(error !='' ){ %>
                                <%= error %>
                              <% }}); %>
                        </div>
                        <% } %>
                          <% if(success.length> 0){ %>
                            <div class="alert alert-success">
                                <%=success %>
                            </div>
                            <% } %>
                    </div>
                    <div class="card">
                      <div class="card-header">
                        <h3 class="card-title">All Users</h3>
                        <!-- <a href="/admin/customer" class="float-right btn btn-success">Add New</a> -->
                      </div>
                      <div class="card-body">
                        <table id="customer_datatable" class="table table-bordered table-hover">
                          <thead>
                            <tr>
                              <th>Index</th>
                              <th >Name</th>
                              <th class="col-2">Email</th>
                              <th>Token Earned</th>
                              <th>Current Balance</th>
                              <th>Joining Date</th>
                              <th>Updated At</th>
                              <th class="col-2">Actions</th>
                            </tr>
                          </thead>
                          <tbody>
                            <!-- <% cnt = 1 %> -->
                            <% data.forEach((itm,index)=> { %>
                              <tr key="<%= index %>">
                                <td>
                                    <%= index + 1 %>
                                </td>
                                <td>
                                  <%= itm.name %>
                                </td>
                                <td>
                                  <%= itm.email %>
                                </td>
                                <td>
                                  <% if(itm.UserTokens !== null && typeof itm.UserTokens !== 'undefined' && itm.UserTokens.length > 0) { %>
                                    <% itm.UserTokens.forEach((tok, index)=> {   %>
                                      <%= index == 0 ? tok.total_earned :  '' %>
                                    <% }) %>
                                  <% }else{ %>
                                    <%= 0 %>
                                  <% } %>
                                </td>
                                <td>
                                  <% if(itm.UserTokens !== null && typeof itm.UserTokens !== 'undefined' && itm.UserTokens.length > 0) { %>
                                    <% itm.UserTokens.forEach((tok, index)=> {   %>
                                      <%= index == 0 ? tok.current_balance :  '' %>
                                    <% }) %>
                                  <% }else{ %>
                                    <%= 0 %>
                                  <% } %>
                                </td>
                                <td>
                                  <%= moment(itm.createdAt).format('MMM/Do/YY h:mm') %>
                                </td>
                                <td>
                                  <%= moment(itm.updatedAt).format('MMM/Do/YY h:mm') %>
                                </td>
                                <td class="project-actions">
                                  <a class="btn btn-primary btn-sm" href="/admin/user/view/<%=itm.id %>">
                                    <i class="fas fa-eye">
                                    </i>
                                  </a>
                                  <% if(itm.account_status === 2){ %>
                                    <button class="btn btn-danger btn-sm deleteCustomer" data-id="<%= itm.id %>" href="#"
                                      data-toggle="modal" data-target="#modal-danger">
                                      <i class="fas fa-trash">
                                      </i>
                                    </button> 
                                  <% }%>
                                  <% if(itm.account_status > 0){ %>
                                  <button class="btn btn-warning btn-sm blockCustomer" title="Block" data-id="<%= itm.id %>" href="#"
                                    data-toggle="modal" data-target="#modal-block">
                                    <i class="fas fa-lock">
                                    </i>
                                  </button>
                                  <% }else{ %>
                                  <button class="btn btn-success btn-sm unblockCustomer" title="Unblock" data-id="<%= itm.id %>" href="#"
                                    data-toggle="modal" data-target="#modal-unblock">
                                    <i class="fas fa-unlock">
                                    </i>
                                  </button>
                                  <% } %>
                                </td>
                              </tr>
                              <% }) %>

                          </tbody>
                        </table>
                      </div>
                    </div>

                  </div>
                  <!-- /.col-md-6 -->
                </div>
                <!-- /.row -->
              </div>
              <!-- /.container-fluid -->
            </div>
            <!-- /.content -->
          </div>
          <!-- /.content-wrapper -->

          <!-- Control Sidebar -->
          <aside class="control-sidebar control-sidebar-dark">
            <!-- Control sidebar content goes here -->
          </aside>
          <!-- /.control-sidebar -->

          <!-- Main Footer -->
          <!-- /.modal -->

          <div class="modal fade" id="modal-danger">
            <div class="modal-dialog">
              <div class="modal-content bg-danger">
                <div class="modal-header">
                  <h4 class="modal-title">Delete Customer</h4>
                  <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                  </button>
                </div>
                <div class="modal-body">
                  <p>This user will be deleted permanently.</p>
                </div>
                <div class="modal-footer justify-content-between">
                  <button type="button" class="btn btn-outline-light" data-dismiss="modal">Cancel</button>
                  <form class="deleteCustomerForm" action="" method="POST" data-url="/admin/user/delete/">
                    <button type="submit" class="btn btn-outline-light">Delete</button>
                  </form>
                </div>
              </div>
              <!-- /.modal-content -->
            </div>
            <!-- /.modal-dialog -->
          </div>
          <!-- /.modal -->
          <!-- Block modal -->
          <div class="modal fade" id="modal-block">
            <div class="modal-dialog modal-dialog-centered">
              <div class="modal-content bg-secondary">
                <div class="modal-header">
                <h4 class="modal-title">Block User</h4>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                </div>
                <div class="modal-body">
                <p>You really want to block this user.</p>
                </div>
                <div class="modal-footer justify-content-between">
                <button type="button" class="btn btn-outline-light" data-dismiss="modal">Cancel</button>
                <form class="blockCustomerForm" action="" method="POST" data-url="/admin/user/block/">
                    <button type="submit" class="btn btn-outline-light">Block</button>
                </form>
                </div>
              </div>
              <!-- /.modal-content -->
            </div>
            <!-- /.modal-dialog -->
          </div>
          <!-- unblock modal -->
          <div class="modal fade" id="modal-unblock">
            <div class="modal-dialog modal-dialog-centered">
              <div class="modal-content bg-secondary">
                <div class="modal-header">
                <h4 class="modal-title">Unblock User</h4>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                </div>
                <div class="modal-body">
                <p>You really want to unblock this user.</p>
                </div>
                <div class="modal-footer justify-content-between">
                  <button type="button" class="btn btn-outline-light" data-dismiss="modal">Cancel</button>
                  <form class="unblockCustomerForm" action="" method="POST" data-url="/admin/user/unblock/">
                    <button type="submit" class="btn btn-outline-light">Unblock</button>
                  </form>
                </div>
              </div>
            </div>
          </div>
        <!-- /.modal -->
    </div>
    <!-- ./wrapper -->

    <!-- REQUIRED SCRIPTS -->

    <!-- jQuery -->

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

  </body>