<?
$r = "../";
require_once($r."inc/funciones.php");
validNotSession($r);
$sql="";
foreach($_GET as $key => $value){
    $param_name = 'idcliente';
    if(substr($key, 0, strlen($param_name)) == $param_name) {
        if($value!=""){$sql .=  "z_pedidos.idcliente=".$value.' AND ';}
    }
    $param_name = 'iduser';
    if(substr($key, 0, strlen($param_name)) == $param_name) {
        if($value!=""){$sql .=  "z_pedidos.iduser=".$value.' AND ';}
        
    }
    $param_name = 'idrutero';
    if(substr($key, 0, strlen($param_name)) == $param_name) {
        if($value!=""){$sql .=  "z_pedidos.idrutero=".$value.' AND ';}
        
    }
    $param_name = 'estado';
    if(substr($key, 0, strlen($param_name)) == $param_name) {
        if($value!=""){$sql .=  "z_pedidos.estado=".$value.' AND ';}
        
    }
    $param_name = 'idproducto';
    if(substr($key, 0, strlen($param_name)) == $param_name) {
       if($value!=""){$sql .=  "z_pedido.idproducto=".$value.' AND ';}
    }
    $param_name = 'bono';
    if(substr($key, 0, strlen($param_name)) == $param_name) {
       if($value!=""){
        if($value=="0"){$sql .=  "z_pedido.bono!=".$value.' AND ';}
        else{$sql .=  "z_pedido.bono=".$value.' AND ';}
       }}
    if($key=="fecha1" or $key=='fecha2'){
        if($key=="fecha1"){
            if($value!=""){
            $fecha1=$value;
            $fecha2=$value;
            $fechai= "z_pedidos.fecha BETWEEN '$fecha1 00:00:00' AND '$fecha2 23:59:59' AND ";
            
            }
        }
        if($key=="fecha2"){
            if($value!=""){
            $fecha2=$value;
            $fechaf= "z_pedidos.fecha BETWEEN '$fecha1 00:00:00' AND '$fecha2 23:59:59' AND ";
            }
        }
    }
   
}
if($fechaf==""){$sql.=$fechai;}else{$sql.=$fechaf;}
$mysqli = connectDB($r);
$row1 = $mysqli->query("SELECT * FROM z_pedidos INNER JOIN z_pedido WHERE $sql z_pedidos.codigopedido = z_pedido.codigopedido ");

?>
<!DOCTYPE html>
<html lang="en">
  <?php include $r."inc/head.php"; ?>
<body class="fixed-nav sidenav-toggled sticky-footer bg-dark" id="page-top">
  <?php include $r."inc/nav.php"; ?>
  
  <div class="content-wrapper">
    <div class="container-fluid">
      <div class="card mb-3">
        <div class="h3 card-header">
          <i class="fa fa-table"></i> RESULTADO CONSULTA</div>
        <div class="card-body">
          <div class="table-responsive">
            <table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
              <thead>
                <tr>
                  <th>Cliente</th>
                  <th>Pedido</th>
                  <th>Estado</th>
                  <th>Producto</th>
                  <th>Cantidad</th>
                  <th>Bono</th>
                </tr>
              </thead>
              <tfoot>
                <tr>
                  <th>Cliente</th>
                  <th>Pedido</th>
                  <th>Estado</th>
                  <th>Producto</th>
                  <th>Cantidad</th>
                  <th>Bono</th>
                </tr>
              </tfoot>
              <tbody>
                    <?php
                    while($row2 = $row1->fetch_assoc()) {?>
                      <tr>
                        <td><?php $cliente=sclien($row2['idcliente']); echo $cliente['nombre']?></td>
                        <td><?php echo $row2['codigopedido'];?></td>
                        <td><?php   if($row2["pago"]!=1){
                        if($row2["estado"]==0){echo 'Por aprovar';}
                        if($row2["estado"]==1){echo 'Por entregar';}
                        if($row2["estado"]==2){echo 'En ruta';}
                        if($row2["estado"]==3){echo 'finalizado';}
                        if($row2["estado"]==4){echo 'Cancelado';}
                        if($row2["estado"]==5){echo 'Devuelto';}}else{echo '<spam class="text-warning">Bono</spam>';}?></td>
                        <td><?php $producto=sprod($row2['idproducto']); echo $producto['producto'];?></td>
                        <td><?php echo $row2['cantidad']; $suma=$suma+$row2['cantidad'];?></td>
                        <td><?php echo $row2['bono']; $suma2=$suma2+$row2['bono'];?></td>
                      </tr>
                    <? } ?>
                    
                <tr>
                  <th></th>
                  <th></th>
                  <th></th>
                  <th>Total Unds</th>
                  <th><? echo $suma ?></th>
                  <th><? echo $suma2 ?></th>
                </tr>
              <tbody>
            </table>
          </div>
        </div><? echo $subtotal;?>
      </div>
      <input type="button" class="btn btn-primary btn-lg float-left" value="Atras" name="Back2" onclick="history.back()" />
    </div>
  </div>
  <?php include $r."inc/foot.php"; ?>
 
<script>
    function popitup(url){
	    newwindow=window.open(url,'name','height=600,width=320'); 
	    if (window.focus) {newwindow.print();}
	    return false; 
    }
</script> 
<script>
$('#dataTable').DataTable( {
        "order": [[ 3, "desc" ]],
        "lengthMenu": [[100, 500, 1000, -1], [100, 500, 1000, "All"]]
    } );
  </script>
</body>
</html>   