<?php
session_start();
require_once('conexion.php');
if (isset($_SESSION['usu_nombre']))
{
    require_once('include/sesiones.php');
    $regis=mysqli_query($con,"SELECT * from permiso where modulo_mod_id='6' and usuario_usu_id='$usu_id'") or die(mysqli_error($con));
    $reg=mysqli_fetch_array($regis);
        if($reg['per_visualizar']=="Activo")
        {
?>
<!DOCTYPE html>
<html lang="en">
<head>
<?php
    include ('head.php');
?>
</head>
<body>
<?php
$inicio="";
$M_inventario="active";
$M_contactos="";
$M_compras="";
$M_ventas="";
$M_pedidos="";
$M_pos="";
$R_reportes="";

$m_familias="";
$m_articulos="";
$m_combos="";
$m_servicios="";
$m_productos="active";
$m_tarifas="";
$m_bodegas="";
$m_traslados="";
$m_ajustes="";
$m_etiquetas="";
$m_ubicacion="";
$m_marca="";
include ('menu.php');
?> 
<div class="main">
    <div class="col-md-2">
        <?php include ('include/menu_inventario.php');?>           
    </div> <!-- /span6 -->      
    <div class="col-md-10"> 
        <div class="widget stacked">
            <div class="widget-header">
                <i class="icon-gift "></i>
                <h3>PRODUCTOS</h3>
            </div> 
           <?php
                $art_id=$_GET['art_id'];
                $can=mysqli_query($con,"SELECT * FROM articulos where art_id='$art_id'") or die(mysqli_error($con)); 
                $dato=mysqli_fetch_array($can);
                $com_referencia=$dato['art_referencia'];
                $com_barras=$dato['art_barras'];
                $com_nombre=$dato['art_nombre'];
                $familias_fam_id =$dato['familias_fam_id'];
                $ubicacion_ubi_id =$dato['ubicacion_ubi_id'];
                $art_precio_costo=$dato['art_precio_costo'];
                $art_iva=$dato['art_iva'];
                $art_pvp=$dato['art_pvp'];
            ?> 
            <form name="form_combo_mod" id="form_combo_mod" action="" method="post" enctype="multipart/form-data" >
                <div class="widget-content">
                    <div class="col-md-12 ">
                        <div id="div_produccion"></div>
                        <div class="col-md-4">
                            <div class="form-group">
                                <label>Codigo</label>
                                <input type="hidden" name="art_id" id="art_id" class="form-control input-sm"  autocomplete="off" value="<?php echo  $art_id;?>" >
                                    <div class="input-group">
                                        <input type="text"  name="art_referencia" id="art_referencia" class="form-control input-sm"  autocomplete="off" value="<?php echo  $com_referencia;?>" onchange="" >
                                        <span class="input-group-addon"><div id="Info_referencia"></div> </span>
                                    </div>
                            </div> <!-- /.form-group -->
                            <div class="form-group">
                                <label>Codigo de barras</label>
                                    <div class="input-group">
                                        <input type="text"  name="art_barras" id="art_barras" class="form-control input-sm" autocomplete="off" value="<?php echo  $com_barras;?>"  >
                                        <span class="input-group-addon"><div id="Info_barras"></div> </span>
                                    </div> <!-- /.form-group -->
                            </div> <!-- /.form-group -->
                            <label>Categorias</label>
                            <section id="intro" >
                                <select name="art_familia" id="art_familia" class="form-control input-sm" required>
                                     <?php
                                    $datos = mysqli_query($con,"SELECT * FROM familias WHERE fam_id='$familias_fam_id' ") ;
                                    while( $fila = mysqli_fetch_array( $datos ) )
                                    {
                                    echo' <option value="'.$fila['fam_id'].'">'.$fila['fam_nombre'].'</option>';
                                    }
                                    $datos = mysqli_query($con,"SELECT * FROM familias WHERE empresa='$empresa' and fam_id<>'$familias_fam_id' order by fam_nombre asc") ;
                                    while( $fila = mysqli_fetch_array( $datos ) )
                                    {
                                    echo' <option value="'.$fila['fam_id'].'">'.$fila['fam_nombre'].'</option>';
                                    }
                                    ?>
                                </select>
                            </section>
                            <div class="form-group">
                                <label>Nombre</label>
                                <textarea rows="4" name="nombre" class="form-control input-sm" required><?php echo  $com_nombre;?></textarea>
                            </div> <!-- /.form-group -->
                        </div>

                        <div class="col-md-4">
                            <label>Ubicacion</label>
                            <section id="intro" >
                            <select name="art_ubicacion" id="art_ubicacion" class="form-control input-sm" required>
                                <?php
                                $datos = mysqli_query($con,"SELECT * FROM ubicacion WHERE ubi_id='$ubicacion_ubi_id' ") ;
                                while( $fila = mysqli_fetch_array( $datos ) )
                                {
                                echo' <option value="'.$fila['ubi_id'].'">'.$fila['ubi_nombre'].'</option>';
                                }
                                $datos = mysqli_query($con,"SELECT * FROM ubicacion WHERE empresa='$empresa' and ubi_id<>'$ubicacion_ubi_id' order by ubi_nombre asc") ;
                                while( $fila = mysqli_fetch_array( $datos ) )
                                {
                                echo' <option value="'.$fila['ubi_id'].'">'.$fila['ubi_nombre'].'</option>';
                                }
                                ?>
                            </select>
                            </section>
                            <div class="form-group">
                                <label>Precio Costo</label>
                                <input type="number" name="art_costo" value="<?php echo  $art_precio_costo;?>" class="form-control input-sm" required>
                            </div> <!-- /.form-group -->
                            <?php if($emp_regimen=='Responsable de Iva') { ?>
                            <label>Impuesto</label>
                            <select name="art_iva" id="art_iva" class="form-control input-sm" required >
                                <option value="<?php echo $art_iva; ?>"><?php echo $art_iva; ?> %</option>
                                <?php
                                    $datos=mysqli_query($con,"SELECT * FROM iva where empresa='$empresa' and iva_porcentaje<>'$art_iva'") ;
                                    while($fila=mysqli_fetch_array($datos ))
                                    {
                                    echo' <option value="'.$fila['iva_porcentaje'].'" select>'.$fila['iva_nombre'].'</option>';
                                    }
                                ?>
                            </select> 
                            <?php } else { ?>
                            <input type="hidden" name="art_iva" id="art_iva" class="form-control input-sm" autocomplete="off" value="0">
                            <?php } ?>
                            <div class="form-group">
                                <label>Precio Venta</label>
                                <input type="number" name="art_pvp" value="<?php echo  $art_pvp;?>" class="form-control input-sm" required>
                            </div> <!-- /.form-group -->

                            <label>&nbsp;</label>
                            <label for="imagen" style="width: 100%;"><i class="btn-icon-only icon-upload"></i>Subir Imagen </label>
                            <input type="file" name="imagen" id="imagen">
                        </div>
                        
                        <div class="col-md-4">
                            <label>&nbsp;</label>
                            <ul class="gallery-container" >
                                <?php
                                $files = glob("img/articulos/$art_id.jpg");
                                if(count($files) > 0) 
                                { 
                                    foreach($files as $file) 
                                    { 
                                ?>
                                <li>
                                    <?php  $nu=rand(1,1000); $nu; ?>
                                    <a href="img\articulos\<?php echo $art_id.'.jpg?' .$nu .''; ?>" class="ui-lightbox">
                                        <img src="img\articulos\<?php echo $art_id.'.jpg?' .$nu .''; ?>" alt="" height="250px"/>
                                    </a>
                                    <label for="imagen" style="width: 98%;">Cambiar Imagen </label>
                                    <input type="file" name="imagen" id="imagen" >
                                </li>
                                <?php } } else { ?>
                                <li>
                                    <a href="img/articulos/sin_imagen.jpg" class="ui-lightbox">
                                        <img src="img/articulos/sin_imagen.jpg" alt=""  height="250px"/>
                                    </a>
                                </li>
                                <?php } ?>
                            </ul>
                        </div>

                        <div class="col-md-8">
                            <div class="form-group">
                                <a href="#" type="button" id="add_field" class="btn btn-primary" ><i class="btn-icon-only icon-plus"></i>Añadir Adicionales</a>
                            </div>
                            </br>
                            <div class="table-responsive">
                                <div id="resultados"></div>
                                <div id="tabla_sabores"></div>
                            </div> <!-- /.table-responsive -->
                            <div id="listas"> </div>
                        </div>
                        

                    </div>
                    <div class="col-md-12 pull-right">
                        <div class="modal-footer">
                            <a href="I_produccion.php"  class="btn btn-default">Cancelar</a>
                            <button id="modificar_com" type="submit" class="btn btn-success">Modificar </button> 
                        </div>
                        <?php include ('include/pie_de_pagina.php'); ?>
                    </div>
                </div> <!-- /.widget-content -->
            </form>
        </div> <!-- /.widget -->
    </div> <!-- /.col-md-9 -->
</div>  

</div><!-- /.modal --> 
<script>
function loadTablasabores(){
   var art_id= $("#art_id").val();
    $.get("./php_produccion/tabla_sabores.php?art_id="+art_id,"",function(data){
    $("#tabla_sabores").html(data);
  })
}
loadTablasabores();


var campos_max = 1000;   //max de 10 campos
var x = 0;
$('#add_field').click (function(e) {
    e.preventDefault();     //prevenir novos clicks
    if (x < campos_max)
    {
        $('#listas').append('<div>\
            <div class="col-xs-5" style="padding-left: 0px;">\
                <input class="form-control input-sm" type="text" placeholder="Nombre adicional" name="opcion1[]">\
            </div>\
            <div class="col-xs-3">\
                <input class="form-control input-sm " type="text" placeholder="Precio costo" name="opcion2[]">\
            </div>\
            <div class="col-xs-3">\
                <input class="form-control input-sm " type="text" placeholder="Precio venta" name="opcion3[]">\
            </div>\
            <a href="#" class="remover_campo btn btn-xs btn-danger "> <i class="btn-icon-only icon-remove"></i></a>\
            </br></br></div>');
            x++;
    }
});
// Remover o div anterior
$('#listas').on("click",".remover_campo",function(e) {
      e.preventDefault();
      $(this).parent('div').remove();
      x--;
});




$( "#form_combo_mod" ).submit(function( event ) 
{
    $('#modificar_com').attr("disabled", true);
    var formData = new FormData($("#form_combo_mod")[0]);
    var ruta = "php_produccion/editar_producto.php";
    $.ajax({
        type: "POST",
        url: ruta,
        data: formData,
        contentType: false,
        processData: false, 
        beforeSend: function(objeto){
            $('#div_produccion').html('<center><img src="./img/ajax-loader.gif"> CARGANDO...</center>');
        },
        success: function(datos){
        $("#div_produccion").html(datos);
        $('#modificar_com').attr("disabled", false);
    }
});
event.preventDefault();
})

</script>
<?php
  include ('php_caja/caja_abrir.php');
  include ('script.php');
?>
  </body>
</html>

<?php
}
else
{
   echo '<script>alert("No tienes permiso para ver esta pagina")</script>';
   echo '<script> setTimeout("history.back(1)", 0); </script>';
}
}
else
{
    echo '<script>location.href = "index.php";</script>';
}
?>