<?php
include "conn.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($host, $user, $pass, $base);

// guarda datos
if( $_POST["meta_piso"] > 0 
&& $_POST["meta_bodega"] > 0
 ) {
    
  $query = "update metas2
  SET `meta_piso` = '".$_POST["meta_piso"]."',
    `meta_bodega` = '".$_POST["meta_bodega"]."'
     where `tienda` = '".$_POST["tienda"]."'
    and `genero` = '".$_POST["genero"]."' and linea = '".$_POST["linea"]."'";
  $mysqli->query($query);

  # calcula el % de la meta del cedis
  $query = "REPLACE INTO metas2_editadas(tienda,genero,linea,meta_piso,meta_bodega,meta_cedis_calc)
  SELECT tienda,genero,linea,meta_piso,meta_bodega,meta_cedis_calc
  FROM metas2
  where `tienda` = '".$_POST["tienda"]."'
    and `genero` = '".$_POST["genero"]."' and linea = '".$_POST["linea"]."'";
  $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 where almacen='piso'
  GROUP BY 1,2";
  $mysqli->query($query);
  
  $date = date('m/d/Y h:i:s a', time());
  $mensaje = "se edito la meta de tienda = ".$_POST["tienda"]."
  genero = ".$_POST["genero"]." linea = ".$_POST["linea"]." meta_piso = ".$_POST["meta_piso"]." meta_bodega = ".$_POST["meta_bodega"];

  $mysqli->query("insert into log(tipo,log) values('metas','$mensaje')"); 

  header("Location: /");
  exit(); 

} 
            // Select all records from the db. Keep inside try block incase there an issue with sql query
            $query = "SELECT tienda,genero,linea,meta_piso,meta_bodega
            FROM metas2
            WHERE tienda = '".$_GET["tienda"]."' AND genero='".$_GET["genero"]."' AND linea='".$_GET["linea"]."'
            ";

            //echo $query;
            /*
            `porcentaje_entallado_cedis`,
            `dias_venta`,
            `porcentaje_entallado_bodega`,
            `porcentaje_entallado_global`
            */
            $tienda = "";
            $genero = "";
            $linea = "";


            if ($result = $mysqli->query($query)) {

                /* fetch associative array */
                while ($row = $result->fetch_assoc()) {
                    if($tienda==""){

                    $tienda = $row["tienda"];
                    $genero = $row["genero"];
                    $linea = $row["linea"];
                    $meta_piso = $row["meta_piso"];
                    $meta_bodega = $row["meta_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">

  <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">Metas <?php echo $_GET["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-7">
          <!-- card-wrapper -->
          <div class="card-wrapper">
            
            <div class="card">
              <!-- Card header -->
              <div class="card-header">
                <h3 class="mb-0"><?php echo $_GET["genero"];?> - <?php echo $_GET["linea"];?></h3>
              </div>
              <!-- Card body -->
              <div class="card-body">
                <form method="post">
                    <input type=hidden name=tienda value="<?php echo $tienda;?>">
                    <input type=hidden name=genero value="<?php echo $genero;?>">
                    <input type=hidden name=linea value="<?php echo $linea;?>">

                  <!-- meta_piso -->
                  <div class="row">
                    <div class="col-md-7">
                      <div class="form-group">
                        <div class="input-group input-group-merge">
                        Meta piso % : <input name="meta_piso" value="<?php echo $meta_piso;?>" class="form-control" size="3" maxlength="3" type="text">
                        </div>
                      </div>
                    </div>
                  </div>

                  <!-- meta_bodega -->
                  <div class="row">
                    <div class="col-md-7">
                      <div class="form-group">
                        <div class="input-group input-group-merge">
                        Meta bodega % : <input name="meta_bodega" value="<?php echo $meta_bodega;?>" class="form-control" size="3" maxlength="3" type="text">
                        </div>
                      </div>
                    </div>
                  </div>

                  <?php echo $mensaje;?>
                  <button class="btn btn-primary" type="submit">Guardar</button>
                </form>
              </div>

            </div>

          </div>
          <!-- card-wrapper -->
        </div>
        
      </div>
      <!-- row -->
    </div>
    <!-- container -->
       <!-- 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>