<?php
session_start();
require_once ("../conexion.php");
require_once('../include/sesiones.php'); 
$per=mysqli_query($con,"SELECT * FROM modulo_permiso where usuario='$usu_id' and mod_id='31' and modp_nombre='Eliminar Pedido Confirmado'") or die(mysqli_error($con));
$permi = mysqli_fetch_array($per);
$estado_confirmado=$permi['estado'];


$pen_id=$_GET['pen_id'];
$com=mysqli_query($con, "SELECT * from pedido where pen_id='$pen_id'");
$come=mysqli_fetch_array($com);
$pen_estado=$come["pen_estado"];

?>
<div class="table-responsive">
    <table class="table table-striped table-bordered" style="font-size: 9pt;">
        <tr>
            <th width="7%">CANT.</th>
            <th><span class="pull-center">DESCRIPCION</span></th>
            <th width="10%"><span class="pull-right">TOTAL</span></th>
            <th width="5%"></th>
        </tr>
        <?php
        $sumador_total=0;
        $sql=mysqli_query($con, "SELECT * from  pedido_detalle where pedido_pen_id='$pen_id' and pend_estado='facturado' and pend_archivado='si'");
        while ($row=mysqli_fetch_array($sql))
        {
            $id=$row["pend_id"];
            $pend_id=$row["pend_id"];
            $pend_art_id=$row["pend_art_id"];
            $pend_cantidad=$row['pend_cantidad'];
            $pend_precio=$row['pend_precio'];
            $descuento=$row['pend_descuento'];
            $pend_nombre=$row['pend_nombre'];
            $pend_tipo=$row['pend_tipo'];
            $precio_total=$pend_precio*$pend_cantidad;
            $sumador_total+=$precio_total;//Sumador
            
            $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) 
            {
        ?>
        <tr>
            <td><?php echo $pend_cantidad;?></td>
            <td>
                <a href="#" data-id="<?php echo $id;?>" class="btn-edit" style="text-decoration:none">
                <strong><?php echo $pend_nombre; ?></strong>
                <span style="color: red;">
                <?php   
                    $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 mb_strtoupper($row1['sabor']);
                        echo ' -'; 
                    } 
                ?>
                </span></a>
            </td>
            <td><span class="pull-right"><strong><?php echo number_format($precio_total,0);?></strong></span></td>
            <td>
                <?php 
                if ($estado_confirmado=='Activo') { ?>
                    <a href="#" id="del-<?php echo $row["pend_id"];?>" class="btn btn-xs btn-danger"> <i class="btn-icon-only icon-remove"></i></a>
                <?php }  ?>
                <script>
                $("#del-"+<?php echo $row["pend_id"];?>).click(function(e){
                    e.preventDefault();
                    p = confirm("Estas seguro?");
                    if(p){
                      $.get("./php_pedidos/eliminar_articulo.php","pend_id="+<?php echo $row["pend_id"];?>,function(data){
                        loadTablaConfirmado();
                        loadtotal();
                      });
                    }
                  });
                </script>
            </td>
        </tr>

        <?php  }  else  {  ?>
        <tr>
            <td><?php echo $pend_cantidad; ?>
                <span style="color: red;">
                    <?php   
                        $art3=mysqli_query($con,"SELECT * FROM sabores_pedido_temp WHERE pend_id='$pend_id' order by sabor asc ")or die(mysqli_error($con));
                        while ($row3=mysqli_fetch_array($art3))
                        { 
                            echo ' ';?> </br> <?php
                            echo $row3['cantidad'];
                        } 
                    ?>
                </span>
            </td>
            <td>
                <a href="#" data-id="<?php echo $id;?>" class="btn-edit" style="text-decoration:none">
                <strong><?php echo $pend_nombre; ?></strong>
                <span style="color: red;">
                    <?php   
                        $art=mysqli_query($con,"SELECT * FROM sabores_pedido_temp WHERE pend_id='$pend_id' order by sabor asc ")or die(mysqli_error($con));
                        while ($row1=mysqli_fetch_array($art))
                        { 
                            echo ' ';?> </br> <?php
                            echo mb_strtoupper($row1['sabor']);
                        } 
                    ?>
                </span></a>
            </td>
            
            <td><span class="pull-right"><strong><?php echo number_format($precio_total,0);?></strong></span>
                <?php
                $suma=0;
                $total_gen=0;
                $art1=mysqli_query($con,"SELECT * FROM sabores_pedido_temp WHERE pend_id='$pend_id' order by sabor asc ")or die(mysqli_error($con));
                    while ($row2=mysqli_fetch_array($art1))
                    {  ?>
                        <span class="pull-right"> 
                        <?php echo number_format($precio_sab=$row2['precio']*$row2['cantidad']); ?>
                        </span>
                        <?php
                        $suma+=$precio_sab;
                        $total_gen=$suma+$precio_total;
                    } 
                    if ($total_gen<>0) 
                    {  ?>
                    </br>
                        <span class="pull-right" style="color:red;">
                        <?php if ($pend_tipo=='producto') 
                            {
                                echo number_format($total_gen);
                            }   
                        ?>
                        </span>
                <?php  }  ?>
            </td>
            <td>
                <?php 
               
                if ($estado_confirmado=='Activo') { ?>
                    <a href="#" id="del-<?php echo $row["pend_id"];?>" class="btn btn-xs btn-danger"> <i class="btn-icon-only icon-remove"></i></a>
                <script>
                     <?php }  ?> 
                $("#del-"+<?php echo $row["pend_id"];?>).click(function(e){
                    e.preventDefault();
                    p = confirm("Estas seguro?");
                    if(p){
                      $.get("./php_pedidos/eliminar_articulo.php","pend_id="+<?php echo $row["pend_id"];?>,function(data){
                        loadTablaConfirmado();
                        loadtotal();
                      });
                    }
                  });
                </script>
            </td>
        </tr>   
<?php   }  }


    $sql2=mysqli_query($con, "SELECT * from pedido_detalle_combo where pedido_pen_id='$pen_id' and pend_estado='facturado' and pend_archivado='si'");
    while ($row2=mysqli_fetch_array($sql2))
    {
        $pedcom_id=$row2['pedcom_id'];
        $combo_id=$row2['combo_id'];
        $precio=($row2['pedcom_cantidad']*$row2['pedcom_precio']);
    ?>
        <tr>
             <td><?php echo $row2['pedcom_cantidad'];?></td>
            <td><a href="#" data-id="<?php echo $pedcom_id;?>" class="btn-edit_com" style="text-decoration:none">
                <strong><?php  echo $row2['pedcom_nombre']; ?>
                </strong>
                <span style="color: red;">
                <?php $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 mb_strtoupper($row1['sabor']);
                        echo ' '; 
                    }
                ?>
                </span>
            </td>
           
            <td><span class="pull-right"><strong><?php echo number_format($precio,0);?></strong></span></td>
            <td>
            <?php 
            if ($estado_confirmado=='Activo') { ?>
                <a href="#" id="delc-<?php echo $pedcom_id;?>" class="btn btn-xs btn-danger"> <i class="btn-icon-only icon-remove"></i></a>
            <?php }  ?> 
            <script>
            $("#delc-"+<?php echo $pedcom_id;?>).click(function(e){
                e.preventDefault();
                p = confirm("Estas seguro?");
                if(p){
                  $.get("./php_pedidos/eliminar_combo.php","pedcom_id="+<?php echo $pedcom_id;?>,function(data){
                    loadTablaConfirmado();
                    loadtotal();
                  });
                }
            });
            </script>
            </td>
        </tr> 
<?php } ?>
    </table>
</div>

<script>
$(".btn-edit").click(function(){
  id = $(this).data("id");
  $.get("./php_pedidos/form_modificar_articulo.php","id="+id,function(data){
    $("#form-edit").html(data);
  });
  $('#editModal').modal('show');
});

$(".btn-edit_com").click(function(){
  id = $(this).data("id");
  $.get("./php_pedidos/form_modificar_combo.php","id="+id,function(data){
    $("#form-edit_combo").html(data);
  });
  $('#editModalcombo').modal('show');
});
</script>