<?php
session_start();
require_once('../conexion.php');
require_once('../include/sesiones.php'); 
$action = (isset($_REQUEST['action'])&& $_REQUEST['action'] !=NULL)?$_REQUEST['action']:'';
if($action == 'ajax'){
    $q = mysqli_real_escape_string($con,(strip_tags($_REQUEST['q'], ENT_QUOTES)));
    include '../include/pagination.php'; 
    $page = (isset($_REQUEST['page']) && !empty($_REQUEST['page']))?$_REQUEST['page']:1;
    $per_page = 11; 
    $adjacents  = 4; 
    $offset = ($page - 1) * $per_page;
    $count_query   = mysqli_query($con, "SELECT count(*) AS numrows FROM articulos where art_tipo='producto' and empresa='$empresa' and (art_nombre like '%$q%' ) ");
    $row= mysqli_fetch_array($count_query);
    $numrows = $row['numrows'];
    $total_pages = ceil($numrows/$per_page);
    $reload = './U_usuarios.php';
    $sql="SELECT * FROM  articulos where art_tipo='producto' and empresa='$empresa' and (art_nombre like '%$q%') order by art_referencia asc LIMIT $offset,$per_page";
    $query = mysqli_query($con, $sql);
    if ($numrows>0){
?>
<table class="table table-striped table-bordered">
    <thead>
        <tr>
            <th width="8%">CODIGO</th>
            <th>NOMBRE</th>
            <th width="7%">ESTADO</th>
            <th width="10%">PRECIO</th>
            <th width="10%"></th>
        </tr>
        </thead>
        <tbody class="buscara">
        <?php  
            while ($reg=mysqli_fetch_array($query)){
            $id=$reg['art_id'];;
        ?>
            <input type="hidden" value="<?php echo $reg['art_referencia'];?>" id="referencia<?php echo $reg['art_id'];?>">
            <input type="hidden" value="<?php echo $reg['art_nombre'];?>" id="nombre<?php echo $reg['art_id'];?>">
            <input type="hidden" value="<?php echo $reg['art_precio_costo'];?>" id="costo<?php echo $reg['art_id'];?>">
            <input type="hidden" value="<?php echo $reg['art_pvp'];?>" id="pvp<?php echo $reg['art_id'];?>">
        <tr>
            <td><?php echo $reg['art_referencia']; ?></td>
            <td><?php echo $reg['art_nombre']; ?></td>
             <td align="center">
            <?php
                 if($reg['art_estado']=="Activo")
                        {
                           echo '<a href="#" onclick="estado('.$reg['art_id'].')"> <span class="badgeazul" title="Desactivar"> <i class="btn-icon-only icon-off"></i></span></a>';
                        }
                    else  
                        {
                           echo '<a href="#" onclick="estado('.$reg['art_id'].')"> <span class="badgenaranja" title="Activar"> <i class="btn-icon-only icon-off"></i></span></a>';
                        }
                    
            ?>
            </td>
            <td><p class="pull-right"><?php  echo number_format($reg['art_pvp'],0); ?></p></td>
            <td>
              	<a href="I_produccion_modificar.php?art_id=<?php echo $reg['art_id']; ?>" class='btn btn-xs btn-info' title='Editar Servicio' ><i class="btn-icon-only icon-edit"></i></a> 
                <a href="#" onclick="eliminar('<?php echo $reg['art_id'];?>')" title="Eliminar" class="btn btn-xs btn-danger"> <i class="btn-icon-only icon-remove"></i></a>  
            </td>
        </tr>
<?php } ?>
    	<tr>
            <td colspan=5><span class="pull-right">
                <?php echo paginate($reload, $page, $total_pages, $adjacents);?>
                </span>
            </td>
        </tr>
    </table>
<?php } } ?>
<script>
function eliminar (art_id)
{
    if (confirm("Realmente deseas eliminar el producto")){ 
    $.ajax({
        type: "GET",
        url: "php_produccion/eliminar_producto.php",
      	data: "art_id="+art_id,
     	beforeSend: function(objeto){
         $('#resultados').html('<center><img src="./img/ajax-loader.gif"> CARGANDO...</center>');
      	},
        success: function(datos){
    	$("#resultados").html(datos);
    	load(1);
    	}
      	});
    }
} 


function estado(art_id) 
{
    $.ajax({
        type: "POST",
        url: "php_produccion/cambiar_estado.php",
        data: "art_id="+art_id,
        success: function(datos){
        $("#resultados").html(datos);
        load(1);
    }
    });
} 
</script>


