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

  }

  //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
  ";
  $Arts = mysqli_query($mysqli, $Query);

  header('Content-Type: application/vnd.ms-Excel');
  header('Content-Disposition: attachment;filename="productos.xls"');
  header('Cache-Control: max-age=0');

?>
<table>
  <thead>
    <tr>
      <th>Nombre</th>
      <th>Codigo</th>
      <th>Sku</th>
      <th>Proveedor</th>
      <th>Marca</th>
      <th>Categoría</th>
      <th>Precio</th>
    </tr>
  </thead>
  <tbody>
    <?php
      mysqli_data_seek($Arts, 0);
      while($Art = mysqli_fetch_array($Arts)) {
    ?>
      <tr>
        <td>
          <?php
            echo $Art[nombre];
          ?>
        </td>
        <td>
          <?php echo $Art[codigo]; ?>
        </td>
        <td>
          <?php
            echo $Concepto[sku];
          ?>
        </td>
        <td>
          <?php echo $Art[proveedor]; ?>
        </td>
        <td>
          <?php echo $Art[marca]; ?>
        </td>
        <td>
          <?php echo $Art[categoria]; ?>
        </td>
        <td class="text-right">
          <?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
              LIMIT 1
            ";
            $Precios = $mysqli->query($Query);
            mysqli_data_seek($Precios, 0);
            while($Precio = mysqli_fetch_array($Precios)) {
          ?>
            <div>
              <?php echo $Precio[precio]; ?>
            </div>
          <?php } ?>
        </td>
      </tr>
    <?php } ?>
  </tbody>
</table>
