<?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 Pendiente'") or die(mysqli_error($con));
$permi = mysqli_fetch_array($per);
$estado_pendiente=$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="85%"><span class="pull-center">DESCRIPCION</span></th>
            <th width="5%"></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='no' ");
        while ($row=mysqli_fetch_array($sql))
        {
            $id=$row["pend_id"];
            $pend_id=$row["pend_id"];
            $pend_cantidad=$row['pend_cantidad'];
            $pend_precio=$row['pend_precio'];
            $pend_nombre=$row['pend_nombre'];
            $precio_total=$pend_precio*$pend_cantidad;
            $sumador_total+=$precio_total;//Sumador
        ?>
        <tr>
             <td>
                <a href="#" data-id="<?php echo $id;?>" class="btn-edit" style="text-decoration:none;">
                    <strong style="color:black;font-size: 12px"><?php echo $pend_nombre; ?></strong>
                    <span style="color: red;font-size: 11px ">
                        <?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))
                            { 
                                if($row1['precio']>0) {echo '</br>';
                                echo '('; echo $row1['cantidad']; echo ') ';}
                                else
                                {
                                echo ' - '; 
                                }
                                echo mb_strtoupper($row1['sabor']);
                            } 
                        ?>
                    </span>
                    <span class="pull-right" style="color: green; font-size: 12PX;">
                        <strong>
                        <?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))
                                {    
                                    $precio_sab=$row2['precio']*$row2['cantidad']*$pend_cantidad; 
                                    $suma+=$precio_sab;
                                } 
                                $total_gen=$suma+$precio_total;
                                echo number_format($total_gen,0);
                        ?>
                        </strong>
                    </span> 
                </a>
            </td>   
            <td> 
                <div class="input-group">
                    <span class="input-group-addon">
                        <a  onclick="restar(<?php echo $id;?>)" ><i class="btn-icon-only icon-minus"></i></a>
                    </span> 
                    <input type="text" class="form-control input-sm" style="width:60px" value="<?php echo $pend_cantidad;?>" readonly>
                    <span class="input-group-addon">
                        <a  onclick="sumar(<?php echo $id;?>)" ><i class="btn-icon-only icon-plus"></i></a>
                    </span> 
                </div>
            </td>
            <td>
                <?php 
                if ($estado_pendiente=='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){
                        loadTabla();
                        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='no'");
    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>
            <a href="#" data-id="<?php echo $pedcom_id;?>" class="btn-edit_combo" style="text-decoration:none;"> 
                <strong style="color:black;font-size: 12px"><?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>
                <span class="pull-right" style="color: green; font-size: 12PX;">
                    <strong><?php echo number_format($precio,0);?></strong>
                </span>
            </a>
            </td>
            <td>
                <div class="input-group">
                    <span class="input-group-addon">
                        <a  onclick="restar_combo(<?php echo $pedcom_id;?>)" ><i class="btn-icon-only icon-minus"></i></a>
                    </span> 
                    <input type="text" class="form-control input-sm" style="width:60px" value="<?php echo $row2['pedcom_cantidad'];?>" readonly>
                    <span class="input-group-addon">
                        <a  onclick="sumar_combo(<?php echo $pedcom_id;?>)"><i class="btn-icon-only icon-plus"></i></a>
                    </span> 
                </div>
            </td>
            <td>
           
            <?php 

                if ($estado_pendiente=='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){
                    loadTabla();
                    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_combo").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');
});


function restar(id)
{
    var campo = 'resta';
    var dataString = 'pend_id=' + id + '&campo=' + campo;
    $.ajax({
        type: "POST",
        url: "php_pedidos/restar_sumar.php",
        data: dataString,
        beforeSend: function(objeto){
             $('#tabla').html('<center><img src="./img/ajax-loader.gif"> CARGANDO...</center>');
        },
        success: function(datos){
        loadTabla();
        loadtotal();
        }
    });
}
function sumar(id)
{
    var campo = 'suma';
    var dataString = 'pend_id=' + id + '&campo=' + campo;
    $.ajax({
        type: "POST",
        url: "php_pedidos/restar_sumar.php",
        data: dataString,
        beforeSend: function(objeto){
             $('#tabla').html('<center><img src="./img/ajax-loader.gif"> CARGANDO...</center>');
        },
        success: function(datos){
        loadTabla();
        loadtotal();
        }
    });
}


function restar_combo(id)
{
    var campo = 'resta';
    var dataString = 'pedcom_id=' + id + '&campo=' + campo;
    $.ajax({
        type: "POST",
        url: "php_pedidos/restar_sumar_combo.php",
        data: dataString,
        beforeSend: function(objeto){
             $('#tabla').html('<center><img src="./img/ajax-loader.gif"> CARGANDO...</center>');
        },
        success: function(datos){
        loadTabla();
        loadtotal();
        }
    });
}
function sumar_combo(id)
{
    var campo = 'suma';
    var dataString = 'pedcom_id=' + id + '&campo=' + campo;
    $.ajax({
        type: "POST",
        url: "php_pedidos/restar_sumar_combo.php",
        data: dataString,
        beforeSend: function(objeto){
             $('#tabla').html('<center><img src="./img/ajax-loader.gif"> CARGANDO...</center>');
        },
        success: function(datos){
        loadTabla();
        loadtotal();
        }
    });
}

</script>