<!DOCTYPE html>
<html lang="es">

<head>
  <%- include('../../tools/head'); %>
  <!-- Custom styles for this page -->
  <link href="/vendor/datatables/dataTables.bootstrap4.min.css" rel="stylesheet" />
  <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/amcharts/3.21.15/plugins/export/libs/FileSaver.js/FileSaver.min.js"></script>
  <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.15.6/xlsx.full.min.js"></script>
  <link rel="stylesheet" href="https://cdn.datatables.net/buttons/2.2.3/css/buttons.dataTables.min.css">
</head>

<body id="page-top">
  <!-- Page Wrapper -->
  <div id="wrapper">
    <!-- Sidebar -->
    <%- include('../../tools/sidebarAdmin'); %>
    <!-- End of Sidebar -->

    <!-- Content Wrapper -->
    <div id="content-wrapper" class="d-flex flex-column">
      <!-- Main Content -->
      <div id="content">
        <!-- Topbar -->
        <%- include('../../tools/topbar'); %>
        <!-- End of Topbar -->

        <!-- Begin Page Content -->
        <div class="container-fluid">

          <div class="container">
            <div class="card o-hidden border-0 shadow-lg my-5">
              <div class="card-body p-0">
                <!-- Nested Row within Card Body -->
                <div class="p-5">

                  <div class="text-center">
                    <h1 class="h4 text-gray-900 mb-4">USUARIOS</h1>
                  </div>

                  <!-- DataTales  -->
                  <div class="card-body">
                    <div class="table-responsive">
                      <table class="table table-bordered table-hover display nowrap" id="dataTable" width="100%" cellspacing="0" style="font-size: 13px;  text-align: center !important;">
                        <thead>
                          <tr>
                            <th>Acción</th>
                            <th>Identificación</th>
                            <th>Nombres</th>
                            <th>Apellidos</th>
                            <th>Provincia</th>
                            <th>Municipio</th>
                            <th>Rol</th>
                            <th>Apoyo</th>
                          </tr>
                        </thead>
                        <tfoot>
                          <tr>
                            <th>Acción</th>
                            <th><input type="text" placeholder="Busca por Identificación" /></th>
                            <th><input type="text" placeholder="Busca por Nombres" /></th>
                            <th><input type="text" placeholder="Busca por Apellidos" /></th>
                            <th><input type="text" placeholder="Busca por Provincia" /></th>
                            <th><input type="text" placeholder="Busca por Municipio" /></th>
                            <th><input type="text" placeholder="Busca por Rol" /></th>
                            <th><input type="text" placeholder="Busca por Apoyo" /></th>
                          </tr>
                        </tfoot>
                        <tbody>
                          <% if (users){ %>
                          <% users.forEach((users)=> { %>
                          <tr>
                            <td>
                              <a href="/coordinacion/Cuentas/Usuarios/Edit/<%= users._id %>" type="button" class="btn btn-warning btn-circle btn-sm">
                                <i class="fas fa-pen-to-square"></i>
                              </a>
                              <a onclick='modalsUsuarios.eliminar_modal("<%= users._id %>", "<%= users.user %>", "<%= users.nombre %> <%= users.apellido %>")' type="button" data-target="#deleteModal" data-toggle="modal" class="btn btn-danger btn-circle btn-sm">
                                <i class="fas fa-trash"></i>
                              </a>
                              <a onclick='modalsUsuarios.changePass_modal("<%= users._id %>", "<%= users.user %>")' type="button" data-target="#changePassModal" data-toggle="modal" class="btn btn-success btn-circle btn-sm">
                                <i class="fas fa-key"></i>
                              </a>
                            </td>
                            <td>
                              <%= users.user %>
                            </td>
                            <td>
                              <%= users.nombre %>
                            </td>
                            <td>
                              <%= users.apellido %>
                            </td>
                            <td>
                              <%= users.provincia %>
                            </td>
                            <td>
                              <%= users.municipio %>
                            </td>
                            <td>
                              <%= users.rol %>
                            </td>
                            <td>
                              <%= users.municipioExtra1 %>
                              <br>
                              <%= users.municipioExtra2 %>
                              <br>
                              <%= users.municipioExtra3 %>
                            </td>
                          </tr>
                          <% }); %>
                          <% } %>
                        </tbody>
                      </table>
                    </div>
                  </div>

                </div>

              </div>
            </div>
          </div>
        </div>
        <!-- /.container-fluid -->
      </div>
      <!-- End of Main Content -->

      <!-- Footer -->
      <%- include('../../tools/footer'); %>
      <!-- End of Footer -->
    </div>
    <!-- End of Content Wrapper -->
  </div>
  <!-- End of Page Wrapper -->

  <!-- Scroll to Top Button-->
  <a class="scroll-to-top rounded" href="#page-top">
    <i class="fas fa-angle-up"></i>
  </a>

  <!-- Logout Modal-->
  <div class="modal fade" id="logoutModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title" id="exampleModalLabel">Salida</h5>
          <button class="close" type="button" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">×</span>
          </button>
        </div>
        <div class="modal-body">
          Seleccione "Cerrar sesión" a continuación si está listo para
          finalizar su sesión actual.
        </div>
        <div class="modal-footer">
          <button class="btn btn-secondary" type="button" data-dismiss="modal">
            Cancelar
          </button>
          <a class="btn btn-primary" href="/coordinacion/logout">Cerrar Sesión</a>
        </div>
      </div>
    </div>
  </div>

  <!-- Delete Modal-->
  <div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title" id="exampleModalLabel">¡ATENCIÓN!</h5>
          <button class="close" type="button" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">×</span>
          </button>
        </div>
        <form id="formDeleteUser" method="POST" autocomplete="off">
          <!-- <input type="hidden" name="_method" value="DELETE"> -->
          <div class="modal-body">
            Oprima el botón "Eliminar" a continuación si está listo para
            eliminar al usuario:
            <div class="mb-3">
              <label for="userModal" class="form-label">Identificación</label>
              <input type="number" class="form-control" name="userDel" id="userModal" readonly>
            </div>
            <div class="mb-3">
              <label for="userModal" class="form-label">Nombre</label>
              <input type="text" class="form-control" id="nombreModal" disabled>
            </div>
            <b>*RECUERDE QUE ESTA ACCIÓN ES IRREVERSIBLE*</b>
          </div>
          <div class="modal-footer">
            <button class="btn btn-secondary" type="button" data-dismiss="modal">
              Cancelar
            </button>
            <button class="btn btn-primary " type="submit">
              Eliminar
            </button>
          </div>
        </form>
      </div>
    </div>
  </div>

  <!-- Change Password Modal-->
  <div class="modal fade" id="changePassModal" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title">¡ATENCIÓN!</h5>
          <button class="close" type="button" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">×</span>
          </button>
        </div>
        <form onsubmit="formChangePass(event)" id="formChangePass" method="POST" autocomplete="off">
          <div class="modal-body">
            Oprima el botón "Actualizar" a continuación si está listo para
            cambiar la contraseña a el usuario:
            <div class="mb-3">
              <label class="mr-sm-2" for="userPass">Identificación</label>
              <input type="number" class="form-control form-control-user" id="userPass" name="userPass" placeholder="Identificación" readonly />
            </div>
            <div class="mb-3">
              <label class="mr-sm-2" for="pass">Contraseña
                Nueva</label>
              <input type="password" class="form-control form-control-user" id="pass" name="pass" placeholder="Contraseña" minlength="4" maxlength="12" required />
            </div>
            <div class="mb-3">
              <label class="mr-sm-2" for="repass">Repite la
                Contraseña</label>
              <input type="password" class="form-control form-control-user" id="repass" name="repass" placeholder="Repite la Contraseña" minlength="4" maxlength="12" required />
            </div>
          </div>
          <div class="modal-footer">
            <button class="btn btn-secondary" type="button" data-dismiss="modal">
              Cancelar
            </button>
            <button class="btn btn-primary " type="submit">
              Actualizar
            </button>
          </div>
        </form>
      </div>
    </div>
  </div>

  <!-- Bootstrap core JavaScript-->
  <script src="/vendor/jquery/jquery.min.js"></script>
  <script src="/vendor/bootstrap/js/bootstrap.bundle.min.js"></script>

  <!-- Core plugin JavaScript-->
  <script src="/vendor/jquery-easing/jquery.easing.min.js"></script>

  <!-- Custom scripts for all pages-->
  <script src="/js/sb-admin-2.min.js"></script>

  <!-- Page level plugins -->
  <script src="/vendor/datatables/jquery.dataTables.min.js"></script>
  <script src="/vendor/datatables/dataTables.bootstrap4.min.js"></script>

  <!-- Page level custom scripts -->
  <script src="/js/demo/datatables-demo.js"></script>

  <!-- Validations -->
  <script type="text/javascript" src="/js/Coordinador/validaciones.js"></script>
  <script type="text/javascript" src="/js/Coordinador/formularios.js"></script>

  <!-- Alert -->
  <script src="//cdn.jsdelivr.net/npm/sweetalert2@11"></script>

  <!-- Excel -->
  <script src="https://cdn.datatables.net/buttons/2.2.3/js/dataTables.buttons.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script>
  <script src="https://cdn.datatables.net/buttons/2.2.3/js/buttons.html5.min.js"></script>

  <!-- Search Advanced -->
  <script src="https://cdn.datatables.net/fixedcolumns/4.1.0/js/dataTables.fixedColumns.min.js"></script>

  <script type="text/JavaScript">
    $(window).bind("load", function () {
      var nav = document.getElementById("nav4");
      $("#navlink3").removeClass("collapsed");
      var nav3 = document.getElementById("collapsePages");
      var nav4 = document.getElementById("show5");
      nav.className += "  active";
      nav3.className += "  show";
      nav4.className += "  active";
    });

    var table = $("#dataTable").DataTable({
      order: [
        [1, 'desc']
      ],
      scrollY: true,
      scrollX: true,
      scrollCollapse: true,
      language: {
        search: "Buscar:",
        info: "Mostrando _START_ elementos a _END_ de _TOTAL_ entradas",
        zeroRecords: "Sin Datos en esta Tabla",
        infoFiltered: " - filtrado desde _MAX_ entradas",
        lengthMenu: "Mostrando _MENU_ entradas",
        paginate: {
          previous: "Anterior",
          next: "Siguiente",
        },
      },
      dom: 'Bfrtlip',
      buttons: [{
        extend: 'excel',
        filename: 'USERS_' + new Date().toJSON().substring(0, 10),
        autoFilter: true,
        sheetName: "USUARIOS",
      }],
    });

    table.columns().every(function() {
      var that = this;
      $('input', this.footer()).on('keyup change', function() {
        that
          .search(this.value)
          .draw();
      });
    });
    <% if(fields){ %>
      const array = []
      <% fields.forEach((fields)=> { %>
        array.push(["<%= fields.provincia %>"], ["<%= fields.municipio %>"])
      <% }) %>
    <% } %>
    </script>

  <% if(typeof alert !="undefined" ) { %>
  <script type="text/JavaScript">
    Swal.fire({
              title: '<%= alert[0].alertTitle %>',
              text:'<%= alert[0].alertMessage %>',
              icon:'<%= alert[0].alertIcon %>',
              showConfirmButton: <%= alert[0].showConfirmButton %>,
              timer: <%= alert[0].timer %>
            }).then(()=>{
              window.location = '/<%= alert[0].ruta %>'
            })
          </script>
  <% } %>

</body>

</html>