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

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

    $Bus = "&buscar=Ok";

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

    //Codigo
    if( $_GET['cod'] ){
      $fil_cod = $_GET['cod'];
      $Where .= "
        AND art.codigo LIKE '$fil_cod%'
      ";
      $Bus .= "&cod=".$fil_cod;
    }
    //End Codigo

    //Proveedor
    if( $_GET['pro'] ){
      $fil_pro = $_GET['pro'];
      $Where .= "
        AND art.fk_proveedor = '$fil_pro'
      ";
      $Bus .= "&pro=".$fil_pro;
    }
    //End Proveedor

    //Marca
    if( $_GET['mar'] ){
      $fil_mar = $_GET['mar'];
      $Where .= "
        AND art.fk_marca = '$fil_mar'
      ";
      $Bus .= "&mar=".$fil_mar;
    }
    //End Marca

    //Categoria
    if( $_GET['cat'] ){
      $fil_cat = $_GET['cat'];
      $Where .= "
        AND art.fk_articulo_categoria = '$fil_cat'
      ";
      $Bus .= "&cat=".$fil_cat;
    }
    //End Categoria

    //Estatus
    if( $_GET['est'] ){
      $fil_est = $_GET['est'];
      if( $_GET['est'] == 'si' ){ $fil_esta = '1'; }
      if( $_GET['est'] == 'no' ){ $fil_esta = '0'; }
      $Where .= "
        AND art.activo = '$fil_esta'
      ";
      $Bus .= "&est=".$fil_est;
    }
    //End Estatus

  }

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

  //Total Articulos
  $Query = "
    SELECT
      COUNT(art.Id) AS Total
    FROM articulos art
    LEFT JOIN proveedores pro ON pro.id = art.fk_proveedor
    LEFT JOIN marcas mar ON mar.id = art.fk_marca
    $Where
      ORDER BY pro.nombre ASC
  ";
  $Result = mysqli_query($mysqli, $Query);
  $ArtTotal = mysqli_fetch_assoc($Result);
  $total_records = $ArtTotal[Total];
  $pages = intval($total_records / $cantidad);

  //Articulos
  $Query = "
    SELECT
      art.id,
      art.codigo, art.sku,
      art.nombre, art.descripcion,
      art.principal, art.activo,
      pro.nombre AS proveedor,
      mar.nombre AS marca,
      cat.categoria
    FROM articulos art
    LEFT JOIN proveedores pro ON pro.id = art.fk_proveedor
    LEFT JOIN marcas mar ON mar.id = art.fk_marca
    LEFT JOIN cat_articulos_categorias cat ON cat.id = art.fk_articulo_categoria
    $Where
      ORDER BY art.nombre ASC
    LIMIT $inicial, $cantidad
  ";
  $Arts = 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 -->

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

            						<!-- Articulos -->
                        <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 -->

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

                                    <!-- Categoria -->
                                    <div class="col-sm-4">
                                      <div class="form-group">
                                        <label class="control-label">Categoría</label>
                                        <div class="controls">
                                          <select name="cat" class="SelCat form-control form-control-sm chosen-select">
                                            <option value="">Todos</option>
                                            <?php
                                              //Categorias
                                              $Query = "
                                                SELECT
                                                  cat.id, cat.categoria
                                                FROM cat_articulos_categorias cat
                                                ORDER BY cat.categoria ASC
                                              ";
                                              $Cats = $mysqli->query($Query);
                                              mysqli_data_seek($Cats, 0);
                                              while($Cat = mysqli_fetch_array($Cats)) {
                                            ?>
                                              <option
                                                <?php if( $Cat['id'] == $fil_cat ){ echo 'selected="selected"'; } ?>
                                                value="<?php echo $Cat['id']; ?>"
                                              >
                                                <?php echo $Cat['categoria']; ?>
                                              </option>
                                            <?php } ?>
                                          </select>
                                        </div>
                                      </div>
                                    </div>
                                    <!-- End Categoria -->

                                    <!-- Proveedor -->
                                    <div class="col-sm-3">
                                      <div class="form-group">
                                        <label class="control-label">Proveedor</label>
                                        <div class="controls">
                                          <select name="pro" class="SelCat form-control form-control-sm chosen-select">
                                            <option value="">Todos</option>
                                            <?php
                                              //Proveedores
                                              $Query = "
                                                SELECT
                                                  pro.id, pro.nombre
                                                FROM proveedores pro
                                                ORDER BY pro.nombre ASC
                                              ";
                                              $Pros = $mysqli->query($Query);
                                              mysqli_data_seek($Pros, 0);
                                              while($Pro = mysqli_fetch_array($Pros)) {
                                            ?>
                                              <option
                                                <?php if( $Pro['id'] == $fil_pro ){ echo 'selected="selected"'; } ?>
                                                value="<?php echo $Pro['id']; ?>"
                                              >
                                                <?php echo $Pro['nombre']; ?>
                                              </option>
                                            <?php } ?>
                                          </select>
                                        </div>
                                      </div>
                                    </div>
                                    <!-- End Proveedor -->

                                    <!-- Marcas -->
                                    <div class="col-sm-3">
                                      <div class="form-group">
                                        <label class="control-label">Marca</label>
                                        <div class="controls">
                                          <select name="mar" class="form-control form-control-sm chosen-select">
                                            <option value="">Todos</option>
                                            <?php
                                              //Proveedores
                                              $Query = "
                                                SELECT
                                                  mar.id, mar.nombre
                                                FROM marcas mar
                                                ORDER BY mar.nombre ASC
                                              ";
                                              $Mars = $mysqli->query($Query);
                                              mysqli_data_seek($Mars, 0);
                                              while($Mar = mysqli_fetch_array($Mars)) {
                                            ?>
                                              <option
                                                <?php if( $Mar['id'] == $fil_mar ){ echo 'selected="selected"'; } ?>
                                                value="<?php echo $Mar['id']; ?>"
                                              >
                                                <?php echo $Mar['nombre']; ?>
                                              </option>
                                            <?php } ?>
                                          </select>
                                        </div>
                                      </div>
                                    </div>
                                    <!-- End Marcas -->

                                    <!-- Estatus -->
                                    <div class="col-sm-3">
                                      <div class="form-group">
                                        <label class="control-label">Estatus</label>
                                        <div class="controls">
                                          <select name="est" class="form-control form-control-sm chosen-select">
                                            <option value="">Todos</option>
                                            <option
                                              <?php if( $fil_est == 'si' ){ echo 'selected="selected"'; } ?>
                                              value="si"
                                            >
                                              Activo
                                            </option>
                                            <option
                                              <?php if( $fil_est == 'no' ){ echo 'selected="selected"'; } ?>
                                              value="no"
                                            >
                                              Inactivo
                                            </option>
                                          </select>
                                        </div>
                                      </div>
                                    </div>
                                    <!-- End Estatus -->

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

                                    <!-- Btn Descargar Excel -->
                                    <div class="col-sm-3">
                                      <div class="form-group">
                                        <label class="control-label">.</label>
                                        <a href="excel.php?a=1<?php echo $Bus; ?>" target="_blank">
                                          <button class="btn btn-secondary btn-sm btn-block" type="button">
                                            Descargar Excel
                                            <i class="fa fa-file mar-l-5"></i>
                                          </button>
                                        </a>
                                      </div>
                                    </div>
                                    <!-- End Btn Descargar Excel -->

                                  </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">
                                    Articulos
                                  </p>

                                  <!-- Tabla -->
                                  <table id="" class="table dt-responsive nowrap w-100">
                                    <thead>
                                      <tr>
                                        <th></th>
                                        <th>Articulo</th>
                                        <th>Precios</th>
                                        <th>Proveedor</th>
                                        <th>Marca</th>
                                        <th>Principal</th>
                                        <th>Estatus</th>
              													<th></th>
                                        <th></th>
                                      </tr>
                                    </thead>
                                    <tbody>
											              <?php
                                      mysqli_data_seek($Arts, 0);
                                      while($Art = mysqli_fetch_array($Arts)) {
                                    ?>
                                      <tr>

                                        <!-- Imagen -->
                                        <td>
                                          <img
                                            style="width:120px;"
                                            src="<?php
                                              echo $position; ?>../articulos/<?php echo $Art[id];
                                            ?>.jpg"
                                          />
                                          <img
          																	class="ImgLogo img-responsive"
          																	style="max-width: 60px;"
          																	src="<?php echo $position; ?>../articulos/<?php echo $Art[id]; ?>-m.jpg"
          																/>
                                        </td>
                                        <!-- End Imagen -->

                                        <!-- Articulo -->
                                        <td>
                                          <div>
                                            <?php echo $Art['nombre']; ?>
                                          </div>
                                          <div>
                                            Codigo:
                                            <?php echo $Art['codigo']; ?>
                                          </div>
                                          <div>
                                            Sku:
                                            <?php echo $Art['sku']; ?>
                                          </div>
                                          <div>
                                            <?php echo $Art['categoria']; ?>
                                          </div>
                                        </td>
                                        <!-- End Articulo -->

                                        <!-- Precios -->
                                        <td>
                                          <?php
                                            //Precios
                                            $Query = "
                                              SELECT
                                                pre.id, pre.precio,
                                                pre.rango_a, pre.rango_b
                                              FROM articulos_precios pre
                                              WHERE pre.fk_articulo = '$Art[id]'
                                              ORDER BY pre.rango_a ASC
                                            ";
                                            $Precios = $mysqli->query($Query);
                                            mysqli_data_seek($Precios, 0);
                                            while($Precio = mysqli_fetch_array($Precios)) {
                                          ?>
                                            <div>
                                              $ <?php echo $Precio[precio]; ?>
                                               [ <?php echo $Precio[rango_a]; ?> - <?php echo $Precio[rango_b]; ?> ]
                                            </div>
                                          <?php } ?>
                                        </td>
                                        <!-- End Precios -->

                                        <!-- Proveedor -->
                                        <td>
                                          <?php echo $Art['proveedor']; ?>
                                        </td>
                                        <!-- End Proveedor -->

                                        <!-- Marca -->
                                        <td>
                                          <?php echo $Art['marca']; ?>
                                        </td>
                                        <!-- End Marca -->

                                        <!-- Btn Principal-->
                                        <td>
              														<?php
                                            if ($Art['principal'] == 1){
                                              $btnClass = "btn-success";
                                              $btnStatus = "Principal";
                                          ?>
              														  <button
                                              class="btn <?php echo $btnClass ?> btn-xs"
                                            >
                                              <?php echo $btnStatus ?>
                                            </button>
                                          <?php } ?>
                                        </td>
                                        <!-- End Btn Principal-->

                                        <!-- Btn Activo-->
                                        <td>
              														<?php if ($Art['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 Btn Activo-->

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

                                        <!-- Btn Borrar -->
                                        <td>
                                          <a
                                            class="btn btn-danger waves-effect waves-light btn-xs"
                                            href="#?id=<?php echo $Art['id'];?>">
                                            <i class="fa fa-times"></i>
                                          </a>
                                        </td>
                                        <!-- End Btn Borrar -->

                                      </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>
                        <!-- /Articulos -->

                    </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>
