	<?php
	$usuario = $_COOKIE["usuario"];
$nivel = $_COOKIE["nivel"];
if($nivel=="administrador"){$usuario="";}

	include_once("../header.php");
        include_once("../php/config.php");
		if(isSet($_GET["filtro"])){
        $sql = mysql_query("select prestamos.id_prestamo, prestamos.tasa, prestamos.monto_prestado, prestamos.fecha_prestamo, prestamos.balance_capital, prestamos.balance_interes, prestamos.balance,clientes.cedula, clientes.telefono, clientes.nombre
            from prestamos inner join clientes on clientes.id_cliente = prestamos.id_cliente where clientes.nombre like '%".$_GET['filtro']."%' and prestamos.balance>1 order by clientes.nombre desc") or die(mysql_error());
		}
	?>

<style>
th{
	font-size:18px;
}
</style>

    <div class="container">
       <?php include_once("../header.php")?>  
	   
	   <br>
	   			<span class="text-primary h3"><b><i class="i bi-coin"></i> Financiamientos registrados</b></span><hr>

        <div class="row">
		
			 <div class="col-md-3 mb-2  mt-2">
				<input type="search" id="txt_buscar" class="form-control" value="<?php echo $_GET['filtro']?>">
			</div>

 <div class="col-md-1 mb-2  mt-2 mb-2">
        <a class="btn btn-default " href="#" onClick="filtrar()"><i class="fa fa-search"></i><span style="font-weight:bolder;"> Filtrar</span></a>
</div>
		</div>
		<div class="row mt-2">
			<div class="col-md-12">
			
				<a class="btn  btn-success mt-2" href="_nueva_venta.php"><i class="fa fa-plus"></i> Registrar una venta</a>
				 <a class="btn  btn-default mt-2" href="menu_renta.php"><i class="fa fa-pencil"></i> Salir</a>
				<br>
				<div class="table-responsive mt-2">
					<table class="table table-sm table-borderred table-striped" data-page-length='8'>
						<thead>
							<th># Financiamiento</th>
                             <th>Fecha</th>
							<th>Nombre/Apell.</th>
							<th>Cedula</th>
							<th>Telefono</th>
                            
							<th>Monto prestado</th>
								<th>Balance</th>
							<th>(%)Tasa</th>
                      <th>Accion</th>
					  <th>Cobrar</th>
						</thead>
						<tbody>
                                                        <?php while($r=@mysql_fetch_array($sql)){
                                          $id_prestamo=$r["id_prestamo"]; 
                                          $c++;
                                          $prestado+=$r["monto_prestado"];
                                
                                          $capital+=$r["balance_capital"];
                                          $interes+=$r["balance_interes"];
                                          $balance+=$r["balance"];
                                          
                                          
                                          
                                                        ?>
							<tr>
								<td><b class='text-primary'><?php echo $r["id_prestamo"]?></b></td>
								<td><?php echo fecha($r["fecha_prestamo"])?></td>
								<td><?php echo "<a href='cobrar.php?id_prestamo=$id_prestamo'>"?><b class="text-primary"><?php echo $r["nombre"]."</a>"?></b></td>
								<td><?php echo $r["cedula"]?></td>
								<td><?php echo $r["telefono"]?></td>
							
								<td><?php echo decimal($r["monto_prestado"])?></td>
								<td class='text-success'><b><?php echo decimal($r["balance"])?></b></td>
								<td><?php echo decimal($r["tasa"])?></td>
								
								<td><?php echo "<a class='btn btn-info' href='datos_prestamo.php?id_prestamo=$id_prestamo'><i class='bi bi-pen'></i>  Editar</a>"?></td>
							    <td><?php echo "<a class='btn btn-success' href='cobrar.php?id_prestamo=$id_prestamo'><i class='bi bi-coin'> Cobrar</a>"?></td>
														</td>	
							</tr>
                                                        <?php }?>
						</tbody>
						<tfoot>
						     <th><b>Cant. prestamos</b> </th>
						    <th><b>Total prestado</b></th>
						    
						    <th><b class="text-primary">BALANCE GENERAL</b></th>
						    <tr>
						         <td><?php echo decimal($c)?></td>
						        <td><?php echo decimal($prestado)?></td>
						       
						         
						        <td class="bg-success"><b><?php echo decimal($balance)?></b></td>
						    </tr>
						</tfoot>
					</table>
				</div>
			</div>
		</div>
		
        </div>

      


          
</div>
    </div>



	<?php
	include_once("../footer.php");
	?>
	
	<script>
	
function filtrar(){
    var t = $("#txt_buscar").val();
    window.location.href="prestamos.php?filtro="+t;
   }
   
   $("#txt_buscar").focus()
		
$("#txt_buscar").select()
$("#txt_buscar").on("keypress", function(){
  uniKeyCode(event);
})

////window.addEventListener("keydown",tecla,false);


 function uniKeyCode(event) {
  var key = event.keyCode;
  //alert("Unicode KEY code: " + key);
  if(key=="13"){
	
		////if($("#txt_buscar").val()!=""){
			filtrar();
		///}
	
	  }
  
}

</script>

