<?php
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("localhost", "lara1", "myLARA", "lara1");
            // Select all records from the db. Keep inside try block incase there an issue with sql query

        } 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.
        }

        
   //echo "<a href=/>Regresar</a>";     

if (isset($_POST['submit'])) 
{
    
    $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++) {
            echo $data[2] . ' - ' . $headers[$c] . ' - ' . $data[1] . ' - ' . $data[$c] . ' - ' . $data[0] . "<br />\n";
            $query = "replace into metas(tienda,genero,linea,almacen,meta,total) values('".$data[2]."','".$_POST["genero"]."','".$headers[$c]."','".$data[1]."','".$data[$c]."','".$data[0]."')";
            $mysqli->query($query);
        }
    }
    
    $query = "DELETE FROM metas WHERE linea IN ('#SKUS','UBIC','TDA','Total general')";
    $mysqli->query($query);

    $query = "DELETE FROM metas WHERE almacen='TOTALES'";
    $mysqli->query($query);

    $query = "delete from metas where left(tienda,1)='%'";
    $mysqli->query($query);

    $query = "delete from metas where left(tienda,5)='TOTAL'";
    $mysqli->query($query);

    $query = "UPDATE metas SET linea='BERMUDA FRENCH' WHERE linea='BERMUDA'";
    $mysqli->query($query);

    $query = "UPDATE metas SET linea='CAMISA M CORTA CASUAL' WHERE linea='CAMISA M CORTA'";
    $mysqli->query($query);

    $query = "UPDATE metas SET linea='PANTALON JOGGER CASUAL' WHERE linea='JOGGER CASUAL'";
    $mysqli->query($query);

    $query = "UPDATE metas SET linea='TRAJE SMOKING' WHERE linea='SOMOKING'";
    $mysqli->query($query);

    $query = "UPDATE metas SET linea='CHALECO LIGERO' WHERE linea='CHALECO LIGERO DAMA'";
    $mysqli->query($query);

    $query = "UPDATE metas SET linea='CHAMARRA LIGERA' WHERE linea='CHAMARRA DAMA LIGERA'";
    $mysqli->query($query);

    $query = "UPDATE metas SET linea='CONJUNTO MANGA CORTA' WHERE linea='CONJUNTO M CORTA'";
    $mysqli->query($query);

    $query = "UPDATE metas SET linea='CONJUNTO MANGA LARGA' WHERE linea='CONJUNTO M LARGA'";
    $mysqli->query($query);

    $query = "UPDATE metas SET linea='PLAYERA MANGA CORTA' WHERE linea='PLAYERA M CORTA'";
    $mysqli->query($query);

    $query = "UPDATE metas SET linea='PLAYERA MANGA LARGA' WHERE linea='PLAYERA M LARGA'";
    $mysqli->query($query);

    $query = "UPDATE metas SET linea='SACO MODA' WHERE linea='SACO DAMA MODA'";
    $mysqli->query($query);

    $query = "UPDATE metas SET linea='SWEATER LIGERO' WHERE linea='SWEATER LIGERO DAMA'";
    $mysqli->query($query);

    $query = "DROP TABLE linea_gen_meta";
    $mysqli->query($query);

    $query = "create table linea_gen_meta
select genero,linea,sum(meta) as meta
from metas 
where almacen='piso' 
group by 1,2";
    $mysqli->query($query);

    $query = "DELETE FROM tot_entallado";
    $mysqli->query($query);

    $query = "INSERT INTO tot_entallado(tienda,genero,total)
    SELECT tienda,genero,SUM(meta) AS tot_meta
    FROM metas
    GROUP BY 1,2";
    $mysqli->query($query);

    $query = "INSERT INTO tot_entallado(tienda,genero,total)
    SELECT '00 CDS' as tienda,genero,round(SUM(meta)*(porcentaje_entallado_cedis/100)) AS tot_meta
    FROM metas inner join globales g
    where almacen='piso'
    GROUP BY 1,2";
    $mysqli->query($query);

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

}
?>
 <!DOCTYPE html>
<html>

<head>

  <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">
  <!-- 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">Actualizar entallados meta</h6>
              <nav aria-label="breadcrumb" class="d-none d-md-inline-block ml-md-4">
                <ol class="breadcrumb breadcrumb-links breadcrumb-dark">
                <li class="breadcrumb-item"><a href="/"><i class="fas fa-home"></i></a></li>

                </ol>
              </nav>
            </div>

          </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">
                    <select name="genero">
                      <option>CABALLERO</option>
                      <option>DAMA</option>
</select>
                  </div>
                  <div class="custom-file">
                    <input type="file" name="metas">
                    
                  </div>
                  <div class="custom-file">
                  <input type='submit' name='submit' value='Importar'>
                    
                  </div>
                  
              </form>
              </div>
            </div>
            <!-- Checkboxes and radios -->

              
          </div>
        </div>
      </div>
        <!-- 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>
  <!-- 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>
  <!-- Argon JS -->
  <script src="../../assets/js/argon.js?v=1.1.0"></script>
</body>

</html>