<?php
set_time_limit(600);
include "conn.php";

// si ya confirmaron pulsando el boton
// ****************************************************************************************************
if (isset($_POST['submit'])) {
  
$tiempo_inicial = microtime(true);

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); // Make MySQLi throw exceptions.

try {
    // Try Connect to the DB with new MySqli object - Params {hostname, userid, password, dbname}
    $mysqli = new mysqli($host, $user, $pass, $base);

    
    $handle = fopen($_FILES['metas']['tmp_name'], "r");
    $headers = fgetcsv($handle, 1000, ",");

    /*while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) 
    {
        echo $data[0];
        echo $data[1];
    }*/

    //$query = "DELETE FROM metas where genero='".$_POST["genero"]."'";
    //$mysqli->query($query);

    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        //echo "<p>Hay $num columnas en el renglon $data: <br /></p>\n";
        
        for ($c=0; $c < $num; $c++) {
            $respuesta = $respuesta . $data[0] . ' - ' . $data[1] . ' - ' . $data[2] . "<br />\n";
            //$query = "replace into tallas_comp(rc,talla,part) values('".$data[0]."','".$data[1]."','".$data[2]."')";
            $query = "REPLACE INTO tallas_comp_vta(linea,rc,color,talla,part,part_prom,part_vta,part_compra) values('".$data[0]."','".$data[1]."','".$data[2]."','".$data[3]."','".$data[4]."','".$data[5]."','".$data[6]."','".$data[7]."')";
            if($data[4]>0){
                $mysqli->query($query);

            }
            
            //echo $query."<hr>";
        }
    }



    fclose($handle);

} catch (mysqli_sql_exception $e) { // Failed to connect? Lets see the exception details..
    echo "MySQLi Error Code: " . $e->getCode() . "<br />";
    echo "Exception Msg: " . $e->getMessage();
    echo "<hr>" .$query;
    exit; // exit and close connection.
}

$mysqli->close(); // finally, close the connection

$tiempo_final = microtime(true);
$tiempo = round($tiempo_final - $tiempo_inicial);
$date = date('m/d/Y h:i:s a', time());
$mensaje = "se cargargon las tallas_rc en ".$tiempo." segundos. ".$date; 
       
 // FIN de ya confirmaron pulsando el boton
// ****************************************************************************************************       
} 
    
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); // Make MySQLi throw exceptions.

        try {
            // Try Connect to the DB with new MySqli object - Params {hostname, userid, password, dbname}
            $mysqli = new mysqli($host, $user, $pass, $base);


              $tienda = "Porcentaje de participación";
              
              $titulo = "Tallas";
              if(isset($mensaje)){
                $subtitulo = $mensaje;
              } else {
                $subtitulo = "La actualización las ventas tarda 15 segs. aprox.";
              }

            // # para mostrar antes de actualizar el inventario
            $query = "select * from globales
            ";

            //echo $query;
            
            if ($result = $mysqli->query($query)) {

                /* fetch associative array */
                while ($row = $result->fetch_assoc()) {
                    
                    $porcentaje_entallado_cedis = $row["porcentaje_entallado_cedis"];
                    $porcentaje_entallado_bodega = $row["porcentaje_entallado_bodega"];

                }

                /* free the memory associated with this result set */
                $result->free();
            }
        } catch (mysqli_sql_exception $e) { // Failed to connect? Lets see the exception details..
            echo "MySQLi Error Code: " . $e->getCode() . "<br />";
            echo "Exception Msg: " . $e->getMessage();
            exit; // exit and close connection.
        }

        $mysqli->close(); // finally, close the connection
    
 ?>  
 
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="Start your development with a Dashboard for Bootstrap 4.">
  <meta name="author" content="Creative Tim">
  <title><?php echo $marca;?></title>

  <!-- Fonts -->
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700">
  <!-- Icons -->
  <link rel="stylesheet" href="../../assets/vendor/nucleo/css/nucleo.css" type="text/css">
  <link rel="stylesheet" href="../../assets/vendor/@fortawesome/fontawesome-free/css/all.min.css" type="text/css">
  <!-- Page plugins -->
  <link rel="stylesheet" href="../../assets/vendor/datatables.net-bs4/css/dataTables.bootstrap4.min.css">
  <link rel="stylesheet" href="../../assets/vendor/datatables.net-buttons-bs4/css/buttons.bootstrap4.min.css">
  <link rel="stylesheet" href="../../assets/vendor/datatables.net-select-bs4/css/select.bootstrap4.min.css">
  <!-- Argon CSS -->
  <link rel="stylesheet" href="../../assets/css/argon.css?v=1.1.0" type="text/css">

</head>

<body>

  <!-- menu lateral -->
  <?php include "menu.php"?>
  
  <!-- Main content -->
  <div class="main-content" id="panel">
    <!-- Topnav -->
    
    <!-- Header -->
    <!-- Header -->
    <div class="header bg-primary pb-6">
      <div class="container-fluid">
        <div class="header-body">
        <div class="row align-items-center py-4">

<div class="col-lg-6 col-7">
  <h6 class="h2 text-white d-inline-block mb-0"><?php echo $tienda;?></h6>
</div>

  <!-- Sidenav toggler -->
  <?php include "toggler.php"?>


</div>
        </div>
      </div>
    </div>
<!-- Page content -->

<div class="container-fluid mt--6">

<div class="row">

  <div class="col-lg-6">
    <div class="card-wrapper">
     
      
      
      <!-- File browser -->
      <div class="card">
        <!-- Card header -->
        <div class="card-header">
          <h3 class="mb-0">Importar archivo CSV</h3>
        </div>
        <!-- Card body -->
        <div class="card-body">
        <form enctype='multipart/form-data' action='' method='post'>

            <div class="custom-file">
              <input type="file" name="metas" id="metas"> 
              
            </div>

            <div class="custom-file">
                <input type='submit' name='submit' value='Importar' onclick="if(document.getElementById('metas').value==''){alert('Seleccione un archivo CSV por favor');return false;}">
                <br>
                <hr>
                Descargar archivo de plantilla: <a href="tallas_rc.txt">TALLAS_RC.TXT</a>
            </div>
            
        </form>
        </div>
      </div>
      <!-- Checkboxes and radios -->

        
    </div>
  </div>
</div>
<?php echo $respuesta;?>
       <!-- Footer -->
       <footer class="footer pt-0">
         <div class="row align-items-center justify-content-lg-between">
           <div class="col-lg-6">
             <div class="copyright text-center text-lg-left text-muted">
               &copy; <?php echo $anio;?> <a href="#" class="font-weight-bold ml-1" ><?php echo $marca;?></a>
             </div>
           </div>

         </div>
       </footer>
    </div>
  </div>
  <!-- Argon Scripts -->
  <!-- Core -->
  <script src="../../assets/vendor/jquery/dist/jquery.min.js"></script>
  <script src="../../assets/vendor/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
  <script src="../../assets/vendor/js-cookie/js.cookie.js"></script>
  <script src="../../assets/vendor/jquery.scrollbar/jquery.scrollbar.min.js"></script>
  <script src="../../assets/vendor/jquery-scroll-lock/dist/jquery-scrollLock.min.js"></script>
  <!-- Optional JS -->
  <script src="../../assets/vendor/datatables.net/js/jquery.dataTables.min.js"></script>
  <script src="../../assets/vendor/datatables.net-bs4/js/dataTables.bootstrap4.min.js"></script>
  <script src="../../assets/vendor/datatables.net-buttons/js/dataTables.buttons.min.js"></script>
  <script src="../../assets/vendor/datatables.net-buttons-bs4/js/buttons.bootstrap4.min.js"></script>
  <script src="../../assets/vendor/datatables.net-buttons/js/buttons.html5.min.js"></script>
  <script src="../../assets/vendor/datatables.net-buttons/js/buttons.flash.min.js"></script>
  <script src="../../assets/vendor/datatables.net-buttons/js/buttons.print.min.js"></script>
  <script src="../../assets/vendor/datatables.net-select/js/dataTables.select.min.js"></script>
  <!-- Argon JS -->
  <script src="../../assets/js/argon.js?v=1.1.0"></script>

</body>

</html>
 