<?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 = 15; 
    $adjacents  = 4; 
    $offset = ($page - 1) * $per_page;
    $count_query=mysqli_query($con,"SELECT count(*) AS numrows FROM garantias_proveedor  
      where empresa='$empresa' and sucursal='$sucursal' and (gar_numero like '%$q%' OR gar_nombre like '%$q%')");
    $row= mysqli_fetch_array($count_query);
    $numrows = $row['numrows'];
    $total_pages = ceil($numrows/$per_page);
    $reload = '../C_compras.php';
    $sqlg = mysqli_query($con,"SELECT * FROM garantias_proveedor 
      where empresa='$empresa' and sucursal='$sucursal' and (gar_numero like '%$q%' OR gar_nombre like '%$q%' )
      order by gar_id desc LIMIT $offset,$per_page");
    
    if ($numrows>0){
?>
<div class="table-responsive">
    <table class="table table-striped table-bordered">
        <thead>
            <tr>
                <th >Nº</th>
                <th width="12%">FECHA ENVIO</th>
                <th width="17%">FECHA ENTREGA</th>
                <th>PROVEEDOR</th>
                <th width="10%">ESTADO</th>
                <th width="15%"></th>
            </tr>
        </thead>
        <tbody  class="buscar">
            <?php
                while ($reg=mysqli_fetch_array($sqlg)){
                $gar_id=$reg['gar_id'];
            ?>
                <tr>
                    <td><?php echo $reg['gar_numero']; ?></td>
                    <td><?php echo $reg['gar_fecha_envio']; ?></td>
                    <td><?php echo $reg['gar_fecha_solucion']; ?></td>
                    <td><?php echo $reg['gar_nombre']; ?></td>
                    <td><?php
                        if($reg['gar_estado']=="Solucionado")
                            {
                            echo $estados='<a title="" class=""><span class="badgeazul"><i class="btn-icon-only ">Solucionado</i></span></a>' ;
                            }
                            else if ($reg['gar_estado']=="Enviado" || $reg['gar_estado']=="Pendiente")
                            {
                            echo $estados='<a  title="" class=""><span class="badgenaranja"><i class="btn-icon-only ">Enviado</i></span></a>';
                            } else {
                                 echo $estados='<a href=" " onclick="estado('.$gar_id.')" title="Enviar" class=""><span class="badgerojo">Sin enviar <i class="fa-solid fa-paper-plane"></i></span></a>';
                            }
                        ?> 
                    </td>
                    <td>
                        <a href="C_garantias_modificar.php?gar_id=<?php echo $reg['gar_id']; ?>" title="Solución" class="btn btn-xs btn-info"><i class="btn-icon-only icon-edit"></i></a>
                        <a href="libraries/pdf/pdf_recibos/garantia_proveedor.php?gar_id=<?php echo $reg['gar_id'];?>" target="_blank"   class="btn btn-xs btn-success"><span class="btn-icon-only icon-print"></span></a>
                      
                        <?php 
                        $files = glob("../img/img_garantias/$gar_id.pdf");
                        if(count($files) > 0) 
                        { 
                        foreach($files as $file) 
                            { 
                                echo "<a href='img/img_garantias/$gar_id.pdf' target='_blank' class='btn btn-warning btn-xs'><span class='btn-icon-only icon-print'></span></a>"; 
                            } 
                        } 
                        else
                        {   ?>
                            <a data-id="<?php echo $reg['gar_id'];?>" class="btn btn-pdf btn-xs btn-primary"> <i class="btn-icon-only icon-upload"></i></a>
                       <?php  
                            }
                        if($reg['gar_estado']<>"Solucionado")
                        { ?>
                        <a onclick="eliminar('<?php echo $reg['gar_id'];?>');" title="Eliminar" class="btn btn-xs btn-danger"> <i class="btn-icon-only icon-remove"></i></a>
                        <?php } ?> 
                    </td>
                </tr>
                <?php }?>
                <tr>
                    <td colspan=7><span class="pull-right">
                        <?php echo paginate($reload, $page, $total_pages, $adjacents); ?></span>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>    
<?php } } ?>
<script>
    $(".btn-pdf").click(function(){
      id = $(this).data("id");
      $.get("./php_garantias_proveedor/form_pdf.php","id="+id,function(data){
        $("#form-pdf").html(data);
      });
      $('#Modalpdf').modal('show');
    });
</script>
<div class="modal fade" id="Modalpdf" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
            </div>
            <div class="modal-body">
                <div id="form-pdf"></div>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->


<script> 
function eliminar (id)
{
    var q= $("#q").val();
    if (confirm("Realmente deseas eliminar la Garantia")){ 
    $.ajax({
        type: "GET",
        url: "php_garantias_proveedor/eliminar_garantias.php",
        data: "id="+id,"q":q,
     beforeSend: function(objeto){
         $('#resultados').html('<center><img src="./img/ajax-loader.gif"> CARGANDO...</center>');
      },
        success: function(datos){
    $("#resultados").html(datos);
    load(1);
    }
      });
    }
}

function estado(gar_id) 
        { p = confirm("Estas seguro que deseas enviar la garantia?");
        if (p) {
            $.ajax({ 
                type: "POST",
                url: "php_garantias_proveedor/cambiar_estado_garantia.php",
                data: "gar_id="+gar_id,
                success: function(datos){
                $("#resultados").html(datos);
                load(1);
            }
            });
        } }
</script> 


