<?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  
  where empresa='$empresa' and sucursal='$sucursal' and ( gar_nombre like '%$q%' or gar_numero 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 where empresa='$empresa' and sucursal='$sucursal' and ( gar_nombre like '%$q%' or gar_numero like '%$q%') order by gar_numero 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 >FECHA RECIBO</th>
                <th >FECHA ENTREGA</th>
                <th >CLIENTE</th>
                <th >EQUIPO</th>
                <th >USUARIO</th>
                <th >ESTADO</th>
                <th width="13%"></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_recibo']; ?></td>
                <td><?php  echo $reg['gar_fecha_entrega']; ?></td>
                <td><?php  echo $reg['gar_nombre']; ?></td>
                <td><?php  echo $reg['gar_articulo']; ?></td>
                <td><?php  
                        $sql_tar=mysqli_query($con, "SELECT * from usuario where usu_id='".$reg['usuario']."'");
                        $bo=mysqli_fetch_array($sql_tar);
                        echo $usu_n=$bo['usu_nombre'];
                    ?>
                </td>

                <td><div  class="btn-group pull-right " >
                    <?php if($reg['gar_estado']=="Pendiente"){ ?>
                        <button type="button" class="btn btn-xs  btn-primary dropdown-toggle" data-toggle="dropdown">Pendiente <span class="caret"></span></button>

                        <ul class="dropdown-menu" role="menu">
                             <li><a href="#" data-id="<?php echo $reg['gar_id'];?>" class="btn-edit">Reparado</a></li>
                             <li><a href="#" data-id="<?php echo $reg['gar_id'];?>" class="btn-cambio">No reparado</a></li>
                        </ul>
                    <?php
                          //  include ('modal.php');
                        } else if ($reg['gar_estado']=="Reparado"){ ?>
                        <a title=""  class=""><span class="badgegris"><i class="btn-icon-only"><?php  echo $reg['gar_estado']; ?></i></span></a>
                    <?php } else { ?>
                        <a  title="" class=""><span class="badgeazul"><i class="btn-icon-only"><?php  echo $reg['gar_estado']; ?></i></span></a>
                    <?php } ?></span> </div></td>
                <td>
                <?php if($reg['gar_estado']=="Pendiente"){?>
                    <a href="V_garantias_modificar.php?gar_id=<?php echo $reg['gar_id']; ?>" title="Editar Garantia" class="btn btn-xs btn-info"><i class="btn-icon-only icon-edit"></i></a>
                    
                    <a href="#" class='btn btn-xs btn-danger' title='Eliminar ' onclick="eliminar('<?php echo $reg['gar_id'];?>');" ><i class="btn-icon-only icon-remove"></i></a>                     
                     
                    <a href="libraries/pdf/pdf_recibos/garantia_cliente.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 }else { ?>
                    <a href="libraries/pdf/pdf_recibos/garantia_cliente.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 }?>
                <?php 
                    $files = glob("../img/img_garantias_clientes/$gar_id.pdf");
                    if(count($files) > 0) 
                    { 
                        foreach($files as $file) 
                        { 
                           ?> <a data-id="<?php echo $reg['gar_id'];?>" class="btn btn-pdf btn-xs btn-info"> <i class="btn-icon-only icon-upload"></i></a>
                       <?php } 
                    } 
                    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  }  ?>

                </td>
            </tr>
        <?php } ?>
            <tr>
                <td colspan=8><span class="pull-right">
                    <?php echo paginate($reload, $page, $total_pages, $adjacents); ?></span>
                </td>
            </tr>
        </tbody>
    </table>
</div>
<?php } } ?>     

<script>
$(".btn-edit").click(function(){
  id = $(this).data("id");
  $.get("./php_garantias/modal_entrega.php","id="+id,function(data){
    $("#form-edit").html(data);
  });
  $('#modal_reparado').modal('show');
});
</script>

<div class="modal fade" id="modal_reparado" 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">Entrega del Producto</h4>
        </div>
        <div class="modal-body">
        <div id="form-edit"></div>
        </div>
      </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
  </div><!-- /.modal -->


  <script>
    $(".btn-cambio").click(function(){
      id = $(this).data("id");
      $.get("./php_garantias/modal_cambio.php","id="+id,function(data){
        $("#form-cambio").html(data);
      });
      $('#cambioModal').modal('show');
    });
</script>

  <div class="modal fade" id="cambioModal" 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">Cambio del Producto</h4>
        </div>
        <div class="modal-body">
        <div id="form-cambio"></div>
        </div>
      </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
  </div><!-- /.modal -->


<script>
    $(".btn-pdf").click(function(){
      id = $(this).data("id");
      $.get("./php_garantias/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 -->