<?php
$genero = "Dama y Caballero";
$query_gen = "";
if($_GET["gen"]=="dama"){
    $genero = "Dama";
    $query_gen = " and genero='dama'";
} elseif($_GET["gen"]=="caballero") {
    $genero = "Caballero";
    $query_gen = " and genero='caballero'";
}

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
            $query = "# inicial
            select fecha,suc
            ,sum(if(entallado>=porcentaje_entallado_global and tallas>1,1,0)) as articulos_entallados
            ,round( 100* sum(if(entallado>=porcentaje_entallado_global and tallas>1,1,0)) /count(*)) as parte_entallados
            ,SUM(IF(entallado<porcentaje_entallado_global AND tallas>1,1,0)) AS articulos_picos
            ,round( 100* SUM(IF(entallado<porcentaje_entallado_global AND tallas>1,1,0)) /count(*)) as parte_picos
            ,SUM(IF(tallas=1,1,0)) AS articulos_unitalla
            ,round( 100* SUM(IF(tallas=1,1,0)) /count(*)) as parte_unitalla
            ,COUNT(*) as tot_articulos
            ,sum(existencia) as tot_existencia
            from inv_res inner join globales
            where fecha=curdate()".$query_gen." and suc<63
            group by suc";

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

                /* fetch associative array */
                while ($row = $result->fetch_assoc()) {
                    
                    $tabla = $tabla . '<tr>
                    <th scope="row">
                    
                      <div class="media align-items-center">

                      

                        <div class="media-body" onclick="self.location.href=\'sucursal_entallados.php?suc='.$row["suc"].'\'">
                          <span class="tienda mb-0 text-sm">'.$row["suc"].'</span>
                        </div>
                        
                      </div>
                      
                    </th>

                    <td>
                     <div class="d-flex align-items-center">
                       <span class="entallado mr-2">'.$row["articulos_entallados"].' --> '.$row["parte_entallados"].'%</span>
                       <div>
                         <div class="progress">
                           <div class="progress-bar bg-warning" role="progressbar" aria-valuenow="'.$row["parte_entallados"].'" aria-valuemin="0" aria-valuemax="100" style="width: '.$row["parte_entallados"].'%;"></div>
                         </div>
                       </div>
                     </div>
                   </td>

                   <td>
                     <div class="d-flex align-items-center">
                       <span class="picos mr-2">'.$row["articulos_picos"].' --> '.$row["parte_picos"].'%</span>
                       <div>
                         <div class="progress">
                           <div class="progress-bar bg-warning" role="progressbar" aria-valuenow="'.$row["parte_picos"].'" aria-valuemin="0" aria-valuemax="100" style="width: '.$row["parte_picos"].'%;"></div>
                         </div>
                       </div>
                     </div>
                   </td>

                   <td>
                     <div class="d-flex align-items-center">
                       <span class="unitalla mr-2">'.$row["articulos_unitalla"].' --> '.$row["parte_unitalla"].'%</span>
                       <div>
                         <div class="progress">
                           <div class="progress-bar bg-warning" role="progressbar" aria-valuenow="'.$row["parte_unitalla"].'" aria-valuemin="0" aria-valuemax="100" style="width: '.$row["parte_unitalla"].'%;"></div>
                         </div>
                       </div>
                     </div>
                   </td>

                  </tr>';
                }

                /* 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="Bruno Corza">
   <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 -->
   <!-- Argon CSS -->
   <link rel="stylesheet" href="assets/css/argon.css?v=1.1.0" type="text/css">
 </head>
 
 <body>
   <!-- Sidenav -->
     
   <!-- Main content -->
   <div class="main-content" id="panel">
     <!-- Topnav -->
     <nav class="navbar navbar-top navbar-expand navbar-dark bg-primary border-bottom">
       <div class="container-fluid">
         <div class="collapse navbar-collapse" id="navbarSupportedContent">

           <!-- Navbar links -->
           
           <ul class="navbar-nav align-items-center ml-auto ml-md-0">
             <li class="nav-item dropdown">
               <a class="nav-link pr-0" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                 <div class="media align-items-center">
                   <span class="avatar avatar-sm rounded-circle">
                     <img alt="Image placeholder" src="assets/img/theme/team-4.jpg">
                   </span>
                   <div class="media-body ml-2 d-none d-lg-block">
                     <span class="mb-0 text-sm  font-weight-bold">John Snow</span>
                   </div>
                 </div>
               </a>
               <div class="dropdown-menu dropdown-menu-right">
                 <div class="dropdown-header noti-title">
                   <h6 class="text-overflow m-0">Welcome!</h6>
                 </div>
                 <a href="#!" class="dropdown-item">
                   <i class="ni ni-single-02"></i>
                   <span>My profile</span>
                 </a>
                 <a href="#!" class="dropdown-item">
                   <i class="ni ni-settings-gear-65"></i>
                   <span>Settings</span>
                 </a>
                 <a href="#!" class="dropdown-item">
                   <i class="ni ni-calendar-grid-58"></i>
                   <span>Activity</span>
                 </a>
                 <a href="#!" class="dropdown-item">
                   <i class="ni ni-support-16"></i>
                   <span>Support</span>
                 </a>
                 <div class="dropdown-divider"></div>
                 <a href="#!" class="dropdown-item">
                   <i class="ni ni-user-run"></i>
                   <span>Logout</span>
                 </a>
               </div>
             </li>
           </ul>
         </div>
       </div>
     </nav>

     <!-- Page content -->
     <div class="container-fluid mt--6">
       <!-- Table -->

       
       <!-- Dark table -->
       <div class="row">
         <div class="col">
           <div class="card bg-default shadow">
             <div class="card-header bg-transparent border-0">
               <h3 class="text-white mb-0">Entallado por tienda  <?php echo $genero;?></h3>
               <a href="index_ini.php?gen=dama" class="btn btn-sm btn-neutral">Dama</a>
              <a href="index_ini.php?gen=caballero" class="btn btn-sm btn-neutral">Caballero</a>
             </div>
             <div class="table-responsive" data-toggle="list" data-list-values='["tienda", "entallado", "picos", "unitalla"]'>
               <table class="table align-items-center table-dark table-flush">
                 <thead class="thead-dark">
                   <tr>
                     <th scope="col" class="sort" data-sort="tienda">Tienda</th>
                     <th scope="col" class="sort" data-sort="entallado">Entallado</th>
                     <th scope="col" class="sort" data-sort="picos">Picos</th>
                     <th scope="col" class="sort" data-sort="unitalla">Unitalla</th>
                   </tr>
                 </thead>


                 <tbody class="list">

                   <?php echo $tabla;?>

                 </tbody>
               </table>
             </div>
           </div>
         </div>
       </div>

       <div class="col-lg-6 col-5 text-right">
              <a href="index_categoria.php" class="btn btn-sm btn-neutral">Cambiar al modo por linea</a>
              
            </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>
   </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/list.js/dist/list.min.js"></script>
   <!-- Argon JS -->
   <script src="assets/js/argon.js?v=1.1.0"></script>

 </body>
 
 </html>