<?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'; //include pagination file
    $page = (isset($_REQUEST['page']) && !empty($_REQUEST['page']))?$_REQUEST['page']:1;
    $per_page = 11; //how much records you want to show
    $adjacents  = 4; //gap between pages after number of adjacents
    $offset = ($page - 1) * $per_page;

    $count_query=mysqli_query($con, "SELECT count(*) AS numrows FROM combos where empresa='$empresa' and (com_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 combos where empresa='$empresa' and (com_nombre like '%$q%' OR com_referencia like '%$q%' ) order by com_nombre 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="10%">CANTIDAD</th>
                <th width="10%">PRECIO</th>
                <th width="12%"></th>
            </tr>
        </thead>
        <tbody class="buscara">
            <?php  
                while ($reg=mysqli_fetch_array($query)){
                      $id=$reg['com_id'];
                      $com_id=$reg['com_id'];
            ?>
            <tr>
                <td><?php  echo $reg['com_referencia']; ?></td>
                <td><?php  echo $reg['com_nombre']; ?></td>
                <td><p class="pull-right"><?php  echo $reg['com_cantidad']; ?></p></td>
                <td ><p class="pull-right">
                    <?php
                      $sumador_total=0;
                      $sql1=mysqli_query($con,"SELECT * FROM  combos_articulos where combos_com_id='$id'");
                      while($row1=mysqli_fetch_array($sql1))
                      {
                      $coma_art_id=$row1['coma_art_id'];
                      $coma_art_cantidad=$row1['coma_art_cantidad'];
                      $coma_art_precio=$row1['coma_art_precio'];
                      $art_pvp=$coma_art_cantidad*$coma_art_precio;
                      $sumador_total+=$art_pvp;//Sumador
                      }
                      echo number_format($sumador_total,0);
                    ?>
                </p></td>
                <td>
                    <a href="#verdetalle<?php echo $reg['com_id']; ?>" class='btn btn-xs btn-warning' title='vetalle'  data-toggle="modal" ><i class="btn-icon-only icon-eye-open"></i></a>
                    <a href="I_combo_modificar.php?com_id=<?php echo $reg['com_id']; ?>" class='btn btn-xs btn-info' title='Editar Combo' ><i class="btn-icon-only icon-edit"></i></a> 
                    <a href="#" onclick="eliminar('<?php echo $reg['com_id'];?>')" title="Eliminar" class="btn btn-xs btn-danger"> <i class="btn-icon-only icon-remove"></i></a>
                    <?php include ('form_detalle.php'); ?>
                </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 obtener_datos(id){
      var referencia = $("#referencia"+id).val();
      var nombre = $("#nombre"+id).val();
      var costo = $("#costo"+id).val();
      var pvp = $("#pvp"+id).val();

      $("#id").val(id);
      $("#referencia").val(referencia);
      $("#nombre").val(nombre);
      $("#costo").val(costo);
      $("#pvp").val(pvp);
    }
function eliminar (com_id)
{
    if (confirm("Realmente deseas eliminar el combo")){ 
    $.ajax({
        type: "GET",
        url: "php_combos/eliminar_combo.php",
        data: "com_id="+com_id,
        beforeSend: function(objeto){
         $('#resultados').html('<center><img src="./img/ajax-loader.gif"> CARGANDO...</center>');
        },
        success: function(datos){
        $("#resultados").html(datos);
        load(1);
        }
        });
    }
} 
</script>