<?php
// session_start();
declare(strict_types=0);

foreach($_GET  as $key => $value){${$key}=trim($value);} 
foreach($_POST as $key => $value){${$key}=trim($value);} 
include_once('lib_main.php');


// Conexión a la base de datos
$servername = "50.6.168.20";
$servername = "192.185.41.206";
$username = "vcrxol_lucafull";
$password = "vicore1976";
$database = "vcrxol_luca";

$user_codigo="vicore@hotmail.com";
$cfg_empr = "luca";
$user0=$user_codigo;
$empr0=$cfg_empr;

$conn = new mysqli($servername, $username, $password, $database);
if ($conn->connect_error) {
    die("Conexión fallida: " . $conn->connect_error);
}

if ($macc == 1)
       {
       
$file='../_lib/tabla_html.html';
echo "<br>"."Archivo : ".$file;


echo 'Existe ===> '.file_exists($file);
// Cargar el contenido del archivo HTML
$html = file_get_contents($file);
//echo "===>".$html;

echo "==== hola mundo";


// Usar DOMDocument para parsear el HTML
$dom = new DOMDocument();
@$dom->loadHTML($html);
$rows = $dom->getElementsByTagName('tr');
$linea = 1; 
$mayor = 0 ;
foreach ($rows as $row) 
{
          $cols = $row->getElementsByTagName('td');
          if ($cols->length > 0) 
             {
                 $code_vta = trim($cols->item(0)->nodeValue);
                 $code_nue = new_item() ;   
                 $name_ori = trim($cols->item(1)->nodeValue);
                 $qty_available = trim($cols->item(2)->nodeValue);
                 $virtual_available = trim($cols->item(3)->nodeValue);
                 $lst_price = trim($cols->item(4)->nodeValue);
                 $state = trim($cols->item(5)->nodeValue);
                 $state = '-' ;
                 $busca = '"' ;
                 $reemp = ''  ;
                 $name1 = str_replace($busca, $reemp, $name_ori) ;

                 $busca = 'Ó' ;
                 $name2 = str_replace($busca, $reemp, $name1) ;

                 $busca = 'Ã?' ;
                 $name  = str_replace($busca, $reemp, $name2) ;

                 $largo = strlen(trim($code_vta)) ;

            
                 if ($largo > $mayor)
                     $mayor = $largo ;
            
                 // Update en la base de datos
                 $conn->query("delete from item0 where item_codigo = '".$code_nue."'");

                 if (!empty($name))
                 {
                    echo "<BR> Lineas Procesadas ===> ".$linea;
                    echo " Actual => ".$code_vta." - Nuevo => ".$code_nue." US === ".$state," Longitud : ".$largo ;
                   
                    $sql_inse='insert into item0(item_codigo, item_descri, item_pvp1, item_status, item_codigov) values ("'.$code_nue.'","'.$name.'",'.$lst_price.',"'.$state.'","'. $code_vta.'")';
                    $res_inse=msql_query($sql_inse,$conn);


                   //$stmt = $conn->prepare("INSERT INTO item0 (item_codigo, item_descri, item_pvp1, item_status, item_codigov ) VALUES (?, ?, ?, ?, ?)");
                   // $stmt->bind_param("ssddds", $code_nue, $name, $lst_price, $state, $code_vta);
                   // $stmt->execute();
           
                    $linea=$linea+1;
                 }

             }
  
             //if ($linea == 5)
              //break;
  
 }
echo "Longitud Principal  ===> ".$mayor ;
     }
    
 if ($macc==2)
    {
        echo "***** Hola - ".$macc."<br>" ;
         $conn->query("delete from inve2 where 1=1 ");


         $item_sql = "select * from item0 order by item_codigo" ;
         $item_res=msql_query($item_sql,$conn);
         $i = 1 ;
         while($row = $item_res->fetch_assoc()) 
             {
                if ($i < 5688)
                   {
                      echo "ID: " .$i. " - ". $row["item_codigo"]. " - Descripcion: " . $row["item_descri"]."<br>";
                      $item = $row["item_codigo"] ;
                      $puni = $row["item_puni1"]  ;
                      $sql_inse='insert into inve2(idnum2, iddet, item, cant, puni, iuser2, idate2)  values (1,'.$i.',"'.$item.'",10000,'.$puni.',2,now())' ;
                      echo "Sentencia : ".$sql_inse ;
                      $res_inse=msql_query($sql_inse,$conn);
                      
                   }
                $i++ ;              
             }
   
   }
 $conn->close();
?>