<?php
  $debugin          = $decoded['debug'];
  $usr_id           = $decoded['usr_id'];
  $articulo_id      = $decoded['articulo_id'];
  $nombre_articulo  = $decoded['nombre_articulo'];
  $categoria_id     = $decoded['categoria_id'];
  $marca_id         = $decoded['marca_id'];
  $datetoday        = date('Y-m-d');

  if( $usr_id ){

    //User
    $Query = "
      SELECT
        usr.id
      FROM usuarios_app usr
      WHERE
        usr.id = '$usr_id'
        AND usr.activo = '1'
    ";
    $Result = mysqli_query($mysqli, $Query);
    $Usr = mysqli_fetch_assoc($Result);
    if( $Usr[id] ){

      //Valida Articulos
      $Where = "
        WHERE
          art.id != '0'
          AND art.activo = '1'
      ";

      if( $categoria_id || $marca_id || $articulo_id || $nombre_articulo ){

        //Articulo
        if( $articulo_id ){
          $Where .= "
            AND art.id = '$articulo_id'
          ";
        }
        //End Articulo

        //Nombre de Articulo
        if( $nombre_articulo ){
          $Where .= "
            AND art.nombre LIKE '$nombre_articulo%'
          ";
        }
        //End Nombre de Articulo

        //Categorias
        if( $categoria_id ){
          $Where .= "
            AND art.fk_articulo_categoria = '$categoria_id'
          ";
        }
        //End Categorias

        //Marca
        if( $marca_id ){
          $Where .= "
            AND art.fk_marca = '$marca_id'
          ";
        }
        //End Marca

      }else{

      }

      //Articulos
      $Query = "
        SELECT
          art.id,
          art.codigo,
          art.nombre, art.descripcion,
          art.principal, art.activo,
          mar.nombre AS marca,
          cat.categoria
        FROM articulos art
        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
      ";
      $Articulos = $mysqli->query($Query);
      mysqli_data_seek($Articulos, 0);
      $Articulos->num_rows;
      $CountArt = 0;
      if( $Articulos->num_rows >= 1 ){
        while($Art = mysqli_fetch_array($Articulos)) {

          //Valida si existe imagen
          $ImageArt = $RootBack.'/articulos/'.$Art[id].'-m.jpg';
          if (file_exists($ImageArt)) {
            $Url_Img = $UrlRoot."/articulos/".$Art[id]."-m.jpg";
            // $Url_Img = base64_encode(file_get_contents($ImageArt));
          }else{
            $Url_Img = $UrlRoot.'/articulos/0-m.jpg';
            // $ImageArt = base64_encode(file_get_contents($ImageArt));
          }

          $CountArt = $CountArt + 1;

          //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)) {
            if( $Precio[id] ){
              $Prices[$Art[id]][] = array(
                'rango_desde'  => $Precio[rango_a],
                'rango_hasta'  => $Precio[rango_b],
                'precio'       => $Precio[precio]
              );
              $TienePrecio = 'Si';
            }else{
              $TienePrecio = 'No';
            }

          }
          //End Precios

          if( $TienePrecio == 'Si' ){
            $Arts[] = array(
              'articulo_id'       => $Art[id],
              'nombre'            => $Art[nombre],
              'descripcion'       => $Art[marca],
              'marca'             => $Art[marca],
              'categoria'         => $Art[categoria],
              'img_url'           => $Url_Img,
              'precios'           => $Prices[$Art[id]]
            );
          }

        }

        if( $CountArt >= 1 ){

          $Data = array(
            'articulos'   => $Arts
          );
          echo json_encode($Data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);

        }else{

          //Error
          $Query = "
            SELECT
              err.Id, err.error, err.msn
            FROM api_errores err
            WHERE err.error = 'ar-1'
          ";
          $Result = mysqli_query($mysqli, $Query);
          $Error = mysqli_fetch_assoc($Result);

          $Data = array(
            'action'      => 'error',
            'error'       => $Error[error],
            'error_msn'   => $Error[msn]
          );
          echo json_encode($Data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);

        }

      }else{

        //Error
        $Query = "
          SELECT
            err.Id, err.error, err.msn
          FROM api_errores err
          WHERE err.error = 'ar-1'
        ";
        $Result = mysqli_query($mysqli, $Query);
        $Error = mysqli_fetch_assoc($Result);

        $Data = array(
          'action'      => 'error',
          'error'       => $Error[error],
          'error_msn'   => $Error[msn]
        );
        echo json_encode($Data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);

      }


    }else{

      //Error
      $Query = "
        SELECT
          err.Id, err.error, err.msn
        FROM api_errores err
        WHERE err.error = 'uv-1'
      ";
      $Result = mysqli_query($mysqli, $Query);
      $Error = mysqli_fetch_assoc($Result);

      $Data = array(
        'action'      => 'error',
        'error'       => $Error[error],
        'error_msn'   => $Error[msn]
      );
      echo json_encode($Data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);

    }

  }
?>
