<?php
include_once("../header.php");
include_once("../php/config.php");

$sql_clientes = mysql_query("select * from clientes order by nombre asc") or die(mysql_error());


?>
<style>
    .titulos{
        font-family: verdana;
        font-size: 12px;
    }
</style>
<br>
    <div class="container" style="background:white; padding:18px; ">
    <!-- Breadcrumbs-->
          

               
   <form id="frm" name="frm" action="" method="post">           
    <div class="row">
       
        <div class="col-md-12">
            <h3 class="text-rojo"><i class="fa fa-calendar"></i> Aplicar descuento a cliente</h3>
            <hr>
                 <div class="row">
                        <div class="col-md-1">
                            <b>Clientes</b>
                        </div>
                        <div class="col-md-3">
                            <select  class="form-control  mb-2" id="cliente" name="cliente">

                                <option value="" selected>Seleccione un cliente</option>
                                <?php while ($r = mysql_fetch_array($sql_clientes)) { ?>
                                    <option value="<?php echo $r['nombre'] ?>"><?php echo $r['nombre'] ?></option>
                                <?php } ?>
                            </select>
                        </div>
                     
					 
					    <div class="col-md-1">
                            <b class="mt-2">Balance</b>
                        </div>
                        <div class="col-md-3">
                            <input type="text" id="balance" name="balance" class="form-control" placeholder="RD$ 0.00">
                        </div>
                     
                     
                       
                     
                     
					</div>
			<br>
		<div class="row">
			 <div class="col-md-1">
			   % Desc
			 </div>
			
			<div class="col-md-1">
			  <input type="text" id="descuento" name="descuento" class="form-control" placeholder="% 0.00" onKeyUp="calcular()">
			 </div>
			
			
			<div class="col-md-1">
			   Valor
			 </div>
			
			<div class="col-md-3">
			  <input type="text" id="valor_descuento" name="valor_descuento" class="form-control" placeholder="RD$ 0.00">
			 </div>
			 
		</div>
			
		<hr>
		<button  class="btn btn-rojo" name="grabar" id="grabar"> <i class="bi bi-upload"></i> Aplicar</a>
	
        </div>
                    
    </div>
    </form>     
            
            
</div>
          


          
</div>
    </div>



        	<!---Modal mora-->
	<div class="modal" id="modalMora" data-backdrop="static" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
      <div class="modal-dialog modal-sm" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title" id="exampleModalLabel">Ajuste de mora manual</h5>
            <button class="close" type="button" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">×</span>
            </button>
          </div>
          <div class="modal-body">
              <div class="row">
                  <label for="monto_mora" class="col-sm-4 col-form-label" >Monto</label>
                  <div class="col-sm-8">
                      <input autocomplete="off" type="number"  class="form-control mb-2" id="monto_mora" name="monto_mora" value="">
                  </div>
              </div>
          </div>
          <div class="modal-footer">
          <button  onClick="ajuste_mora()" class="btn btn-warning"  data-dismiss="modal"><i class="fa fa-check"></i>Aceptar</button>
           <button class="btn btn-default"  data-dismiss="modal"><i class="fa fa-home"></i>Salir</button>
          </div>
        </div>
      </div>
    </div>
        
        
	<?php
	include_once("../footer.php");
 
	?>
<script> 
	
	
	function calcular(){
		var balance = $("#balance").val();
		var descuento = $("#descuento").val()
		
		$("#valor_descuento").val(parseFloat(balance) * parseFloat(descuento) /100);
		
	}
	
	
$(function(){
          $("#cliente").select2()
			$("#grabar").click(function(e){

				e.preventDefault();
				if(!confirm("Seguro que desea aplicar este descuento?")){return;}
				_enviar("descontar.php", $("#frm").serialize())
				.done(function(data){
					if(data==1){
						alert("Factura actualizada correctamente");
						window.location.reload()
					}else{
						alert(data);
					}
				})
			})
})
	
	
	
	$(function(){
         
			$("#cliente").change(function(e){

				e.preventDefault();
				
				_enviar("get_balance2.php", $("#frm").serialize())
				.done(function(data){
					    $("#descuento").val("")
					   $("#valor").val("")
						$("#balance").val(data)
					
				})
			})
})
   </script>