<?php
session_start();
require_once ("../conexion.php");
require_once('../include/sesiones.php'); 

if (isset($_POST['com_id'])){ $com_id=$_POST['com_id'];}
if (isset($_POST['combo_referencia'])){ $combo_referencia=$_POST['combo_referencia'];}
if (isset($_POST['cantidad'])){ $cantidad=$_POST['cantidad'];}
if ($_POST['combo_iva']>0){ $combo_iva=$_POST['combo_iva']; } else { $combo_iva='0';}
if ($_POST['descuento']>0){ $descuento=$_POST['descuento']; } else { $descuento='0';}
if (isset($_POST['nombre'])){ $nombre=$_POST['nombre'];}
if (isset($_POST['precio_venta'])){ $precio_venta=$_POST['precio_venta'];}
 
//	$art=mysqli_query($con,"SELECT * FROM venta_detalle_combo_temp WHERE combo_id='$com_id' and usuario='$usu_id'") or die(mysqli_error($con));
//	if($rw_pre=mysqli_fetch_array($art))
//	{
// 		$combo_id=$rw_pre['combo_id'];
// 		$can7=mysqli_query($con,"SELECT * FROM venta_detalle_combo_temp WHERE combo_id='$combo_id' and usuario='$usu_id'") or die(mysqli_error($con));  
//        $dato=mysqli_fetch_array($can7);
//        $suma_combo=$dato['combo_cantidad'];
//        $suma_total=$suma_combo+$cantidad;
//        $registros=mysqli_query($con,"UPDATE venta_detalle_combo_temp Set combo_cantidad='$suma_total'
//        Where combo_id='$combo_id'") or die(mysqli_error($con));
//  }
//  else
//  {
		$can0=mysqli_query($con,"SELECT MAX(vdc_id)as vdc_id FROM venta_detalle_combo_temp ") or die(mysqli_error($con)); 
		$dato0=mysqli_fetch_array($can0);
		$vdc_id=$dato0['vdc_id']+1;

   	 	$insert_tmp=mysqli_query($con, "INSERT INTO venta_detalle_combo_temp (vdc_id,combo_id,combo_referencia,combo_cantidad,combo_nombre,combo_iva,combo_desc,combo_precio,usuario,empresa) 
        VALUES ('$vdc_id','$com_id','$combo_referencia','$cantidad','$nombre','$combo_iva','$descuento','$precio_venta','$usu_id','$empresa')");
//  }

$sab=mysqli_query($con,"SELECT * FROM sabores_combos WHERE combo_id='$com_id' and empresa='$empresa'")or die(mysqli_error($con));
if ($res=mysqli_fetch_array($sab))
{
?>
<script type="text/javascript">
function redireccionar()
{
	$('#myModal').modal('hide');
    $('body').removeClass('modal-open');
    $('.modal-backdrop').remove();
  	document.getElementById('id01').style.display='active';
  	$("#id01").trigger("click");
} 
setTimeout ("redireccionar()", 100); 
</script>
<input id="id01" type="hidden" style="visibility: none;" data-toggle="modal" data-target="#mycombo"></input>

<form name="form_combo" id="form_combo">
    <div class="modal fade" id="mycombo" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4 class="modal-title">SABORES</h4>
                </div>
                <div class="modal-body">
                    <div id="resultado_sabor"></div>
                    <div class="">
                        <input type="hidden" name="combo_id" id="combo_id" value="<?php echo $com_id; ?>" >
                        <input type="hidden" name="vdc_id" id="vdc_id" value="<?php echo $vdc_id; ?>" >
                        <?php
                           $sab2=mysqli_query($con,"SELECT * FROM sabores_combos WHERE combo_id='$com_id' and empresa='$empresa'")or die(mysqli_error($con));
                           while ($sab1=mysqli_fetch_array($sab2))
                                {
                                    $sab_id=$sab1['sab_id'];
                        ?>
                            <input type="checkbox" id="<?php echo $sab1['sab_id']; ?>" name="sabor[]" value="<?php echo $sab1['sab_id']; ?>">
                            <label class="lab" for="<?php echo $sab1['sab_id']; ?>"><?php echo $sab1['sab_nombre']; ?></label>
                        <?php } ?>                            
                    </div> <!-- /.form-group -->
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button>
                        <button type="submit" id="btn_combo" value="btn_combo" class="btn btn-success"><i class="icon-ok"></i> Registrar</button>        
                    </div>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal-dialog -->
    </div><!-- /.modal -->
</form> 
<script type="text/javascript">
$("#form_combo").submit(function( event ) {
$('#btn_combo').attr("disabled", true);
    var parametros = $(this).serialize();
    $.ajax({
        type: "POST",
        url: "php_facturar/registrar_sabor_temp.php",
        data: parametros,
        beforeSend: function(objeto){
            $('#resultado_sabor').html('<center><img src="./img/ajax-loader.gif"> CARGANDO...</center>');
        },
        success: function(datos){
            $("#resultado_sabor").html(datos);
            $('#btn_combo').attr("disabled", false);
            document.getElementById("form_combo").reset();
            $('#mycombo').modal('hide');
            $('body').removeClass('modal-open');
            $('.modal-backdrop').remove();
            loadTabla();
            loadtotal();
        }
    });
    vent.preventDefault();
})
</script>

<?php  }  else  {  ?>
<script>
  	loadtotal();
  	loadTabla();
</script>
<?php  } ?>