<?php

if($_SESSION["nombre"] == "Inventario" || $_SESSION["nombre"] == "Vendedor"){

  echo '<script>

    window.location = "inicio";

  </script>';

  return;

}

?>

<div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <div class="container-fluid">
        <div class="row mb-2">
          <div class="col-sm-6">
            <h1>Administrar Usuarios</h1>
          </div>
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
              <li class="breadcrumb-item"><a href="inicio">Inicio</a></li>
              <li class="breadcrumb-item active">Administrar usuarios</li>
            </ol>
          </div>
        </div>
      </div><!-- /.container-fluid -->
    </section>

    
    <!-- Main content -->
    <section class="content">

      <!-- Default box -->
      <div class="card">
        <div class="card-header">
          <button class="btn btn-outline-warning" data-toggle="modal" data-target="#modalAgregarUsuario">
            Agregar Usuario
          </button>

          
        </div>
        <div class="card-body">
          <table class="table table-bordered table-hover dt-responsive tablas" >
          <thead>
                <tr>
                  <th style="width: 10px">#</th>
                  <th>Nombre</th>
                  <th>Usuario</th>
                  <th>foto</th>
                  <th>Cargo</th>
                  <th>Estado</th>
                  <th>Último login</th>
                  <th>Acciones</th>
                </tr>
                </thead>
                <tbody>

                <?php 

                $item = null;
                $valor = null;
                
                $usuarios = ControladorUsuarios:: ctrMostrarUsuarios($item, $valor);


                foreach($usuarios as $key => $value){

                  $item = "id";
                  $valor = $value["id_empleado"];

                  $empleado = ControladorEmpleado:: ctrMostrarEmpleado($item, $valor);



                  echo '<tr>
                          <td>'.($key+1).'</td>
                          <td>'.$empleado["nombre"].'</td>
                          <td>'.$value["usuario"].'</td>';
                          
                          if($value["foto"] != ""){
                            echo '<td><img src="'.$value["foto"].'" class="img-thumbnail" width="40px"></td>';
                          }else{

                            echo '<td><img src="vistas/dist/img/usuarios/anonymous.png" class="img-thumbnail" width="40px"></td>';
                          }


                          $item = 'id';
                          $valor = $value["id_area"];
          
                          $area = ControladorArea:: ctrMostrarArea($item, $valor);
                          
                          echo '<td>'.$area["nombre"].'</td>';

                          if($value["estado"] != 0){
                            echo '<td><button class="btn btn-success btn-xs btnActivar" idUsuario="'.$value["id"].'" estadoUsuario="0">Activado</button></td>';
                          }else {
                            echo '<td><button class="btn btn-danger btn-xs btnActivar" idUsuario="'.$value["id"].'" estadoUsuario="1">Desactivado</button></td>';
                          }

                          echo '<td>'.$value["ultimo_login"].'</td>
                          <td>
                            <div class="btn-group">
                              <button class=" btn btn-warning btnEditarUsuario" idUsuario="'.$value["id"].'" data-toggle="modal" data-target="#modalEditarUsuario"><i class="fas fa-pencil-alt"></i></button>
                              <button class=" btn btn-danger btnEliminarUsuario" idUsuario="'.$value["id"].'" fotoUsuario="'.$value["foto"].'" usuario="'.$value["usuario"].'"><i class="fa fa-times"></i></button>
                            </div>


                          </td>

                        </tr>';
                }
                ?>
                </tbody>


          </table>
        </div>

      </div>
      <!-- /.card -->

    </section>
    <!-- /.content -->
  </div>

   <!-- MODAL AGREGAR USUARIO -->

   <div class="modal fade" id="modalAgregarUsuario">
        <div class="modal-dialog">
          <div class="modal-content">
            <form role="form" method="post" enctype="multipart/form-data">

                <div class="modal-header" style="background: #EAA354; color: white">
                  <h4 class="modal-title">Agregar Usuario</h4>
                  <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                  </button>
                </div>

                <div class="modal-body">
                  <div class="box-body">

                  <div class="form-group">
                      <div class="input-group mb-3">
                        <div class="input-group-append">
                          <div class="input-group-text">
                            <span class="fas fa-users"></span>
                          </div>
                        </div>
                        <select class="form-control input-lg" name="nuevoEmpleado" required>
                          <option value="">Seleccionar un Trabajador</option>
                          <?php

                           $item = null;
                           $valor = null;

                           $empleado = ControladorEmpleado::ctrMostrarEmpleadoU($item, $valor);

                           foreach($empleado as $key => $value){

                            if ($value["flag"] == 0) {
                              echo'<option value="'.$value["id"].'">'.$value["nombre"].'</option>';
                            } 
                            
                           }
                          

                          ?>
                        </select>
                      </div>
                    </div >

                    <div class="form-group">
                      <div class="input-group mb-3">
                        <div class="input-group-append">
                          <div class="input-group-text">
                            <span class="fas fa-key"></span>
                          </div>
                        </div>
                        <input type="text" class="form-control input-lg" name="nuevoUsuario" id="nuevoUsuario" placeholder="Ingresar usuario" required>
                      </div>
                    </div>

                    <div class="form-group">
                      <div class="input-group mb-3">
                        <div class="input-group-append">
                          <div class="input-group-text">
                          <span class="fas fa-lock"></span>
                          </div>
                        </div>
                        <input type="password" class="form-control input-lg" name="nuevoPassword" placeholder="Ingresar contraseña" required>
                      </div>
                    </div>

                    <div class="form-group">
                      <div class="input-group mb-3">
                        <div class="input-group-append">
                          <div class="input-group-text">
                            <span class="fas fa-users"></span>
                          </div>
                        </div>
                        <select class="form-control input-lg " name="nuevoPerfil" required>
                        <option value="">Seleccionar Area</option>
                        <?php 

                            $item = null;
                            $valor = null;

                            $area = ControladorArea:: ctrMostrarArea($item, $valor);
                            foreach($area as $key => $value){
                              echo '<option value="'.$value["id"].'">'.$value["nombre"].'</option>';
                              
                            }
                            ?>
                        
                      </select>
                      </div>
                    </div >

                    <div class="form-group">
                      <div class="panel">Subir Foto</div>
                      <input type="file" class="nuevaFoto" name="nuevaFoto">
                      <p class="help-block">Peso maximo de la foto 2 MB</p>
                      <img src="vistas/dist/img/usuarios/anonymous.png" class="img-thumbnail previsualizar" width="100px">
                    </div>


                  </div>
                </div>


                  <div class="modal-footer justify-content-between">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Salir</button>
                    <button type="submit" class="btn btn-warning">Guardar</button>
                  </div>
                      <?php 
                      
                      $crearUsuario = new ControladorUsuarios();
                      $crearUsuario -> ctrCrearUsuario();
                      
                      ?>
              </form>
          </div>
          <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
      </div>
      <!-- /.modal -->

       <!-- MODAL EDITAR USUARIO -->

   <div class="modal fade" id="modalEditarUsuario">
        <div class="modal-dialog">
          <div class="modal-content">
    <form role="form" method="post" enctype="multipart/form-data">

            <div class="modal-header" style="background: #EAA354; color: white">
              <h4 class="modal-title">Modificar Usuario</h4>
              <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span>
              </button>
            </div>

            <div class="modal-body">
              <div class="box-body">

              <div class="form-group">
                <div class="input-group mb-3">
                <div class="input-group-append">
                  <div class="input-group-text">
                    <span class="fas fa-user"></span>
                  </div>
                  </div>
                  <select class="form-control input-lg" name="editarNombre">
                      <option value="" id="editarNombre"></option>
                    </select>
                  </div>
                </div>

                <div class="form-group">
                <div class="input-group mb-3">
                <div class="input-group-append">
                  <div class="input-group-text">
                    <span class="fas fa-key"></span>
                  </div>
                  </div>
                    <input type="text" class="form-control input-lg" id="editarUsuario" name="editarUsuario" values="" readonly required>
                    
                  </div>
                </div>

                <div class="form-group">
                <div class="input-group mb-3">
                <div class="input-group-append">
                  <div class="input-group-text">
                    <span class="fas fa-lock"></span>
                  </div>
                  </div>
                    <input type="password" class="form-control input-lg" id="editarPassword" name="editarPassword" placeholder="Escriba la nueva contraseña">
                    <input type="hidden" id="passwordActual" name="passwordActual">
                  </div>
                </div>

                <div class="form-group">
                <div class="input-group mb-3">
                <div class="input-group-append">
                  <div class="input-group-text">
                    <span class="fas fa-users"></span>
                  </div>
                  </div>
                    <select class="form-control input-lg" name="editarPerfil">
                      <option value="" id="editarPerfil"></option>
                      <?php 

                            $item = null;
                            $valor = null;

                            $area = ControladorArea:: ctrMostrarArea($item, $valor);
                            foreach($area as $key => $value){
                              echo '<option value="'.$value["id"].'">'.$value["nombre"].'</option>';
                              
                            }
                            ?>
                    </select>
                  </div>
                </div>

                <div class="form-group">
                  <div class="panel">Subir Foto</div>
                  <input type="file" class="nuevoFoto" name="editarFoto">
                  <p class="help-block">Peso maximo de la foto 2 MB</p>
                  <img src="vistas/dist/img/usuarios/anonymous.png" class="img-thumbnail previsualizar" width="100px">
                  <input type="hidden" name="fotoActual" id="fotoActual">
                </div>


              </div>
            </div>
            <div class="modal-footer justify-content-between">
              <button type="button" class="btn btn-default" data-dismiss="modal">Salir</button>
              <button type="submit" class="btn btn-warning">Modificar</button>
            </div>
            <?php 
            
            $editarUsuario = new ControladorUsuarios();
            $editarUsuario -> ctrEditarUsuario();
            
            ?>
        </form>
          </div>
          <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
      </div>
      <!-- /.modal -->
<?php 

$borrarUsuario = new ControladorUsuarios();
$borrarUsuario -> ctrBorrarUsuario();

?>
<script>
        $(function () {
          $("#example1").DataTable();
          $('#example2').DataTable({
            "paging": true,
            "lengthChange": false,
            "searching": false,
            "ordering": true,
            "info": true,
            "autoWidth": false,
          });
        });
</script>
<script src="./vistas/js/plantilla.js"></script>
<script>
      
      var elemento = document.getElementById("mantenimiento");
      elemento.className += " active";

      var elemento = document.getElementById("mante");
      elemento.className += " menu-open";

      var elemento = document.getElementById("usuario");
      elemento.className += " active";
</script>

