<?php 
session_start();
require_once ("../conexion.php");
require_once('../include/sesiones.php'); 
$est=mysqli_query($con,"SELECT * FROM modulo_permiso WHERE mod_id='31' and usuario='$usu_id' ") or die(mysqli_error($con));
$est=mysqli_fetch_array($est);
if($est['estado']=='Activo')
{
    $readonly='';
}
else
{
    $readonly='readonly'; 
}

$pend_id=$_GET['id']; 
?>
<input type="hidden" name="pend_id" id="pend_id" value="<?php echo $pend_id; ?>">
<table class="table-responsive col-md-12" >
    <tr>
        <th width="59%"><strong>Sabor</strong></th>
        <th width="20%"><strong>Cantidad</strong></th>
        <th width="30%"><strong>Precio</strong></th>
        <th width="1%"> 
           
            <button type="button" class="btn btn-sab btn-success btn-xs" data-toggle="modal" ><i class="btn-icon-only icon-plus"></i></i></button>
        </th>
    </tr>

<?php 
$can3=mysqli_query($con,"SELECT * FROM sabores_pedido_temp WHERE pend_id='$pend_id' order by sabor asc ") or die(mysqli_error($con));  
while($dat3=mysqli_fetch_array($can3))
{
    $sab_id=$dat3['sab_id'];
    $sabor=$dat3['sabor'];
    $cantidad=$dat3['cantidad'];
    $precio=$dat3['precio']; 
?>
    <tr>
        <td><input type="text" class="form-control input-sm" name="sabor" id="sabor" value="<?php echo $sabor; ?>" disabled></td>
        <td> <?php if($precio>0){?>
            <input type="number" class="form-control input-sm" name="cant_sab" min="1" onchange="update_cantidad(<?php echo $sab_id;?>,this.value);" value="<?php echo $cantidad; ?>"> <?php } ?>
        </td>
        <td>
            <?php if($precio>0){?>
            <input type="number" class="form-control input-sm" name="precio_sab" min="0" onchange="update_precio(<?php echo $sab_id;?>,this.value);" value="<?php echo $precio; ?>" <?php echo $readonly; ?>>
            <?php } ?>
        </td>
            <input type="hidden" name="sab_id" id="sab_id" value="<?php echo $sab_id; ?>" >
        <td>
            <a href="#" id="del-<?php echo $sab_id ?>" class="btn btn-xs btn-danger"> <i class="btn-icon-only icon-remove"></i></a>
        </td>
        <script>
            $("#del-"+<?php echo $sab_id ?>).click(function(e){
            e.preventDefault();
            p = confirm("Desea eliminar el sabor?");
            if(p){
              $.get("./php_pedidos/eliminar_sabor.php","sab_id="+<?php echo $sab_id ?>,function(data){
                loadTablasabor();
                loadTabla();
                loadtotal();
                loadTablaConfirmado();
                });
            }
          });
        </script>
    </tr>
     <?php  }  ?> 
</table>

<script>
$(".btn-sab").click(function(){
    var pend_id=$("#pend_id").val();
    $.get("./php_pedidos/form_sabor_nuevo.php?pend_id="+pend_id,function(data){
    $("#sabores").html(data);
  });
  $('#modal_sabor_nuevo').modal('show');
});


function update_cantidad(campo,valor){
var parametros = {"action":"update","campo":campo,"valor":valor}; 
$.ajax({
    type: "POST",
    url: "./php_pedidos/editar_cantidad_sabores.php",
    data: parametros,
     beforeSend: function(objeto){
      $("#resultados").html("Mensaje: Cargando...");
      },
    success: function(datos){
    $("#resultados").html(datos);
    }
      });
    loadTabla();
    loadtotal();
    loadTablaConfirmado();

}

function update_precio(campo,valor){
var parametros = {"action":"update","campo":campo,"valor":valor}; 
$.ajax({
    type: "POST",
    url: "./php_pedidos/editar_precio_sabores.php",
    data: parametros,
     beforeSend: function(objeto){
      $("#resultados").html("Mensaje: Cargando...");
      },
    success: function(datos){
    $("#resultados").html(datos);
    }
      });
    loadTabla();
    loadtotal();
    loadTablaConfirmado();
}
</script>