<?php
session_start();
require_once('../conexion.php');
require_once('../include/sesiones.php');
$ubi_id=$_GET['ubi_id'];
$registros=mysqli_query($con,"SELECT * from pedido where pen_estado='Procesando' and sucursal='$sucursal' limit 50") or die(mysqli_error($con));
while ($reg=mysqli_fetch_array($registros))
    {
        $pen_id=$reg['pen_id'];
        $pen_comentario=$reg['pen_comentario'];
        $vendedor=$reg['usuario'];
        $mesa_id=$reg['mesa_id'];

        $us=mysqli_query($con,"SELECT * from usuario where  usu_id='$vendedor'");
        $us=mysqli_fetch_array($us);
        $vendedor_nombre=$us['usu_nombre'];

        $ubi=mysqli_query($con,"SELECT * from pedido_detalle pd, articulos a where pd.pedido_pen_id='$pen_id'  and pd.pend_estado='facturado' and pd.pend_cocina='no' and pd.pend_archivado='si' and a.ubicacion_ubi_id='$ubi_id' and pd.pend_art_id=a.art_id ") or die(mysqli_error($con));
        $row=mysqli_fetch_array($ubi);

        $ubi1=mysqli_query($con,"SELECT * from pedido_detalle_combo pdc, combos c where pdc.pedido_pen_id='$pen_id'  and pdc.pend_estado='facturado' and pdc.pend_cocina='no' and pdc.pend_archivado='si' and pdc.combo_id=c.com_id and c.com_ubicacion='$ubi_id' ") or die(mysqli_error($con));
        $row1=mysqli_fetch_array($ubi1);
        
        if ($row>0 || $row1>0) {
           
?>
        <div class="plan-container">
            <div class="plan stacked">
                <div class="plan-header">
                    <div class="plan-title">  
                    <?php
                        $can7=mysqli_query($con,"SELECT * FROM mesas WHERE mes_id='$mesa_id'") or die(mysqli_error($con));  
                        $dat=mysqli_fetch_array($can7);
                        $mes_nombre=$dat['mes_nombre']; 
                        $zon_id=$dat["zon_id"];
                        $sql=mysqli_query($con,"SELECT * FROM zonas where zon_id='$zon_id' and empresa='$empresa' ");
                        $zon=mysqli_fetch_array($sql);
                        echo $zon_nombre=$zon["zon_nombre"];
                        echo ' - '; echo  $mes_nombre;
                        echo '</br>'; 
                        echo '('; echo $vendedor_nombre;echo')';
                    ?> 
                    </div> <!-- /plan-title -->
                </div> <!-- /plan-header -->          
                <div class="plan-features" style="background-color: #fff">
                    <ul style="color: black;">
                        <?php
                          
                            $registros3=mysqli_query($con,"SELECT * from pedido_detalle pd, articulos a where pd.pedido_pen_id='$pen_id'  and pd.pend_estado='facturado' and pd.pend_cocina='no' and pd.pend_archivado='si' and a.ubicacion_ubi_id='$ubi_id' and pd.pend_art_id=a.art_id ") or die(mysqli_error($con));
                            while ($ped=mysqli_fetch_array($registros3))
                            {
                                $pend_art_id=$ped['pend_art_id'];
                                $pend_id=$ped["pend_id"];
                                $pend_observaciones=$ped['pend_observaciones'];
                        ?>
                        <li style="padding: 5px"><strong><?php echo $ped['pend_cantidad']; ?></strong> 
                            <?php 

                          

                                $can7=mysqli_query($con,"SELECT * FROM articulos WHERE art_id='$pend_art_id'") or die(mysqli_error($con));  
                                $dat=mysqli_fetch_array($can7);
                                echo $art_nombre=$dat['art_nombre'];
                                $art_id=$dat['art_id']; 
                               
                                
                                $sab_pro=mysqli_query($con,"SELECT * FROM sabores_productos WHERE empresa='$empresa' and sab_estado='Activo' and articulo_art_id='$pend_art_id' ") or die (mysqli_error($con)); 
                                while ($sab=mysqli_fetch_array($sab_pro))
                                { 
                                $precio=$sab['sab_precio'];

                                } if ($precio==0) 

                                {  echo '(';
                                    $art=mysqli_query($con,"SELECT * FROM sabores_pedido_temp WHERE pend_id='$pend_id'")or die(mysqli_error($con));
                                    while ($row1=mysqli_fetch_array($art))
                                    {
                                        echo '-';
                                        echo $row1['sabor'];
                                        echo '-'; 
                                    }
                                    echo ')'; 
                                    echo mb_strtoupper($pend_observaciones);
                                } else 

                                {
                                    $art=mysqli_query($con,"SELECT * FROM sabores_pedido_temp WHERE pend_id='$pend_id'")or die(mysqli_error($con));
                                    while ($row1=mysqli_fetch_array($art))
                                    { ?><br><?php
                                        echo '-';
                                        echo $row1['cantidad'];
                                        echo $row1['sabor'];
                                        echo ' '; 
                                    }
                                    echo mb_strtoupper($pend_observaciones);
                                } 
                              
                            ?>
                        </li>
                        <?php } ?>

                        <?php
                            $registros3=mysqli_query($con,"SELECT * from pedido_detalle_combo pdc, combos c where pdc.pedido_pen_id='$pen_id'  and pdc.pend_estado='facturado' and pdc.pend_cocina='no' and pdc.pend_archivado='si' and pdc.combo_id=c.com_id and c.com_ubicacion='$ubi_id' ") or die(mysqli_error($con));
                            while ($ped=mysqli_fetch_array($registros3))
                            {
                                $pedcom_id=$ped['pedcom_id'];
                                $pedcom_observaciones=$ped["pedcom_observaciones"];
                        ?>
                        <li style="padding: 5px"><strong><?php echo $ped['pedcom_cantidad']; ?></strong> 
                            <?php echo $ped['pedcom_nombre']; 
                            echo ' (';
                            $art=mysqli_query($con,"SELECT * FROM sabores_pedido_combos_temp WHERE pedcom_id='$pedcom_id'")or die(mysqli_error($con));
                            while ($row1=mysqli_fetch_array($art))
                                {
                                    echo '-'; 
                                    echo $row1['sabor'];
                                    echo '-'; 
                                }
                            echo ')';
                            echo mb_strtoupper($pedcom_observaciones ?? '');
                            ?>
                        </li>
                        <?php } ?>
                    </ul>
                    <hr>
                    <center><p style="padding:5px; color:#FF0000"><?php echo $pen_comentario;?></p></center>
                </div> <!-- /plan-features -->
                <div class="plan-actions">        
                    <a onclick="eliminar('<?php echo $pen_id;?>')" class="btn btn-info">Listo</a>       
                </div> <!-- /plan-actions -->
            </div> <!-- /plan -->
        </div> <!-- /plan-container -->
<?php  } 
    } ?>

<script>
function eliminar (pen_id)
{
    if (confirm("Confirmar Despacho del Pedido")){ 
       var ubi_id='<?php echo $ubi_id; ?>';  
    $.ajax({
        type: "GET",
        url: "php_pedidos/espachar.php",
        data: "pen_id="+pen_id+"&ubi_id="+ubi_id,
        beforeSend: function(objeto){
         $('#recargado').html('<center><img src="./img/ajax-loader.gif"> CARGANDO...</center>');
        },
        });
    }
} 
</script>