<?php
include_once "../header.php";
include_once("../php/config.php");
$id_prestamo =$_GET["id_prestamo"];
$id =$_GET["id"];

$sql= mysql_query("select * from cuotas where  id='$id'") or die(mysql_error());


?>

<div class="container">
<div class="row mt-2">

    <div class="col-md-1">
        <label>Id</label>
    </div>
      <div class="col-md-10">
        <input type="text" class="form-control" id="id" value="<?php echo @mysql_result($sql,0,'id')?>" readonly>
    </div>
</div>

<div class="row mt-2">

    <div class="col-md-1">
        <label>Capital</label>
    </div>
      <div class="col-md-10">
        <input type="number" class="form-control" id="capital" value="<?php echo @mysql_result($sql,0,'balance_capital')?>">
    </div>
</div>


<div class="row mt-2">

    <div class="col-md-1">
        <label>Interes</label>
    </div>
      <div class="col-md-10">
        <input type="number" class="form-control" id="interes" value="<?php echo @mysql_result($sql,0,'balance_interes')?>">
    </div>
</div>

<div class="row mt-2">

    <div class="col-md-1">
        <label>Mora</label>
    </div>
      <div class="col-md-10">
        <input type="number" class="form-control" id="mora" value="<?php echo @mysql_result($sql,0,'mora')?>">
    </div>
</div>

<div class="row mt-2">

    <div class="col-md-1">
        <label>Total</label>
    </div>
      <div class="col-md-10">
        <input type="number" class="form-control" id="total">
    </div>
</div>
<hr>
<div class="row mt-2">

    <div class="col-md-1">
        <label>PAGAR</label>
    </div>
      <div class="col-md-10">
        <input type="text" class="form-control" id="pagar" onkeyup="calculo()">
    </div>
</div>
<hr>
<div class="row mt-2">
    <div class="col-md-1">
        <label>Pago de mora</label>
    </div>
      <div class="col-md-1">
        <span   id="pago_mora">0.00</span>
    </div>



    <div class="col-md-1">
        <label>Pago interes</label>
    </div>
      <div class="col-md-1">
        <span   id="pago_interes">0.00</span>
    </div>



    <div class="col-md-1">
        <label>Pago capital</label>
    </div>
      <div class="col-md-1">
        <span   id="pago_capital">0.00</span>
    </div>
</div>
<div class="row">
    <div class="col-md-12">
        <button class="btn btn-success" onclick="pagar()"> Aplicar abono</button>
        <a class="btn btn-default" href="prestamos.php?id_prestamo=<?php echo $id_prestamo?>"> Regresar</a>
    </div>
</div>


<?php


include_once "../footer.php";


?>
<script>
    $("#total").val(parseFloat($("#mora").val()) + parseFloat($("#interes").val()) + parseFloat($("#capital").val()))
    function calculo(){
        var capital = parseFloat($("#capital").val());
        var interes = parseFloat($("#interes").val());
        var mora = parseFloat($("#mora").val());
        var total = parseFloat($("#total").val());
        var pagar = parseFloat($("#pagar").val());
        var restan = 0.00;
        restan = pagar;

        if(mora>0){
            if(restan>=mora){
                $("#pago_mora").html(mora);
                restan = restan - mora;
            }else{
             $("#pago_mora").html(restan);
             restan = 0;
            }
        }


        if(interes>0){
            if(restan>=interes){
                $("#pago_interes").html(interes);
                restan = restan - interes;
            }else{
             $("#pago_interes").html(restan);
             restan = 0;
        }
        }
        if(capital>0){
                if(restan>=capital){
                    $("#pago_capital").html(capital.toFixed(2));
                    restan = restan - capital;
               }else{
                 $("#pago_capital").html(restan);
                 restan = 0;
        }
        }


        if(pagar>total){
            $("#pagar").val(total)
        }

    }









///grabar abono
function pagar(){
        var id = $("#id").val();
        var capital = parseFloat($("#pago_capital").html());
        var interes = parseFloat($("#pago_interes").html());
        var mora = parseFloat($("#pago_mora").html());
        var total = parseFloat($("#pagar").val());
       
        if(isNaN(total) || total<=0 || total=="" || id==""){
            alert("Datos invalidos!!");
            return;
        }
        if(!confirm("Seguro de procesar este abono")){return;}

        var param;
        param = "capital="+capital;
        param+="&interes="+interes;
        param+="&mora="+mora;
        param+="&total="+total
        param+="&id="+id
        $(".btn").hide();
        window.location.href="grabar_abono.php?"+param;

       
}
</script>