<?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
            $query = "SELECT fecha,suc,sucursal
            ,FORMAT(SUM(IF(entallado>=porcentaje_entallado_global AND tallas>1,1,0)),0) AS articulos_entallados
            ,CONCAT(ROUND( 100* SUM(IF(entallado>=porcentaje_entallado_global AND tallas>1,1,0)) /COUNT(*)),'%') AS parte_entallados
            ,FORMAT(SUM(IF(entallado<porcentaje_entallado_global AND tallas>1,1,0)),0) AS articulos_picos
            ,CONCAT(ROUND( 100* SUM(IF(entallado<porcentaje_entallado_global AND tallas>1,1,0)) /COUNT(*)),'%') AS parte_picos
            ,FORMAT(SUM(IF(tallas=1,1,0)),0) AS articulos_unitalla
            ,CONCAT(ROUND( 100* SUM(IF(tallas=1,1,0)) /COUNT(*)),'%') AS parte_unitalla
            ,FORMAT(COUNT(*),0) AS tot_articulos
            ,FORMAT(SUM(existencia),0) AS tot_existencia
            from inv_res inner join globales
            where fecha=curdate() and suc='".$_GET["suc"]."'
            group by suc";

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

                /* fetch associative array */
                while ($row = $result->fetch_assoc()) {
                    
                    $sucursal = $row["sucursal"];
                    $suc = $row["suc"];
                    $articulos_entallados = $row["articulos_entallados"];
                    $parte_entallados = $row["parte_entallados"];
                    $articulos_picos = $row["articulos_picos"];
                    $parte_picos = $row["parte_picos"];
                    $articulos_unitalla = $row["articulos_unitalla"];
                    $parte_unitalla = $row["parte_unitalla"];
                    $tot_articulos = $row["tot_articulos"];
                    $tot_existencia = $row["tot_existencia"];
                }

                /* 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">
  <!-- 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 -->

    <!-- 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 $sucursal;?></h6>
            </div>

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



          </div>
          <!-- Card stats -->
          <div class="row">

            <div class="col-xl-3 col-md-6">
              <div class="card card-stats" onclick="self.location.href='sucursal_detalle.php?suc=<?php echo $suc;?>&art=ent'">
                <!-- Card body -->
                <div class="card-body">
                  <div class="row">
                    <div class="col">
                      <h5 class="card-title text-uppercase text-muted mb-0">Artículos entallados</h5>
                      <span class="h2 font-weight-bold mb-0"><?php echo $articulos_entallados;?></span>
                    </div>
                    <div class="col-auto">
                      <div class="icon icon-shape bg-gradient-red text-white rounded-circle shadow">
                        <i class="ni ni-active-40"></i>
                      </div>
                    </div>
                  </div>
                  <p class="mt-3 mb-0 text-sm">
                    <span class="h2 font-weight-bold mb-0"><font color=red><?php echo $parte_entallados;?></font></span>
                    <span class="text-nowrap">del total de artículos <?php echo $tot_articulos;?></span>
                  </p>
                </div>
              </div>
            </div>

            <div class="col-xl-3 col-md-6">
              <div class="card card-stats" onclick="self.location.href='sucursal_detalle.php?suc=<?php echo $suc;?>&art=pic'">
                <!-- Card body -->
                <div class="card-body">
                  <div class="row">
                    <div class="col">
                      <h5 class="card-title text-uppercase text-muted mb-0">Artículos picos</h5>
                      <span class="h2 font-weight-bold mb-0"><?php echo $articulos_picos;?></span>
                    </div>
                    <div class="col-auto">
                      <div class="icon icon-shape bg-gradient-orange text-white rounded-circle shadow">
                        <i class="ni ni-chart-pie-35"></i>
                      </div>
                    </div>
                  </div>
                  <p class="mt-3 mb-0 text-sm">
                    <span class="h2 font-weight-bold mb-0"><font color=orange><?php echo $parte_picos;?></font></span>
                    <span class="text-nowrap">del total de artículos <?php echo $tot_articulos;?></span>
                  </p>
                </div>
              </div>
            </div>
            
            <div class="col-xl-3 col-md-6">
              <div class="card card-stats" onclick="self.location.href='sucursal_detalle.php?suc=<?php echo $suc;?>&art=uni'">
                <!-- Card body -->
                <div class="card-body">
                  <div class="row">
                    <div class="col">
                      <h5 class="card-title text-uppercase text-muted mb-0">Artículos unitalla</h5>
                      <span class="h2 font-weight-bold mb-0"><?php echo $articulos_unitalla;?></span>
                    </div>
                    <div class="col-auto">
                      <div class="icon icon-shape bg-gradient-info text-white rounded-circle shadow">
                        <i class="ni ni-chart-bar-32"></i>
                      </div>
                    </div>
                  </div>
                  <p class="mt-3 mb-0 text-sm">
                    <span class="h2 font-weight-bold mb-0"><font color=blue><?php echo $parte_unitalla;?></font></span>
                    <span class="text-nowrap">del total de artículos <?php echo $tot_articulos;?></span>
                  </p>
                </div>
              </div>
            </div>

            
          </div>

          <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">Existencia total <?php echo $tot_existencia;?> piezas</h6>
              
            </div>
 
            <div class="col-auto">
            <ol class="breadcrumb breadcrumb-links breadcrumb-dark">
                  <li class="breadcrumb-item"><a onclick="self.history.go(-1); return false;" href="#"><i class="fas fa-home"></i></a></li>
            </ol>

            </div>

          </div>

        </div>
      </div>
    </div>


    <div class="col-lg-6 col-5 text-right">
              <a href="categoria.php?suc=<?php echo $suc;?>" class="btn btn-sm btn-neutral">Cambiar al modo por categoría</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>
  <!-- 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>