<?php
$id = $_GET["id"];
$usuario =  $_COOKIE["usuario"];
include_once "../../php/config.php";
include_once("set_mora.php");
$q = mysql_query("select * from cuotas where id_prestamo='$id' and balance>1") or die(mysql_error());

include_once 'header.php';
?>
<head>


  <link rel="icon" sizes="192x192" href="../../imagenes/mon.png">
  <script src="jquery.js"></script>


</header>
<body>
  <div class="container">
<h4> Cuotas del prestamo # <?php echo "000"."".$id?></h4>
<hr>
<?php


$id_prestamo =$_GET["id"];
$id_cuota =$_GET["id_cuota"];
$sql_cuotas= mysql_query("select * from cuotas where id_prestamo='$id_prestamo' and balance>0") or die(mysql_error());

$sql_suma= mysql_query("select * from cuotas where id<='$id_cuota' and id_prestamo='$id_prestamo' and balance>1") or die(mysql_error());
while($r0=mysql_fetch_array($sql_suma)){
    $capital+=$r0["balance_capital"];
    $interes+=$r0["balance_interes"];
    $total+=$r0["balance"];
    $mora+=$r0["mora"];
    $id_cuota=$r0["id"];
    $ncuota=$r0["numero"];
	
}
    

?>


<form id="frm" name="frm" action="grabar_pago.php" method="POST" onSubmit="desabilita()">
    <div class="row">
        <div class="col-md-12">
                <div class="row">
                    <div class="col-md-2 mt-2" style="display:none">
                        <label># Prestamo</label>
                        <input type="number" id="numero" name="numero" class="form-control"  readonly value="<?php echo $_GET['id']?>">
                    </div>
                    
                  <div class="col-md-3 mt-2">
                         <label>Seleccione la cuota a pagar</label><br>
                            <select  class="form-control  mb-2" id="cuotas"      name="cuotas" onChange="sumar(this.value)">
                                 <option value="" selected>Cuotas</option>
                                <?php while ($r3 = mysql_fetch_array($sql_cuotas)) { 
                                         // $capital+=$r3["balance_capital"];
                                          ///$interes+=$r3["balance_interes"];
                                          //$balance+=$r3["balance"];
                                ?>
                                    <option value="<?php echo $r3['id'] ?>">
                                        
                                        #<?php echo $r3['numero'] ?> |
                                        <?php echo fecha($r3['vence'])?> |
                                     - <?php echo "RD$: ".decimal($r3['balance']) ?></option>
                                <?php } ?>
                                
                            </select>
                </div> 
  
            </div>
                 
            <br><br>                
           
                
                
                    <b>Capital:</b> <?php echo decimal($capital) ?> |  
                
                    
                
                    <b>Interes:</b> <?php echo decimal($interes) ?>|
                      <b>Mora:</b>  <?php echo "<b class='text-primary'>".decimal($mora)."</b>" ?> <br>
                 
                    <b>Balance:</b>  <?php echo "<b class='text-primary'>".decimal($total)."</b>" ?> <br>
                   
                    
               
                <input type="hidden" name="total" value="<?php echo $total?>">
            <br><br>
                    <button class="btn btn-success" name="btnGrabar" id="btnGrabar"><i class="fa fa-save"></i> Pagar cuota</button><hr><br>
                    <a class="btn btn-info ml-2" id='btnHistorial' href="historial_cobros_prestamo.php?id_prestamo=<?php echo $id_prestamo?>"><i class="fa fa-file"></i>  Ver historial pa pagos</a> 
                         <a class="btn btn-default ml-2" id='btnAtras' href="prestamos.php"><i class="fa fa-home"></i> Ir atras</a>
                
           
            <div class="text-primary" id="mensaje">
               <?php
               echo $_POST["cuotas"];
               ?>
            </div>
            
        </div>
    </div>
</form>
</div>
</body>

</div>
<?php
include_once 'footer.php';
?>
<script>
function nuevo_cliente(){
window.open("../clientes/clientes.php", "_blank", "toolbar=yes,scrollbars=yes,resizable=no,top=50,left=100,width=600,height=600");
}

function desabilita(){
    
    $(".btn").prop("disabled", true);
    $("a").hide();
    $("#mensaje").html("Grabando datos, favor espere...");
}

function sumar(c){
	
    var id_prestamo="<?php echo $id_prestamo?>";
    window.location.href="cuotas.php?id="+id_prestamo+"&id_cuota="+c;
	 $(".btn").prop("disabled", true);
     $("a").hide();
}
var id_cuota = "<?php echo $id_cuota?>";
$("#cuotas option[value='"+id_cuota+"']").prop('selected', true);
</script>
