<?php
include_once "../header.php";
include_once("../php/config.php");
$sql_cliente= mysql_query("select * from clientes") or die(mysql_error());
$sql_cobradores= mysql_query("select * from login") or die(mysql_error());
$q= mysql_query("select id_prestamo from prestamos order by id_prestamo desc limit 1") or die(mysql_error());
$last_id =@mysql_result($q,0,"id_prestamo")+1; 
$id_cuota = $_GET["id_cuota"];

$id_venta = $_GET["id_venta"];

$sql_venta= mysql_query("select * from ventas where id_venta='$id_venta'") or die(mysql_error());
$sCliente =@mysql_result($sql_venta,0,"cliente"); 


$sql_cliente2= mysql_query("select * from clientes where nombre='$sCliente'") or die(mysql_error());
$id_cliente =@mysql_result($sql_cliente2,0,"id_cliente"); 


$balance =@mysql_result($sql_venta,0,"balance"); 
$usuario =@mysql_result($sql_venta,0,"usuario");
$fecha =@mysql_result($sql_venta,0,"fecha");

///echo "Id cliente: ".$id_cliente;
?>

<style>
  body{
	  background:#D2D2D2;
  }
 
 .container{
	 
	  background:White;
	  padding:15px;
 }
</style>

<div class="container">
    <span class="text-primary h2"><i class="bi bi-coin"></i> Registrar nuevo Financiamiento</span><br>
	<hr>
<form id="frm" name="frm" action="cuotas.php" method="POST">
    <div class="row">
        <div class="col-md-12">
               <a class="mt-2" href="#" onClick="nuevo_cliente()"><i class="fa fa-user"></i> Nuevo Cliente</a>
             |
              <a class="mt-2" href="nuevo_prestamo.php" ><i class="fa fa-undo"></i> Refrescar</a>
             
			 <div class="row">
                    <div class="col-md-2 mt-2">
                        <label># Prestamo</label>
                        <input type="number" id="numero" name="numero" class="form-control" value="<?php echo $last_id;?>" readonly>
                    </div>
					 <div class="col-md-10 mt-2">
                        <label>Vendedor</label>
                        <input type="text" id="usuario" name="usuario" readonly class="form-control" value="<?php echo $usuario;?>"  readonly>
                    </div>
             </div>       
					
                <div class="row">
                    <div class="col-md-2 mt-2">
                        <label>Venta #</label>
                        <input type="number" id="id_venta" name="id_venta" class="form-control" value="<?php echo $id_venta;?>" readonly>
                    </div>
                    
                  <div class="col-md-3 mt-2">
                         <label>Cliente</label>
                            <select  class="form-control  mb-2" id="cliente"      name="cliente">
                                 <option value="">Sel. cliente</option>
                                <?php while ($r3 = mysql_fetch_array($sql_cliente)) { ?>
                                    <option value="<?php echo $r3['id_cliente'] ?>"><?php echo $r3['nombre'] ?></option>
                                <?php } ?>
                                
                            </select>
                </div> 
                
                
           
                <div class="col-md-2 mt-2">
                    <label>Fecha</label>
                    <input type="Date" id="fecha" name="fecha" class="form-control" value="<?php echo $fecha?>">
                </div>
                
                <div class="col-md-2 mt-2">
                    <label>Monto prestamo</label>
                    <input type="number" id="monto" name="monto" class="form-control" value="<?php echo $balance?>">
                </div>
                
                <div class="col-md-1 mt-2">
                    <label>Tasa</label>
                    <input type="text" id="tasa" name="tasa" class="form-control" value="<?php echo $_GET['tasa']?>">
                </div>
                  
              
                
                <div class="col-md-1 mt-2">
                    <label># Cuotas</label>
                    <input type="number" id="cuotas" name="cuotas" class="form-control" value="<?php echo $_GET['cuotas']?>" >
                </div>
                
                 
                
            </div>
            <div class="row">
                    <div class="col-md-2 mt-2">
                        <label>Periodos de pago</label>
                        <select id="tipo" name="tipo" class="form-control mt-2" selected="<?php echo $_GET['tipo']?>">
                            <option value="">Sel. Tipo</option>
                            <option value="Mensual">Mensual</option>
                            <option value="Quincenal">Quincenal</option>
                            <option value="Semanal">Semanal</option>
                            <option value="Diario">Diario</option>
                        </select>
                    </div>
                    
                     <div class="col-md-3 mt-2">
                        <label>Cobrador</label>
                        <select id="cobrador" name="cobrador" class="form-control mt-2" value="<?php echo $_GET['cobrador']?>">
                            <option value="">Sel. cobrador</option>
                            <?php while ($r3 = mysql_fetch_array($sql_cobradores)) { ?>
                                    <option value="<?php echo $r3['nombre'] ?>"><?php echo $r3['nombre'] ?></option>
                          <?php } ?>
                        </select>
                    </div>
                    
              
            </div>
            
            <div class="row">
                      <div class="col-md-3 mt-2">
                       
                        <button class="btn btn-danger"> Calcular cuotas</button>
                        <a class="btn btn-default ml-2" href="prestamos.php"> Salir</a>
                       </div>
            </div>
        </div>
    </div>
</form>
</div>

<?php
include_once "../footer.php";

?>

<script>
$(function(){
	  v = "<?php echo $id_cliente?>";
	    $("#cliente option[value='"+v+"']").prop('selected', true);
		$("#tasa").val("0");

})
function nuevo_cliente(){
window.open("../clientes/clientes.php", "_blank", "toolbar=yes,scrollbars=yes,resizable=no,top=50,left=100,width=600,height=600");
}

var cliente = "<?php echo $_GET['cliente']?>";
var id_prestamo = "<?php echo $_GET['numero']?>";
var tipo = "<?php echo $_GET['tipo']?>";
var cobrador = "<?php echo $_GET['cobrador']?>";
$("#cliente option[value='"+cliente+"']").prop('selected', true);
$("#numero option[value='"+id_prestamo+"']").prop('selected', true);
$("#tipo option[value='"+tipo+"']").prop('selected', true);
$("#cobrador option[value='"+cobrador+"']").prop('selected', true);

</script>
