<?php
  $seccion      = 'Clientes';
  $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
      cli.id != 0
  ";

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

    $Bus = "&buscar=Ok";

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

  }

  //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(cli.Id) AS Total
    FROM clientes cli
    $Where
  ";
  $Result = mysqli_query($mysqli, $Query);
  $UsrTotal = mysqli_fetch_assoc($Result);
  $total_records = $UsrTotal[Total];
  $pages = intval($total_records / $cantidad);

  //Clientes
  $Query = "
    SELECT
      cli.id, cli.nombre, cli.activo
    FROM clientes cli
    $Where
    ORDER BY cli.nombre ASC
    LIMIT $inicial, $cantidad
  ";
  $Clientes = mysqli_query($mysqli, $Query);

?>
<!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">

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

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

                        <?php if( $ShowBuscador == '1' ){ ?>
                          <!-- 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 -->
                        <?php } ?>

                        <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 clientes de Switch Point app.
                              </p>

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

                                      <!-- Usuario -->
                                      <td>
                                        <div>
                                          <i class="fa fa-user m-r-5"></i>
                                          <?php echo $Cli['nombre']; ?>
                                        </div>
                                      </td>
                                      <!-- End Usuario -->

                                      <!-- Estatus -->
                                      <td>
            														<?php if ($Cli['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="cliente?id=<?php echo $Cli['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>
