<?php
  $seccion      = 'Tenderos';
  $position     = '../../';

  require_once($position.'../switch/access.php');
  require_once($position.'assets/global.php');
  require_once($position.'assets/session.php');
  require_once($position.'assets/funciones.php');
  require_once($position.'assets/paginador1.php');

  //Buscador
  $Where = "
    WHERE
      us.id != 0
      AND us.fk_usuario_tipo = 2
  ";

  if( $_GET['buscar'] == 'Ok' ){

    $Bus = "&buscar=Ok";

    //Nombre
    if( $_GET['nom'] ){
      $fil_nom = $_GET['nom'];
      $Where .= "
        AND us.nombre LIKE '$fil_nom%'
      ";
      $Bus .= "&nom=".$fil_nom;
    }
    //End Nombre

    //Apellido Paterno
    if( $_GET['ape'] ){
      $fil_ape = $_GET['ape'];
      $Where .= "
        AND us.apellido_paterno LIKE '$fil_ape%'
      ";
      $Bus .= "&ape=".$fil_ape;
    }
    //End Apellido Paterno

    //Apellido Materno
    if( $_GET['apm'] ){
      $fil_apm = $_GET['apm'];
      $Where .= "
        AND us.apellido_materno LIKE '$fil_apm%'
      ";
      $Bus .= "&apm=".$fil_apm;
    }
    //End Apellido Materno

    //Email
    if( $_GET['ema'] ){
      $fil_ema = $_GET['ema'];
      $Where .= "
        AND us.email LIKE '$fil_ema%'
      ";
      $Bus .= "&ema=".$fil_ema;
    }
    //End Email

    //Celular
    if( $_GET['cel'] ){
      $fil_cel = $_GET['cel'];
      $Where .= "
        AND us.celular LIKE '$fil_cel%'
      ";
      $Bus .= "&cel=".$fil_cel;
    }
    //End Celular

  }

  //Paginación
  if (isset($_GET['pgn']))
  {
    $pgn =  $_GET['pgn']; // $pg es la pagina actual
  }
  else{
    $pgn = 0;
  }
  $cantidad = 20; // cantidad de resultados por página
  $PagerLap = $cantidad / 10;
  $inicial = $pgn * $cantidad;

  //Total Usuarios
  $Query = "
    SELECT
      COUNT(us.Id) AS Total
    FROM usuarios_app us
    $Where
  ";
  $Result = mysqli_query($mysqli, $Query);
  $UsrTotal = mysqli_fetch_assoc($Result);
  $total_records = $UsrTotal[Total];
  $pages = intval($total_records / $cantidad);

  //Usuarios
  $Usuarios = "
    SELECT
      us.id,
      us.nombre, us.apellido_paterno, us.apellido_materno,
      us.celular, us.email,
      us.fecha_alta, us.activo,
      cli.nombre AS cliente
    FROM usuarios_app us
    LEFT JOIN clientes cli ON cli.id = us.fk_cliente
    $Where
    ORDER BY us.nombre ASC
    LIMIT $inicial, $cantidad
  ";
  $Usuarios = mysqli_query($mysqli, $Usuarios);

?>
<!DOCTYPE html>
<html lang="es_MX">
  <?php require_once($position.'head.php'); ?>

    <body class="loading" data-layout='{"mode": "light", "width": "fluid", "menuPosition": "fixed", "sidebar": { "color": "dark", "size": "default", "showuser": false}, "topbar": {"color": "light"}, "showRightSidebarOnPageLoad": false}'>

        <!-- Pagina de inicio -->
        <div id="wrapper">

          <!-- ========== Inicio nav-izq ========== -->
          <?php include_once($position.'assets/nav.php'); ?>
          <!-- Fin nav-izq -->

          <!-- ============================================================== -->
          <!-- Incio de Contenido -->
          <!-- ============================================================== -->

            <div class="content-page">
                <div class="content">

                    <!-- Topbar -->
                    <?php include_once($position.'assets/topbar.php'); ?>
                    <!-- /Topbar -->

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

                      <!-- page title -->
                      <div class="row">
                        <div class="col-12">
                          <div class="page-title-box">
                            <h4 class="page-title"><?php echo $seccion; ?></h4>
                            <div class="page-title-right">
                              <ol class="breadcrumb m-0">
                                <li class="breadcrumb-item"><a href="<?php echo $dir; ?>inicio/">Inicio</a></li>
                                <li class="breadcrumb-item active"><?php echo $seccion; ?></li>
                              </ol>
                            </div>
                          </div>
                        </div>
                      </div>
                      <!-- /page title -->

                      <!-- secciones  -->

                      <!-- Botones -->
                			<div class="row">
                				<div class="col-12 text-right">
                					<button type="button" class="btn btn-secondary btn-sm" onclick="window.location.href='usuario.php'">
                            <i class="fe-user-plus"></i>
                            &nbsp; Nuevo Usuario
                          </button>
                				</div>
                				<div class="col-12" style="height: 5px;">&nbsp;</div>
                			</div>
                      <!-- End Botones -->

						          <!-- Usuarios -->
                      <div class="row">

                        <!-- Buscador -->
                        <div class="col-12">

                          <div class="card">
                            <div class="card-body">
                              <form method="get">
                                <div class="row">

                                  <!-- Nombre -->
                                  <div class="col-sm-4">
                                    <div class="form-group">
                                      <label class="control-label">Nombre</label>
                                      <input name="nom" value="<?php echo $fil_nom; ?>" type="text"
                                        class="form-control form-control-sm"
                                      >
                                    </div>
                                  </div>
                                  <!-- End Nombre -->

                                  <!-- Apellido Paterno -->
                                  <div class="col-sm-4">
                                    <div class="form-group">
                                      <label class="control-label">Apellido Paterno</label>
                                      <input name="ape" value="<?php echo $fil_ape; ?>" type="text"
                                        class="form-control form-control-sm"
                                      >
                                    </div>
                                  </div>
                                  <!-- End Apellido Paterno -->

                                  <!-- Apellido Materno -->
                                  <div class="col-sm-4">
                                    <div class="form-group">
                                      <label class="control-label">Apellido Materno</label>
                                      <input name="apm" value="<?php echo $fil_apm; ?>" type="text"
                                        class="form-control form-control-sm"
                                      >
                                    </div>
                                  </div>
                                  <!-- End Apellido Materno -->

                                  <!-- Email -->
                                  <div class="col-sm-3">
                                    <div class="form-group">
                                      <label class="control-label">Email</label>
                                      <input name="ema" value="<?php echo $fil_ema; ?>" type="text"
                                        class="form-control form-control-sm"
                                      >
                                    </div>
                                  </div>
                                  <!-- End Email -->

                                  <!-- Celular -->
                                  <div class="col-sm-3">
                                    <div class="form-group">
                                      <label class="control-label">Celular</label>
                                      <input name="cel" value="<?php echo $fil_cel; ?>" type="text"
                                        class="form-control form-control-sm"
                                      >
                                    </div>
                                  </div>
                                  <!-- End Celular -->

                                  <!-- Filtrar -->
                                  <div class="col-sm-3">
                                    <div class="form-group">
                                      <label class="control-label">.</label>
                                      <button class="btn btn-success btn-sm btn-block" type="submit" name="button">Filtrar <i class="fa fa-search mar-l-5"></i></button>
                                      <input type="hidden" name="buscar" value="Ok" />
                                    </div>
                                  </div>
                                  <!-- End Filtrar -->

                                  <!-- Total -->
                                  <div class="col-sm-3">
                                    <div class="form-group">
                                      <label class="control-label">Total Articulos</label>
                                      <input
                                        readonly
                                        type="text"
                                        class="form-control form-control-sm"
                                        autocomplete="off"
                                        placeholder=""
                                        value="<?php echo $total_records; ?>"
                                      >
                                    </div>
                                  </div>
                                  <!-- End Total -->

                                </div>
                              </form>
                            </div>
                          </div>

                        </div>
                        <!-- End Buscador -->

                        <div class="col-12">
                          <div class="card">
                            <div class="card-body">
                              <h4 class="header-title">Lista de <?php echo $seccion; ?></h4>
                              <p class="text-muted font-13 mb-4">
                                  Información de tenderos de Switch Point app.
                              </p>

                              <!-- Tabla -->
                              <table class="table dt-responsive nowrap w-100">
                                <thead>
                                  <tr>
                                    <th>Nombre</th>
                                    <th>Fecha Alta</th>
                                    <th>Cliente</th>
                                    <th>Estatus</th>
          													<th style="width:100px;">&nbsp;</th>
          													<th style="width:20px;">&nbsp;</th>
                                  </tr>
                                </thead>
                                <tbody>
                                  <?php
                                    mysqli_data_seek($Usuarios, 0);
                                    while($urs = mysqli_fetch_array($Usuarios)) {
                                  ?>
                                    <tr>

                                      <!-- Usuario -->
                                      <td>
                                        <div>
                                          <i class="fa fa-user text-success m-r-5"></i>
                                          <?php echo $urs['nombre']." ".$urs['apellido_paterno']." ".$urs['apellido_materno']; ?>
                                        </div>
                                        <div>
                                          <i class="fa fa-envelope m-r-5"></i>
                                          <?php echo $urs['email'];?>
                                        </div>
                                        <div>
                                          <i class="fa fa-phone m-r-5"></i>
                                          <?php echo $urs['celular']; ?>
                                        </div>
                                      </td>
                                      <!-- End Usuario -->

                                      <!-- Fecha Alta -->
                                      <td>
                                        <div>
                                          <i class="fa fa-calendar text-success m-r-5"></i>
                                          <?php echo cambio_fecha_numerica_texto(date("Y-m-d", strtotime($urs['fecha_alta']))); ?>
                                        </div>
                                        <div>
                                          <i class="fa fa-clock m-r-5"></i>
                                          <?php echo date("H:i", strtotime($urs['fecha_alta'])); ?>
                                        </div>
                                      </td>
                                      <!-- End Fecha Alta -->

                                      <!-- Cliente -->
                                      <td>
                                        <?php echo $urs['cliente'];?>
                                      </td>
                                      <!-- End Cliente -->

                                      <!-- Estatus -->
                                      <td>
            														<?php if ($urs['activo'] == 1){$btnClass = "btn-success"; $btnStatus = "Activo";}else{$btnClass = "btn-primary"; $btnStatus = "Inactivo";}?>
            														<button class="btn <?php echo $btnClass ?> btn-xs"><?php echo $btnStatus ?></button>
            													</td>
                                      <!-- End Estatus -->

                                      <!-- Editar -->
            													<td>
            														<a
                                          class="btn  btn-outline-secondary waves-effect btn-xs"
                                          href="usuario.php?id=<?php echo $urs['id'];?>"
                                        >
                                          <i class="fe-edit-1"></i>
                                          Editar
                                        </a>
            													</td>
                                      <!-- End Editar -->

                                      <!-- Eliminar -->
                                      <td>

                                        <!-- Btn Eliminar -->
                                        <a
                                          class="btn btn-danger waves-effect waves-light btn-xs"
                                          href="#"
                                        >
                                          <i class="fa fa-times"></i>
                                        </a>
                                        <!-- End Btn Eliminar -->

                                      </td>
                                      <!-- End Eliminar -->

                                    </tr>
				                          <?php } ?>
                                </tbody>
                              </table>
                              <!-- End Tabla -->

                              <!-- Paginador -->
                              <div class="m-t-40 text-center">
                                <!-- <button class="btn btn-secondary">< Primera</button> -->

                                <?php
                                  $cantRegistros  = $total_records / $PagerLap;
                                  $pgnActual      = $_GET['pgn'];
                                  $pag            = new Paginador();
                                  $datos          = $pag->paginar($pgnActual, $cantRegistros);
                                  if ($datos) {
                                    'Pagina: ' . ($_GET['pgn'] + 1) . ' de ' . $pag->getCantidadPaginas() . '<br />';
                                ?>
                                  <?php
                                    foreach( $datos as $enlace ) {
                                  ?>
                                    <a
                                      href="?pgn=<?php
                                        echo $enlace['numero'];
                                        echo $Bus;
                                      ?>"
                                    >
                                      <button type="button" class="btn btn-secondary"><?php echo $enlace['vista']; ?></button>
                                    </a>
                                  <?php } ?>
                                <?php } ?>

                              </div>
                              <!-- End Paginador -->

                            </div> <!-- end card body-->
                          </div> <!-- end card -->
                        </div><!-- end col-->
                      </div>
                      <!-- End Usuarios -->

                    </div> <!-- /container -->

                </div> <!-- /content -->

                <!-- Footer -->
                <?php include_once($position.'assets/footer.php'); ?>
                <!-- /Footer -->

            </div>

            <!-- ============================================================== -->
            <!-- Fin de Contenido -->
            <!-- ============================================================== -->


        </div>
        <!--  /wrapper -->

        <?php require_once($position.'scripts.php') ?>

    </body>
</html>
