<?php 
	

    require_once('../Connections/pandora.php');
	
	//ESTO PARA OCULTAR LOS PARÁMETROS GET
	if (!$_GET){
		$_SESSION['got'] = $_GET;
		$uri_parts = explode('?', $_SERVER['REQUEST_URI'], 2);
		$actual_link = "http://$_SERVER[HTTP_HOST]" . $uri_parts[0];
		echo $actual_link;
        header('Location: ' . $actual_link);
        die;
    }
    else{
		if (!empty($_SESSION['got'])){
			$_GET = $_SESSION['got'];
			unset($_SESSION['got']);
		}
		
		$currentPage = $_SERVER["PHP_SELF"];
		$maxRows_Recordsetcomun = 25;
		$pageNum_Recordsetcomun = 0;
		if (isset($_GET['pageNum_Recordsetcomun'])) {
		  $pageNum_Recordsetcomun = $_GET['pageNum_Recordsetcomun'];
		}
		$startRow_Recordsetcomun = $pageNum_Recordsetcomun * $maxRows_Recordsetcomun;
		$idMarca = $idSegmento = $idModelo = "";
		
		//Controlamos que no haya pasado con parametros vacíos
		if ((!isset($_GET['modelo']) || $_GET['modelo'] == "" || 
				$_GET['modelo'] == '0')
			&& (!isset($_GET['codigo_pieza']) || $_GET['codigo_pieza'] == "") 
			&& (!isset($_GET['diametro']) || $_GET['diametro'] == "") 
			&& (!isset($_GET['longitud']) || $_GET['longitud'] == "")
			&& (!isset($_GET['tipo_pieza']) || 
					(isset($_GET['tipo_pieza']) && $_GET['tipo_pieza'] != "Movimiento"))
		   ){
			//Y que las variables de sesión estén vacías (sino es que volvió para atrás del detalle)
			if ($_SESSION['idMarca'] == "" && $_SESSION['segmento'] == "" 
			&& $_SESSION['modelo'] == "" && $_SESSION['codigo_pieza'] == ""
			&& $_SESSION['diametro'] == "" && $_SESSION['longitud'] == ""
			&& $_SESSION['tipo_pieza'] == ""){	
				header("Location: http://www.etma.com.ar/catalogo");
				exit;	
			}
		}
		
		if (isset($_GET['idMarca'])) {
			$_SESSION['idMarca'] = $_GET['idMarca'];
			if (isset($_GET['segmento'])) {
				$_SESSION['segmento'] = $_GET['segmento'];
				if (isset($_GET['modelo'])) {
					$_SESSION['modelo'] = $_GET['modelo'];
				}
			}
			$_SESSION['codigo_pieza'] = "";
			$_SESSION['diametro'] = "";
			$_SESSION['longitud'] = "";
			$_SESSION['tipo_pieza'] = "";
		}
		if (isset($_GET['codigo_pieza'])) {
			$_SESSION['idMarca'] = "";
			$_SESSION['segmento'] = "";
			$_SESSION['modelo'] = "";
			$_SESSION['codigo_pieza'] = $_GET['codigo_pieza'];
			$_SESSION['diametro'] = "";
			$_SESSION['longitud'] = "";
			$_SESSION['tipo_pieza'] = "";
		}
		if (isset($_GET['tipo_pieza'])) {
			$_SESSION['idMarca'] = "";
			$_SESSION['segmento'] = "";
			$_SESSION['modelo'] = "";
			$_SESSION['codigo_pieza'] = "";
			$_SESSION['diametro'] = $_GET['diametro'];
			$_SESSION['longitud'] = $_GET['longitud'];
			$_SESSION['tipo_pieza'] = $_GET['tipo_pieza'];
		}
		
		//PARA TEST
		//echo "TipoPieza: " . $_GET['tipo_pieza'] . " - Diametro: " . $_GET['diametro'] . " - SesionDiametro: " . $_SESSION['diametro'] . " - Longitud: " . $_GET['longitud'] . " SesiónLongitud: " . $_SESSION['longitud'];
		
		$query_Recordsetcomun = "SELECT pieza_standar.* FROM pieza_standar";
		//$query_Recordsetcomun = "select pieza.*, piezacodigo.id_marca from pieza left join piezacodigo on pieza.id = piezacodigo.id_pieza where id_marca = 'c8730441-b37d-4824-8a40-a00c22ba8979' or id_marca = '2be8c832-3d18-4721-b64f-9dfc16d02c8e' or id_marca = '1417ea7e-7f6c-4854-9e23-becf7bb4582a'";
		//BÚSQUEDA POR MARCA/SEGMENTO/MODELO
		if (isset($_SESSION['idMarca']) && $_SESSION['idMarca'] != "") { 
			$query_Recordsetcomun = sprintf("%s INNER JOIN vehiculo ON pieza_standar.id = vehiculo.id_pieza LEFT JOIN segmento ON vehiculo.id_segmento = segmento.id  WHERE id_marcavehiculo = '%s'", $query_Recordsetcomun, $_SESSION['idMarca']);
			if (isset($_SESSION['segmento']) && $_SESSION['segmento'] != "" && $_SESSION['segmento'] != '0'){
				$query_Recordsetcomun = sprintf("%s AND segmento = '%s'", $query_Recordsetcomun, $_SESSION['segmento']);
				if (isset($_SESSION['modelo']) && $_SESSION['modelo'] != "" && $_SESSION['modelo'] != '0'){
					$query_Recordsetcomun = sprintf("%s AND modelo = '%s'", $query_Recordsetcomun, $_SESSION['modelo']);
				}
			}
			$query_Recordsetcomun = sprintf("%s GROUP BY pieza_standar.id ORDER BY pieza_standar.id", $query_Recordsetcomun);
		}
		//BÚSQUEDA POR CÓDIGO DE PIEZA
		else if (isset($_SESSION['codigo_pieza']) && $_SESSION['codigo_pieza'] != "" ) {  
			$codigo = $_SESSION['codigo_pieza'];
			//$oemPosible = "%" . $_SESSION['codigo_pieza'] . "%";
			$oemPosible = $_SESSION['codigo_pieza'];
			$query_Recordsetcomun = sprintf("%s INNER JOIN piezacodigo ON pieza_standar.id = piezacodigo.id_pieza WHERE  codigo = '%s' UNION SELECT pieza_standar.* FROM pieza_standar INNER JOIN vehiculo ON pieza_standar.id = vehiculo.id_pieza WHERE oems = '%s'",$query_Recordsetcomun, $codigo, $oemPosible);
		}
		//BÚSQUEDA POR MEDIDA
		else if (isset($_SESSION['tipo_pieza'])){
			$tipoPieza = $_SESSION['tipo_pieza'];
			$query_Recordsetcomun = sprintf("%s WHERE tipoPieza = '%s'", $query_Recordsetcomun, $tipoPieza);
			
			//CRUCETA
			if ($tipoPieza == "Cruceta"){
				if (isset($_SESSION['diametro']) && $_SESSION['diametro'] != "") {  //CARGÓ DIAMETRO
					$diametro = $_SESSION['diametro'];
					$diametro = str_replace(',', '.', $diametro);
					$diametroMenor = $diametro - 0.5;
					$diametroMayor = $diametro + 0.5;
					$query_Recordsetcomun = sprintf("%s AND (
						(CAST(REPLACE(C1, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(C1, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(Di, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(Di, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(S1, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(S1, ',', '.') AS DECIMAL(10,4)) <= %s)",$query_Recordsetcomun, $diametroMenor, $diametroMayor, $diametroMenor, $diametroMayor, $diametroMenor, $diametroMayor);
					if (isset($_SESSION['longitud']) && $_SESSION['longitud']){   //Y TAMBIÉN LONGITUD
						$longitud = $_SESSION['longitud'];
						$longitud = str_replace(',', '.', $longitud);
						$longitudMenor = $longitud - 0.5;
						$longitudMayor = $longitud + 0.5;
						$query_Recordsetcomun = sprintf("%s AND (
						(CAST(REPLACE(A1, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(A1, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(A2, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(A2, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(B1, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(B1, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(C2, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(C2, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(C3, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(C3, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(C4, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(C4, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(D1, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(D1, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(D2, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(D2, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(H, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(H, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(H1, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(H1, ',', '.') AS DECIMAL(10,4)) <= %s))",$query_Recordsetcomun, $longitudMenor, $longitudMayor, $longitudMenor, $longitudMayor, $longitudMenor, $longitudMayor, $longitudMenor, $longitudMayor, $longitudMenor, $longitudMayor, $longitudMenor, $longitudMayor, $longitudMenor, $longitudMayor, $longitudMenor, $longitudMayor, $longitudMenor, $longitudMayor, $longitudMenor, $longitudMayor);
					}
					$query_Recordsetcomun = sprintf("%s)", $query_Recordsetcomun);
				}
				else if (isset($_SESSION['longitud']) && $_SESSION['longitud']){  //SOLO CARGÓ LONGITUD
					$longitud = $_SESSION['longitud'];
					$longitud = str_replace(',', '.', $longitud);
					$longitudMenor = $longitud - 0.5;
					$longitudMayor = $longitud + 0.5;
					$query_Recordsetcomun = sprintf("%s AND (
						(CAST(REPLACE(A1, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(A1, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(A2, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(A2, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(B1, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(B1, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(C2, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(C2, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(C3, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(C3, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(C4, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(C4, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(D1, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(D1, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(D2, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(D2, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(H, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(H, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(H1, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(H1, ',', '.') AS DECIMAL(10,4)) <= %s))",$query_Recordsetcomun, $longitudMenor, $longitudMayor, $longitudMenor, $longitudMayor, $longitudMenor, $longitudMayor, $longitudMenor, $longitudMayor, $longitudMenor, $longitudMayor, $longitudMenor, $longitudMayor, $longitudMenor, $longitudMayor, $longitudMenor, $longitudMayor, $longitudMenor, $longitudMayor, $longitudMenor, $longitudMayor);
				}
			}
			//TRICETA
			else if ($tipoPieza == "Triceta"){
				if (isset($_SESSION['diametro']) && $_SESSION['diametro'] != "") {  //CARGÓ DIAMETRO
					$diametro = $_SESSION['diametro'];
					$diametro = str_replace(',', '.', $diametro);
					$diametroMenor = $diametro - 0.5;
					$diametroMayor = $diametro + 0.5;
					$query_Recordsetcomun = sprintf("%s AND (
						(CAST(REPLACE(C1, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(C1, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(Di, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(Di, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(S1, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(S1, ',', '.') AS DECIMAL(10,4)) <= %s)",$query_Recordsetcomun, $diametroMenor, $diametroMayor, $diametroMenor, $diametroMayor, $diametroMenor, $diametroMayor);
					if (isset($_SESSION['longitud']) && $_SESSION['longitud'] 
					&& is_numeric($_SESSION['longitud'])){   //Y TAMBIÉN LONGITUD(ESTRIAS)
						$longitudMenor = $_SESSION['longitud'] - 0.5;
						$longitudMayor = $_SESSION['longitud'] + 0.5;
						$query_Recordsetcomun = sprintf("%s) AND (
							CAST(REPLACE(Z, ',', '.') AS DECIMAL(10,4)) >= %s 
								AND CAST(REPLACE(Z, ',', '.') AS DECIMAL(10,4)) <= %s",$query_Recordsetcomun,  $longitudMenor, $longitudMayor);
					}
					$query_Recordsetcomun = sprintf("%s)", $query_Recordsetcomun);
				}
				else if (isset($_SESSION['longitud']) && $_SESSION['longitud']){  //SOLO CARGÓ LONGITUD (ESTRIAS)
					$longitud = $_SESSION['longitud'];
					$longitud = str_replace(',', '.', $longitud);
					$longitudMenor = $_SESSION['longitud'] - 0.5;
					$longitudMayor = $_SESSION['longitud'] + 0.5;
					$query_Recordsetcomun = sprintf("%s AND (
						CAST(REPLACE(Z, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(Z, ',', '.') AS DECIMAL(10,4)) <= %s)",$query_Recordsetcomun, $longitudMenor, $longitudMayor);
				}
			}
		}
		
		//PARA TEST
		//echo "_SESSION Seteada? = " . isset($_SESSION['idMarca']) . " - Valor idMarca = " . $_SESSION['idMarca'];
		//echo "TipoPieza:". $_SESSION['tipo_pieza'];
		//echo "\n CONSULTA SQL: ". $query_Recordsetcomun;
		$query_limit_Recordsetcomun = sprintf("%s LIMIT %d, %d", $query_Recordsetcomun, $startRow_Recordsetcomun, $maxRows_Recordsetcomun);
		$Recordsetcomun = mysqli_query($catalogo, $query_limit_Recordsetcomun) or die(mysql_error());
		$row_Recordsetcomun = $Recordsetcomun->fetch_assoc();
		
		if (isset($_GET['totalRows_Recordsetcomun'])) {
		  $totalRows_Recordsetcomun = $_GET['totalRows_Recordsetcomun'];
		} else {
		  $all_Recordsetcomun = mysqli_query($catalogo, $query_Recordsetcomun);
		  $totalRows_Recordsetcomun = $all_Recordsetcomun->num_rows;
		}
		$totalPages_Recordsetcomun = ceil($totalRows_Recordsetcomun/$maxRows_Recordsetcomun)-1;
		
		$queryString_Recordsetcomun = "";
		if (!empty($_SERVER['QUERY_STRING'])) {
		  $params = explode("&", $_SERVER['QUERY_STRING']);
		  $newParams = array();
		  foreach ($params as $param) {
			if (stristr($param, "pageNum_Recordsetcomun") == false && 
				stristr($param, "totalRows_Recordsetcomun") == false) {
			  array_push($newParams, $param);
			}
		  }
		  if (count($newParams) != 0) {
			$queryString_Recordsetcomun = "&" . htmlentities(implode("&", $newParams));
		  }
		}
		$queryString_Recordsetcomun = sprintf("&totalRows_Recordsetcomun=%d%s", $totalRows_Recordsetcomun, $queryString_Recordsetcomun);
		
		
		//PARA TEST
		//echo "TipoPieza: " . $_GET['tipo_pieza'] . " - Diametro: " . $_GET['diametro'] . " - SesionDiametro: " . $_SESSION['diametro'] . " - Longitud: " . $_GET['longitud'] . " SesiónLongitud: " . $_SESSION['longitud'];
		
		$query_Recordsetstrong = "SELECT pieza_strong.* FROM pieza_strong";
		//$query_Recordsetstrong = "select pieza.*, piezacodigo.id_marca from pieza left join piezacodigo on pieza.id = piezacodigo.id_pieza where id_marca = 'c8730441-b37d-4824-8a40-a00c22ba8979' or id_marca = '2be8c832-3d18-4721-b64f-9dfc16d02c8e' or id_marca = '1417ea7e-7f6c-4854-9e23-becf7bb4582a'";
		//BÚSQUEDA POR MARCA/SEGMENTO/MODELO
		if (isset($_SESSION['idMarca']) && $_SESSION['idMarca'] != "") { 
			$query_Recordsetstrong = sprintf("%s INNER JOIN vehiculo ON pieza_strong.id = vehiculo.id_pieza LEFT JOIN segmento ON vehiculo.id_segmento = segmento.id  WHERE id_marcavehiculo = '%s'", $query_Recordsetstrong, $_SESSION['idMarca']);
			if (isset($_SESSION['segmento']) && $_SESSION['segmento'] != "" && $_SESSION['segmento'] != '0'){
				$query_Recordsetstrong = sprintf("%s AND segmento = '%s'", $query_Recordsetstrong, $_SESSION['segmento']);
				if (isset($_SESSION['modelo']) && $_SESSION['modelo'] != "" && $_SESSION['modelo'] != '0'){
					$query_Recordsetstrong = sprintf("%s AND modelo = '%s'", $query_Recordsetstrong, $_SESSION['modelo']);
				}
			}
			$query_Recordsetstrong = sprintf("%s GROUP BY pieza_strong.id ORDER BY pieza_strong.id", $query_Recordsetstrong);
		}
		//BÚSQUEDA POR CÓDIGO DE PIEZA
		else if (isset($_SESSION['codigo_pieza']) && $_SESSION['codigo_pieza'] != "" ) {  
			$codigo = $_SESSION['codigo_pieza'];
			//$oemPosible = "%" . $_SESSION['codigo_pieza'] . "%";
			$oemPosible = $_SESSION['codigo_pieza'];
			$query_Recordsetstrong = sprintf("%s INNER JOIN piezacodigo ON pieza_strong.id = piezacodigo.id_pieza WHERE codigo = '%s' UNION SELECT pieza_strong.* FROM pieza_strong INNER JOIN vehiculo ON pieza_strong.id = vehiculo.id_pieza WHERE oems = '%s'",$query_Recordsetstrong, $codigo, $oemPosible);
		}
		//BÚSQUEDA POR MEDIDA
		else if (isset($_SESSION['tipo_pieza'])){
			$tipoPieza = $_SESSION['tipo_pieza'];
			$query_Recordsetstrong = sprintf("%s WHERE tipoPieza = '%s'", $query_Recordsetstrong, $tipoPieza);
			
			//CRUCETA
			if ($tipoPieza == "Cruceta"){
				if (isset($_SESSION['diametro']) && $_SESSION['diametro'] != "") {  //CARGÓ DIAMETRO
					$diametro = $_SESSION['diametro'];
					$diametro = str_replace(',', '.', $diametro);
					$diametroMenor = $diametro - 0.5;
					$diametroMayor = $diametro + 0.5;
					$query_Recordsetstrong = sprintf("%s AND (
						(CAST(REPLACE(C1, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(C1, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(Di, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(Di, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(S1, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(S1, ',', '.') AS DECIMAL(10,4)) <= %s)",$query_Recordsetstrong, $diametroMenor, $diametroMayor, $diametroMenor, $diametroMayor, $diametroMenor, $diametroMayor);
					if (isset($_SESSION['longitud']) && $_SESSION['longitud']){   //Y TAMBIÉN LONGITUD
						$longitud = $_SESSION['longitud'];
						$longitud = str_replace(',', '.', $longitud);
						$longitudMenor = $longitud - 0.5;
						$longitudMayor = $longitud + 0.5;
						$query_Recordsetstrong = sprintf("%s AND (
						(CAST(REPLACE(A1, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(A1, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(A2, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(A2, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(B1, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(B1, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(C2, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(C2, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(C3, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(C3, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(C4, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(C4, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(D1, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(D1, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(D2, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(D2, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(H, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(H, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(H1, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(H1, ',', '.') AS DECIMAL(10,4)) <= %s))",$query_Recordsetstrong, $longitudMenor, $longitudMayor, $longitudMenor, $longitudMayor, $longitudMenor, $longitudMayor, $longitudMenor, $longitudMayor, $longitudMenor, $longitudMayor, $longitudMenor, $longitudMayor, $longitudMenor, $longitudMayor, $longitudMenor, $longitudMayor, $longitudMenor, $longitudMayor, $longitudMenor, $longitudMayor);
					}
					$query_Recordsetstrong = sprintf("%s)", $query_Recordsetstrong);
				}
				else if (isset($_SESSION['longitud']) && $_SESSION['longitud']){  //SOLO CARGÓ LONGITUD
					$longitud = $_SESSION['longitud'];
					$longitud = str_replace(',', '.', $longitud);
					$longitudMenor = $longitud - 0.5;
					$longitudMayor = $longitud + 0.5;
					$query_Recordsetstrong = sprintf("%s AND (
						(CAST(REPLACE(A1, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(A1, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(A2, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(A2, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(B1, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(B1, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(C2, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(C2, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(C3, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(C3, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(C4, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(C4, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(D1, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(D1, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(D2, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(D2, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(H, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(H, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(H1, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(H1, ',', '.') AS DECIMAL(10,4)) <= %s))",$query_Recordsetstrong, $longitudMenor, $longitudMayor, $longitudMenor, $longitudMayor, $longitudMenor, $longitudMayor, $longitudMenor, $longitudMayor, $longitudMenor, $longitudMayor, $longitudMenor, $longitudMayor, $longitudMenor, $longitudMayor, $longitudMenor, $longitudMayor, $longitudMenor, $longitudMayor, $longitudMenor, $longitudMayor);
				}
			}
			//TRICETA
			else if ($tipoPieza == "Triceta"){
				if (isset($_SESSION['diametro']) && $_SESSION['diametro'] != "") {  //CARGÓ DIAMETRO
					$diametro = $_SESSION['diametro'];
					$diametro = str_replace(',', '.', $diametro);
					$diametroMenor = $diametro - 0.5;
					$diametroMayor = $diametro + 0.5;
					$query_Recordsetstrong = sprintf("%s AND (
						(CAST(REPLACE(C1, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(C1, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(Di, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(Di, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(S1, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(S1, ',', '.') AS DECIMAL(10,4)) <= %s)",$query_Recordsetstrong, $diametroMenor, $diametroMayor, $diametroMenor, $diametroMayor, $diametroMenor, $diametroMayor);
					if (isset($_SESSION['longitud']) && $_SESSION['longitud'] 
					&& is_numeric($_SESSION['longitud'])){   //Y TAMBIÉN LONGITUD(ESTRIAS)
						$longitudMenor = $_SESSION['longitud'] - 0.5;
						$longitudMayor = $_SESSION['longitud'] + 0.5;
						$query_Recordsetstrong = sprintf("%s) AND (
							CAST(REPLACE(Z, ',', '.') AS DECIMAL(10,4)) >= %s 
								AND CAST(REPLACE(Z, ',', '.') AS DECIMAL(10,4)) <= %s",$query_Recordsetstrong,  $longitudMenor, $longitudMayor);
					}
					$query_Recordsetstrong = sprintf("%s)", $query_Recordsetstrong);
				}
				else if (isset($_SESSION['longitud']) && $_SESSION['longitud']){  //SOLO CARGÓ LONGITUD (ESTRIAS)
					$longitud = $_SESSION['longitud'];
					$longitud = str_replace(',', '.', $longitud);
					$longitudMenor = $_SESSION['longitud'] - 0.5;
					$longitudMayor = $_SESSION['longitud'] + 0.5;
					$query_Recordsetstrong = sprintf("%s AND (
						CAST(REPLACE(Z, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(Z, ',', '.') AS DECIMAL(10,4)) <= %s)",$query_Recordsetstrong, $longitudMenor, $longitudMayor);
				}
			}
		}
		
		//PARA TEST
		//echo "_SESSION Seteada? = " . isset($_SESSION['idMarca']) . " - Valor idMarca = " . $_SESSION['idMarca'];
		//echo "TipoPieza:". $_SESSION['tipo_pieza'];
		//echo "\n CONSULTA SQL: ". $query_Recordsetstrong;
		$query_limit_Recordsetstrong = sprintf("%s LIMIT %d, %d", $query_Recordsetstrong, $startRow_Recordsetstrong, $maxRows_Recordsetstrong);
		$Recordsetstrong = mysqli_query($catalogo, $query_limit_Recordsetstrong) or die(mysql_error());
		$row_Recordsetstrong = $Recordsetstrong->fetch_assoc();
		
		if (isset($_GET['totalRows_Recordsetstrong'])) {
		  $totalRows_Recordsetstrong = $_GET['totalRows_Recordsetstrong'];
		} else {
		  $all_Recordsetstrong = mysqli_query($catalogo, $query_Recordsetstrong);
		  $totalRows_Recordsetstrong = $all_Recordsetstrong->num_rows;
		}
		$totalPages_Recordsetstrong = ceil($totalRows_Recordsetstrong/$maxRows_Recordsetstrong)-1;
		
		$queryString_Recordsetstrong = "";
		if (!empty($_SERVER['QUERY_STRING'])) {
		  $params = explode("&", $_SERVER['QUERY_STRING']);
		  $newParams = array();
		  foreach ($params as $param) {
			if (stristr($param, "pageNum_Recordsetstrong") == false && 
				stristr($param, "totalRows_Recordsetstrong") == false) {
			  array_push($newParams, $param);
			}
		  }
		  if (count($newParams) != 0) {
			$queryString_Recordsetstrong = "&" . htmlentities(implode("&", $newParams));
		  }
		}
		$queryString_Recordsetstrong = sprintf("&totalRows_Recordsetstrong=%d%s", $totalRows_Recordsetstrong, $queryString_Recordsetstrong);
    
	
		//PARA TEST
		//echo "TipoPieza: " . $_GET['tipo_pieza'] . " - Diametro: " . $_GET['diametro'] . " - SesionDiametro: " . $_SESSION['diametro'] . " - Longitud: " . $_GET['longitud'] . " SesiónLongitud: " . $_SESSION['longitud'];
		
		$query_Recordsetpremium = "SELECT pieza_premiun.* FROM pieza_premiun";
		//$query_Recordsetpremium = "select pieza.*, piezacodigo.id_marca from pieza left join piezacodigo on pieza.id = piezacodigo.id_pieza where id_marca = 'c8730441-b37d-4824-8a40-a00c22ba8979' or id_marca = '2be8c832-3d18-4721-b64f-9dfc16d02c8e' or id_marca = '1417ea7e-7f6c-4854-9e23-becf7bb4582a'";
		//BÚSQUEDA POR MARCA/SEGMENTO/MODELO
		if (isset($_SESSION['idMarca']) && $_SESSION['idMarca'] != "") { 
			$query_Recordsetpremium = sprintf("%s INNER JOIN vehiculo ON pieza_premiun.id = vehiculo.id_pieza LEFT JOIN segmento ON vehiculo.id_segmento = segmento.id  WHERE id_marcavehiculo = '%s'", $query_Recordsetpremium, $_SESSION['idMarca']);
			if (isset($_SESSION['segmento']) && $_SESSION['segmento'] != "" && $_SESSION['segmento'] != '0'){
				$query_Recordsetpremium = sprintf("%s AND segmento = '%s'", $query_Recordsetpremium, $_SESSION['segmento']);
				if (isset($_SESSION['modelo']) && $_SESSION['modelo'] != "" && $_SESSION['modelo'] != '0'){
					$query_Recordsetpremium = sprintf("%s AND modelo = '%s'", $query_Recordsetpremium, $_SESSION['modelo']);
				}
			}
			$query_Recordsetpremium = sprintf("%s GROUP BY pieza_premiun.id ORDER BY pieza_premiun.id", $query_Recordsetpremium);
		}
		//BÚSQUEDA POR CÓDIGO DE PIEZA
		else if (isset($_SESSION['codigo_pieza']) && $_SESSION['codigo_pieza'] != "" ) {  
			$codigo = $_SESSION['codigo_pieza'];
			//$oemPosible = "%" . $_SESSION['codigo_pieza'] . "%";
			$oemPosible = $_SESSION['codigo_pieza'];
			$query_Recordsetpremium = sprintf("%s INNER JOIN piezacodigo ON pieza_premiun.id = piezacodigo.id_pieza WHERE codigo = '%s' UNION SELECT pieza_premiun.* FROM pieza_premiun INNER JOIN vehiculo ON pieza_premiun.id = vehiculo.id_pieza WHERE oems = '%s'",$query_Recordsetpremium, $codigo, $oemPosible);
		}
		//BÚSQUEDA POR MEDIDA
		else if (isset($_SESSION['tipo_pieza'])){
			$tipoPieza = $_SESSION['tipo_pieza'];
			$query_Recordsetpremium = sprintf("%s WHERE tipoPieza = '%s'", $query_Recordsetpremium, $tipoPieza);
			
			//CRUCETA
			if ($tipoPieza == "Cruceta"){
				if (isset($_SESSION['diametro']) && $_SESSION['diametro'] != "") {  //CARGÓ DIAMETRO
					$diametro = $_SESSION['diametro'];
					$diametro = str_replace(',', '.', $diametro);
					$diametroMenor = $diametro - 0.5;
					$diametroMayor = $diametro + 0.5;
					$query_Recordsetpremium = sprintf("%s AND (
						(CAST(REPLACE(C1, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(C1, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(Di, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(Di, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(S1, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(S1, ',', '.') AS DECIMAL(10,4)) <= %s)",$query_Recordsetpremium, $diametroMenor, $diametroMayor, $diametroMenor, $diametroMayor, $diametroMenor, $diametroMayor);
					if (isset($_SESSION['longitud']) && $_SESSION['longitud']){   //Y TAMBIÉN LONGITUD
						$longitud = $_SESSION['longitud'];
						$longitud = str_replace(',', '.', $longitud);
						$longitudMenor = $longitud - 0.5;
						$longitudMayor = $longitud + 0.5;
						$query_Recordsetpremium = sprintf("%s AND (
						(CAST(REPLACE(A1, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(A1, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(A2, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(A2, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(B1, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(B1, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(C2, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(C2, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(C3, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(C3, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(C4, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(C4, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(D1, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(D1, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(D2, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(D2, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(H, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(H, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(H1, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(H1, ',', '.') AS DECIMAL(10,4)) <= %s))",$query_Recordsetpremium, $longitudMenor, $longitudMayor, $longitudMenor, $longitudMayor, $longitudMenor, $longitudMayor, $longitudMenor, $longitudMayor, $longitudMenor, $longitudMayor, $longitudMenor, $longitudMayor, $longitudMenor, $longitudMayor, $longitudMenor, $longitudMayor, $longitudMenor, $longitudMayor, $longitudMenor, $longitudMayor);
					}
					$query_Recordsetpremium = sprintf("%s)", $query_Recordsetpremium);
				}
				else if (isset($_SESSION['longitud']) && $_SESSION['longitud']){  //SOLO CARGÓ LONGITUD
					$longitud = $_SESSION['longitud'];
					$longitud = str_replace(',', '.', $longitud);
					$longitudMenor = $longitud - 0.5;
					$longitudMayor = $longitud + 0.5;
					$query_Recordsetpremium = sprintf("%s AND (
						(CAST(REPLACE(A1, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(A1, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(A2, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(A2, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(B1, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(B1, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(C2, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(C2, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(C3, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(C3, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(C4, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(C4, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(D1, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(D1, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(D2, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(D2, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(H, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(H, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(H1, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(H1, ',', '.') AS DECIMAL(10,4)) <= %s))",$query_Recordsetpremium, $longitudMenor, $longitudMayor, $longitudMenor, $longitudMayor, $longitudMenor, $longitudMayor, $longitudMenor, $longitudMayor, $longitudMenor, $longitudMayor, $longitudMenor, $longitudMayor, $longitudMenor, $longitudMayor, $longitudMenor, $longitudMayor, $longitudMenor, $longitudMayor, $longitudMenor, $longitudMayor);
				}
			}
			//TRICETA
			else if ($tipoPieza == "Triceta"){
				if (isset($_SESSION['diametro']) && $_SESSION['diametro'] != "") {  //CARGÓ DIAMETRO
					$diametro = $_SESSION['diametro'];
					$diametro = str_replace(',', '.', $diametro);
					$diametroMenor = $diametro - 0.5;
					$diametroMayor = $diametro + 0.5;
					$query_Recordsetpremium = sprintf("%s AND (
						(CAST(REPLACE(C1, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(C1, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(Di, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(Di, ',', '.') AS DECIMAL(10,4)) <= %s) 
						OR (CAST(REPLACE(S1, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(S1, ',', '.') AS DECIMAL(10,4)) <= %s)",$query_Recordsetpremium, $diametroMenor, $diametroMayor, $diametroMenor, $diametroMayor, $diametroMenor, $diametroMayor);
					if (isset($_SESSION['longitud']) && $_SESSION['longitud'] 
					&& is_numeric($_SESSION['longitud'])){   //Y TAMBIÉN LONGITUD(ESTRIAS)
						$longitudMenor = $_SESSION['longitud'] - 0.5;
						$longitudMayor = $_SESSION['longitud'] + 0.5;
						$query_Recordsetpremium = sprintf("%s) AND (
							CAST(REPLACE(Z, ',', '.') AS DECIMAL(10,4)) >= %s 
								AND CAST(REPLACE(Z, ',', '.') AS DECIMAL(10,4)) <= %s",$query_Recordsetpremium,  $longitudMenor, $longitudMayor);
					}
					$query_Recordsetpremium = sprintf("%s)", $query_Recordsetpremium);
				}
				else if (isset($_SESSION['longitud']) && $_SESSION['longitud']){  //SOLO CARGÓ LONGITUD (ESTRIAS)
					$longitud = $_SESSION['longitud'];
					$longitud = str_replace(',', '.', $longitud);
					$longitudMenor = $_SESSION['longitud'] - 0.5;
					$longitudMayor = $_SESSION['longitud'] + 0.5;
					$query_Recordsetpremium = sprintf("%s AND (
						CAST(REPLACE(Z, ',', '.') AS DECIMAL(10,4)) >= %s 
							AND CAST(REPLACE(Z, ',', '.') AS DECIMAL(10,4)) <= %s)",$query_Recordsetpremium, $longitudMenor, $longitudMayor);
				}
			}
		}
		
		//PARA TEST
		//echo "_SESSION Seteada? = " . isset($_SESSION['idMarca']) . " - Valor idMarca = " . $_SESSION['idMarca'];
		//echo "TipoPieza:". $_SESSION['tipo_pieza'];
		//echo "\n CONSULTA SQL: ". $query_Recordsetpremium;
		$query_limit_Recordsetpremium = sprintf("%s LIMIT %d, %d", $query_Recordsetpremium, $startRow_Recordsetpremium, $maxRows_Recordsetpremium);
		$Recordsetpremium = mysqli_query($catalogo, $query_limit_Recordsetpremium) or die(mysql_error());
		$row_Recordsetpremium = $Recordsetpremium->fetch_assoc();
		
		if (isset($_GET['totalRows_Recordsetpremium'])) {
		  $totalRows_Recordsetpremium = $_GET['totalRows_Recordsetpremium'];
		} else {
		  $all_Recordsetpremium = mysqli_query($catalogo, $query_Recordsetpremium);
		  $totalRows_Recordsetpremium = $all_Recordsetpremium->num_rows;
		}
		$totalPages_Recordsetpremium = ceil($totalRows_Recordsetpremium/$maxRows_Recordsetpremium)-1;
		
		$queryString_Recordsetpremium = "";
		if (!empty($_SERVER['QUERY_STRING'])) {
		  $params = explode("&", $_SERVER['QUERY_STRING']);
		  $newParams = array();
		  foreach ($params as $param) {
			if (stristr($param, "pageNum_Recordsetpremium") == false && 
				stristr($param, "totalRows_Recordsetpremium") == false) {
			  array_push($newParams, $param);
			}
		  }
		  if (count($newParams) != 0) {
			$queryString_Recordsetpremium = "&" . htmlentities(implode("&", $newParams));
		  }
		}
		$queryString_Recordsetpremium = sprintf("&totalRows_Recordsetpremium=%d%s", $totalRows_Recordsetpremium, $queryString_Recordsetpremium);
    } 
?>