<?php
require_once 'shared/conexionBD.php';
//encode utf8
mysqli_query($conexion, "SET NAMES utf8");

$nombreParametros = ["tEti", "nITM", "cLITM", "nLote", "cPzas", "uMed", "tDoc", "nDoc", "UKID"];

if($_SERVER['METHOD'] == 'POST'){
    header("Location: https://www.solerpalau.mx");
    exit;
}else{

    if(empty($_GET)){
        header("Location: https://www.solerpalau.mx");
        exit;
    }else{
        if(!array_key_exists("data", $_GET)){
            header("Location: https://www.solerpalau.mx");
            exit;
        }

        $parametrosStr = $_GET["data"];
        $parametrosArr = explode(" ", $parametrosStr);

        if(count($parametrosArr) < 9){
            header("Location: https://www.solerpalau.mx");
            exit;
        }

        $dataArr = array();
        for($i = 0; $i < count($nombreParametros); $i++){
            $dataArr[$nombreParametros[$i]] = $parametrosArr[$i];
        }

        $qryReg = "SELECT IDREGISTRO FROM ITT_ALP_TABLAS WHERE CONTENIDO LIKE '%" . $dataArr["cLITM"] . "%'";
        $gstReg = mysqli_query($conexion, $qryReg);
        $date = date('Y-m-d H:i:s');
        $ip = $_SERVER['REMOTE_ADDR'];
        $cadqr = "https://solerpalau.mx" . $_SERVER['REQUEST_URI'];
        
        if($gstReg->num_rows == 0){
            $accionForm = "errorEtiqueta.php";
            $esta = "Error";
            $method = "POST";
        }else{
            $idRegistro = '';

            while($rowReg = mysqli_fetch_assoc($gstReg)){
                $idRegistro = $rowReg["IDREGISTRO"];
            }

            $qryProducto = "SELECT GL.LINEA, GL.MODELO, GL.SUBMODELO, PR.ARCH_FICH FROM ITT_ALP_GLOBAL AS GL LEFT JOIN ITT_ASW_PROD AS PR ON PR.SUBM = GL.SUBMODELO WHERE GL.ID = '$idRegistro' LIMIT 1";
            $gstProducto = mysqli_query($conexion, $qryProducto);

            while($rowProd = mysqli_fetch_assoc($gstProducto)){
                $linea = $rowProd["LINEA"];
                $modelo = $rowProd["MODELO"];
                $submodelo = $rowProd["SUBMODELO"];
                $ficha = $rowProd["ARCH_FICH"];
            }

            $accionForm = "producto.php";
            $esta = "Exito";
            $method = "GET";
        }
        
        $qryRegistrarConsulta = "INSERT INTO ITT_ASW_ETQR (FEHR, IPUS, CAQR, ESTA) VALUES ('$date', '$ip', '$cadqr', '$esta')";
        $gstRegCons = mysqli_query($conexion, $qryRegistrarConsulta);

        if($ficha !== null){
            $location = $ficha;
            
            if(file_exists($location)){
                $name = basename($location);
                $name = urlencode($name);
                
                header("Content-type: application/pdf");
                header('Content-Disposition: inline; filename="' . $name . '"');
                header('Expires: 0');
                header('Cache-Control: must-rSomething is wrongidate');
                header('Pragma: public');
                header('Content-Length: ' . filesize($location));
                // Send the file to the browser.
                readfile($location);
                exit;
            }
        }
    }
}
?>
<!DOCTYPE html>
<html xml:lang="es" lang="es">

<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<head>
<!-- Global site tag (gtag.js) - Google Analytics -->

    <!--Producción-->
    <script async src="https://www.googletagmanager.com/gtag/js?id=UA-125019062-2"></script>

    <!--Producción-->
    <script>

        window.dataLayer = window.dataLayer || [];

        function gtag(){dataLayer.push(arguments);}

        gtag('js', new Date());

        gtag('config', 'UA-125019062-2');

    </script>

    <!-- End Global site tag (gtag.js) - Google Analytics -->
  <!-- [if IE]> -->
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
  <meta http-equiv="cleartype" content="on"/>
  <!-- <![endif] -->
  <meta content="width=device-width, initial-scale=1" name="viewport"/>
  <title>Etiqueta – S&amp;P</title>
  <meta name="keywords" content="Varien, E-commerce"/>
  <meta name="robots" content="INDEX,FOLLOW"/>

  <link rel="icon" href="https://statics.solerpalau.com/skin/frontend/solerpalau/default/favicon.ico" type="image/x-icon"/>
  <!-- ================================================================================ -->
  <script type="text/javascript" src="js/events.js"></script>

 <!-- ================================================================================ -->
 <link rel="stylesheet" type="text/css" href="css/style-css01.css" media="all" />
 <!-- ================================================================================ -->
 <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
 <link href="css/flexslider.css" rel="stylesheet">
 <!-- ================================================================================ -->
</head>
<body>
    <form id="frm" action="<?php echo $accionForm; ?>" method="<?php echo $method; ?>">
        <?php if($esta == "Error") { ?>
            <input type="hidden" name="data" value="<?php echo base64_encode($cadqr); ?>">
        <?php } else { ?>
            <input type="hidden" name="linea" value="<?php echo $linea; ?>">
            <input type="hidden" name="modelo" value="<?php echo $modelo; ?>">
            <input type="hidden" name="submodelo" value="<?php echo $submodelo; ?>">
        <?php } ?>
    </form>

    <script>
        let frm = document.getElementById("frm");
        frm.submit();
    </script>
</body>
</html>