<?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);

            $tienda = "Cumplimiento de traspasos";
            $titulo = $_GET["linea"]." - ".$_GET["gen"];
            $subtitulo = "";
            if($_GET["dia"]==''){
                $dia = 0;
            } else {
                $dia = $_GET["dia"];
            }
            

            // Select all records from the db. Keep inside try block incase there an issue with sql query
            $query = "# cuantos se hicieron - salidas del lotifica
            SELECT m.fecha,m.origen,m.destino,m.rc,m.talla,i.linea,i.genero
            ,IF(e.fecha IS NULL,'pendiente','') AS pendiente
            ,IF(e.fecha IS NOT NULL,'ok','') AS ok
            ,( IF(e.fecha IS NULL,1,0) ) + ( IF(e.fecha IS NOT NULL,1,0) ) AS total
            ,i.`ultima_entrada` AS ultima_entrada_rc
            FROM movs_buenos m INNER JOIN inv_res i ON i.fecha=m.`fecha`
            AND i.suc=m.`origen` AND i.rc=m.rc
            LEFT JOIN entrasale e ON 
            e.`tienda`=m.origen AND e.rc=m.rc AND e.talla2=m.talla
            AND e.fecha=m.fecha 
            AND e.`tipocompra`='J'
            WHERE m.fecha=DATE_ADD( CURDATE(), INTERVAL -$dia DAY ) AND i.linea='".$_GET["linea"]."' AND i.genero='".$_GET["gen"]."'
            ORDER BY ok DESC,origen,destino,rc 
            ";

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

                /* fetch associative array */
                while ($row = $result->fetch_assoc()) {
                    
                    if($tienda==''){
                      $tienda = $row["sucursal"];
                    }
                    
                    $tabla = $tabla . '<tr>
                    <td>'.$row["fecha"].'</td>
                    <td>'.$row["origen"].'</td>
                    <td>'.$row["destino"].'</td>
                    <td>'.$row["rc"].'</td>
                    <td>'.$row["talla"].'</td>
                    <td>'.$row["pendiente"].'</td>
                    <td>'.$row["ok"].'</td>
                    <td>'.$row["total"].'</td>
                  </tr>';
                }

                // ******** TOTAL **************
                $query = "SELECT 'TOTAL' AS fecha,'' AS origen,'' AS destino,'' AS rc,'' AS talla,i.linea,i.genero
                ,SUM( IF(e.fecha IS NULL,1,0) ) AS pendiente
                ,SUM( IF(e.fecha IS NOT NULL,1,0) ) AS ok
                ,SUM( IF(e.fecha IS NULL,1,0) ) + SUM( IF(e.fecha IS NOT NULL,1,0) ) AS total
                ,MAX( i.`ultima_entrada` ) AS ultima_entrada_rc
                FROM movs_buenos m INNER JOIN inv_res i ON i.fecha=m.`fecha`
                AND i.suc=m.`origen` AND i.rc=m.rc
                LEFT JOIN entrasale e ON 
                e.`tienda`=m.origen AND e.rc=m.rc AND e.talla2=m.talla
                AND e.fecha=m.fecha 
                AND e.`tipocompra`='J'
                WHERE m.fecha=DATE_ADD( CURDATE(), INTERVAL -$dia DAY ) AND i.linea='".$_GET["linea"]."' AND i.genero='".$_GET["gen"]."'";

    //die($query);
                if ($result = $mysqli->query($query)) {
    
                    /* fetch associative array */
                    while ($row = $result->fetch_assoc()) {
                        
                      $totales = $totales . '<tr>
                      <th>'.$row["fecha"].'</th>
                      <th>'.$row["origen"].'</th>
                      <th>'.$row["destino"].'</th>
                      <th>'.$row["rc"].'</th>
                      <th>'.$row["talla"].'</th>
                      <th>'.$row["pendiente"].'</th>
                      <th>'.$row["ok"].'</th>
                      <th>'.$row["total"].'</th>

                  </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="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">
      <!-- Table -->
      <div class="row">
        <div class="col">
          <div class="card">
            <!-- Card header -->
            <div class="card-header">
              <h3 class="mb-0"><?php echo $titulo;?></h3>
              <p class="text-sm mb-0">
              <?php echo $subtitulo;?>
              </p>
            </div>

            <div class="card-header bg-transparent border-0">
               
               <a href="cumplimiento.php?gen=<?php echo $_GET["gen"]?>&linea=<?php echo $_GET["linea"]?>&dia=<?php echo $_GET["dia"]+1;?>" class="btn btn-sm btn-neutral">Día Anterior</a>
              <a href="cumplimiento.php?gen=<?php echo $_GET["gen"]?>&linea=<?php echo $_GET["linea"]?>&dia=<?php echo $_GET["dia"]-1;?>" class="btn btn-sm btn-neutral">Día Siguiente</a>

             </div>

            <div class="table-responsive py-4">
              <table class="table table-flush" id="datatable-basic" data-page-length='100'>
                <thead class="thead-light">
                  <tr>
                    <th>fecha</th>
                    <th>origen</th>
                    <th>destino</th>
                    <th>rc</th>
                    <th>talla</th>
                    <th>pendiente</th>
                    <th>ok</th>
                    <th>total</th>
                    </tr>  
                </thead>

                <thead class="thead-light">
                <?php echo $totales;?>
                </thead>

                <tbody>
                  <?php echo $tabla;?>
                </tbody>
              </table>
            </div>
          </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>
  </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>