<?php include "includes/header.php";?>

<?php include "includes/navbar.php";?>

  <div id="wrapper">

    <!-- Sidebar -->
   <?php include "includes/sidebar.php";?>

    <div id="content-wrapper">

      <div class="container-fluid">

        <!-- Breadcrumbs-->
        <ol class="breadcrumb">
          <li class="breadcrumb-item">
            <a href="#">Ola</a>
          </li>
          <li class="breadcrumb-item active"><?php echo $_SESSION['nome'];?></li>
        </ol>

        <!-- Icon Cards-->
        <div class="row">
       
	   <?php
       if(isset($_GET['id']) && !empty($_GET['id'])){
		   
		   $id_cliente = $_GET['id'];
		   
		  $buscar = "SELECT * FROM creditos WHERE id='$id_cliente'";
		  $ninguem_taco = mysqli_query($con,$buscar);
		  while($row = mysqli_fetch_assoc($ninguem_taco)){
			  
			  $cliente = $row['cliente'];
			  $valor_credito = $row['valor'];
			  $tipo = $row['tipo'];
			  $prest = $row['prestacoes'];
			  $taxas = $row['taxas'];
			  $v_prest = $row['v_prest'];
			  $juros = $row['juros'];
			  $total = $row['total'];
			  $data = $row['data'];
			  
			  ?>
			  
	
		
		<div class="container">
		 <div class="col-sm-4">
		  	<h2><font color='red'><?php echo $cliente;?></font></h2>
		  
		  <form action="" method="post">
		   <?php 
		   if(isset($_POST['submit_p'])){
		
		      $valor = $_POST['valor'];
		      $conta_capital = $_POST['conta_capital'];
		     
			  $user = $_SESSION['nome'];
		      $now = date('Y-m-d');
		      $hora = date("H:i", mktime(gmdate("H")+2, gmdate("i"), gmdate("s"), gmdate("m"), gmdate("d"), gmdate("Y")));
			  
			  
			  if($valor == $total){
				  
				  $juros_deduzir = $juros;
				  $valor_sem_juros = $valor_credito;
			  }else{
			  
			  $juros_deduzir = $valor * $taxas;
			  $valor_sem_juros = $valor - $juros_deduzir;
			  }
			  
		$buscar_conta2 = "SELECT * FROM contas WHERE conta='$conta_capital'";
		$dados_ligar2 = mysqli_query($con,$buscar_conta2);
		$nice_query2 = mysqli_fetch_assoc($dados_ligar2);
		
		$saldo_actual_conta = $nice_query2['saldo'];
		$saldo_2 = $saldo_actual_conta + $valor_sem_juros;
		

			  
			  
			  
			  
			  
			  
			  $jonasse = "SELECT * FROM transacoes WHERE caixa='$conta_capital' ORDER BY ID DESC LIMIT 1";
			  $levanta = mysqli_query($con,$jonasse);
			  $ultimo = mysqli_fetch_assoc($levanta);
					  
			 $saldo_transacoes = $ultimo['saldo'];
					  
			$saldo_novo = $saldo_transacoes + $valor_sem_juros;
			  
			  
			  
			  
			  
			  
			  
			  $verifica_pagamentos = "SELECT * FROM pagamentos WHERE id_pagamento='$id_cliente' ORDER BY ID DESC LIMIT 1";
			  $linkar = mysqli_query($con,$verifica_pagamentos);
			  $row = mysqli_fetch_assoc($linkar);
			 
			 $saldo = $row['saldo'];
			  $contar = mysqli_num_rows($linkar);
			  
			  if($contar == ""){
				  
				  $saldo_actual = $total - $valor;
			  }else{
				  $saldo_actual = $saldo - $valor;
				  
			  }
			  
			 if($valor < 0){
				  
				  echo "<script>alert('Este numero e Negativo. Adicione numeros acima de 0')</script>";
				  echo "<script>window.open('pay.php?id=$id_cliente','_self')</script>";
			  }else{
		     
			 $insere = "INSERT INTO pagamentos(nome_cliente,valor_pago,id_pagamento,data,saldo,status)
			 VALUES('$cliente','$valor','$id_cliente','$now','$saldo_actual','Pago')";
			 $pagamentos = mysqli_query($con,$insere);
			 
			 $tras = "INSERT INTO transacoes(descricao,saida,entrada,saldo,caixa,data,hora,user)
			 VALUES('Pagamento da prestacao de $cliente','0','$valor_sem_juros','$saldo_novo','$conta_capital','$now','$hora','$user')";
		      $query_it = mysqli_query($con,$tras);
			  
			  
			  if($query_it){
				  
				  $actualiza = "UPDATE contas SET saldo='$saldo_2' WHERE conta='$conta_capital'";
				  $men = mysqli_query($con,$actualiza);
				  
				  
				  echo "<script>alert('Credito de $cliente pago com sucesso')</script>";
				  echo "<script>window.open('pay.php?id=$id_cliente','_self')</script>";
				 }else{
					 
					echo "<script>alert('FALHA')</script>";
				  echo "<script>window.open('pay.php?id=$id_cliente','_self')</script>";
				 }
			    }
	           }
		             ?>
		   
		   <div class="form-group">
		    <label for="valor">Valor deposito</label>
			 <input type="number" name="valor" class="form-control" autocomplete="off" required/>
		   </div>
		   
		   <div class="form-group">
		    <label for="conta_capital">Conta capital</label>
			 <select required name="conta_capital" class="form-control">
			  <option></option>
			  <?php selectConta();?>
			 
			 </select>
		   </div>
		  
		  
		   
		   <div class="form-group">		    
			 <input type="submit" name="submit_p" class="btn btn-info btn-block" value="Pagar"/>
		   </div>
		  
		  
		  </form>
		 
		 
		 </div>
		
		</div>
		
		
		
			  
		  <?php }
		   
	   }

	   ?>
	   
        </div>

      </div>
	  
	  
	    
				<style>

 
}



div.absolute{
  position: absolute;
  top: 50px;
  right: 0;
  width: 500px;
  height: 100px;
 
}
</style>
			
			<table>
			 <thead>
			  <tr>
			  
			   <th>Valor pago</th>
			   <th>Saldo</th>
			   <th>Data</th>
			   <th>Recibo</th>
			      </tr>
			 </thead>
			 
			 <tbody>
			 <?php
			 
            $levar = "SELECT * FROM pagamentos WHERE id_pagamento='$id_cliente'";
			$conete = mysqli_query($con,$levar);
			while($row = mysqli_fetch_assoc($conete)){
			$id = $row['id'];
			$valor_pago = $row['valor_pago'];
			$saldo_pago = $row['saldo'];
			$data = $row['data'];
			$estado = $row['status'];
			$nome = $row['nome_cliente'];
			
			
			 
			 ?>
			 
			
			  <tr>
			 
			  <td><?php echo number_format($valor_pago,2,",",".");?></td>
			  <td><?php echo number_format($saldo_pago,2,",",".");?></td>
			  <td><?php echo $data;?></td>
			  <td><button type="button" class="btn btn-info btn-sm"><a href="recibo.php?recibo=<?php echo $id;?>" style="text-decoration:none; 
			  color:white;"><span>Recibo</span></a></button></td>

		  <!--FUNCAO DE EDITAR VALOR FINAL NOVO MODALLLL     -->

<td><button type="button" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#myModaledit">
  Edita valor
</button></td>

<!-- The Modal -->
<div class="modal" id="myModaledit">
  <div class="modal-dialog">
    <div class="modal-content">

      <!-- Modal Header -->
      <div class="modal-header">
        <h4 class="modal-title">EDITAR VALOR</h4>
        <button type="button" class="close" data-dismiss="modal">&times;</button>
      </div>

      <!-- Modal body -->
      <div class="modal-body">
        
		<div class="col-sm-8">
   
		        <form action="" method="post">
            <?php 
            if(isset($_POST['submit'])){
                
                $valor_editar = $_POST['valor'];
                
                $saldo_editar = $total - $valor_editar;
                
                $update = "UPDATE pagamentos SET valor_pago='$valor_editar',saldo='$saldo_editar' WHERE id='$id'";
                $query = mysqli_query($con,$update);
                
                if($query){
                    echo "<script>alert('saldo actualizado com sucesso')</script>";
                    echo "<script>window.open('pay.php?id=$id','_self')</script>";
                }else{
                    echo "<script>alert('ocorreu um erro')</script>";
                    echo "<script>window.open('pay.php','_self')</script>";
                }
            }
            ?>
            <div class="form-group">
                <input type="number" name="valor" value="<?php echo $valor_pago;?>" class="form-control" required/>
            </div>
            
            <div class="form-group">
                <input type="submit" name="submit" class="btn btn-info btn-sm" value="submit"/>
            </div>
        </form>
		</div>
		
		
      </div>

      <!-- Modal footer -->
      <div class="modal-footer">
        <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
      </div>

    </div>
  </div>
</div>
			 
			  </tr>
			<?php }?>
			 </tbody>
			 
			</table>
	  <br><br>
	  
	  		<!-- Button to Open the Modal -->
<button type="button" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#myModal">
  Recapitalizar
</button>

<!-- The Modal -->
<div class="modal" id="myModal">
  <div class="modal-dialog">
    <div class="modal-content">

      <!-- Modal Header -->
      <div class="modal-header">
        <h4 class="modal-title">Recapitalizar credito</h4>
        <button type="button" class="close" data-dismiss="modal">&times;</button>
      </div>

      <!-- Modal body -->
      <div class="modal-body">
        
		<div class="col-sm-8">
		 
		  <form action="recapitalizar.php?id=<?php echo $id_cliente;?>" method="post">
		   <div class="form-group">
		    <label for="valor">Valor</label>
			 <input type="text" name="valor" class="form-control" value="<?php echo $saldo_pago;?>" required/>
		   </div>
		   
		   <input type="hidden" name="nome" value="<?php echo $nome;?>"/>
		   
		   <div class="form-group">
		  <label for="tipo">Tipo de credito</label>
		  <select required name="tipo" class="form-control">
		   <option></option>		  	   
		    <option>Mensal</option>		   
		   <option>Bimensal</option>		   
		   <option>Trimestral</option>		   
		   </select>
		 </div>
		 
		 <div class="form-group">
		  <label for="prestacoes">Prestacoes</label>
		  <select required name="prestacoes" class="form-control">
		   <option></option>		  	   
		   <option>1</option>		   
		   <option>2</option>		   
		   <option>3</option>		   
		   <option>4</option>		   
		   <option>5</option>		   
		   <option>6</option>		   
		   <option>7</option>		   
		   <option>8</option>		   
		   <option>9</option>		   
		   <option>10</option>		   
		   <option>11</option>		   
		   <option>12</option>		   
		   	   
		   </select>
		 </div>
		 		 
		 <div class="form-group">
		  <label for="taxas">Taxa de juros</label>
		  <select required name="taxas" class="form-control">
		   <option></option>
		  	   
		   <option value='0.20'>20%</option>
		   <option value='0.25'>25%</option>
            <option value='0.3'>30%</option>
            <option value='0.35'>35%</option>
            <option value='0.40'>40%</option>
		 </div>	 
		 
		 <div class="form-group">
		    <label for="limite">Data Limite</label>
			 <input type="date" name="limite" class="form-control" required/>
		   </div>
		 
		
		 
		 <div class="form-group">
		  <input type="submit" name="submit" class="btn btn-info btn-block" value="Submeter"/>
		 </div>
		  
		  </form>
		
		</div>
		
		
      </div>

      <!-- Modal footer -->
      <div class="modal-footer">
        <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
      </div>

    </div>
  </div>
</div>
  
 
<button type="button" class="btn btn-success btn-sm"><a href="relatorio_cliente.php?id=<?php echo $id_cliente;?>"
style="text-decoration:none; color:white;"><span>Extracto</span></a></button>	
	
	 <br><br> 
	
<form action="finalizar.php" method="post">
 <input type="hidden" name="id" value="<?php echo $id_cliente;?>"/>
 <input type="hidden" name="valor" value="<?php echo $saldo_pago;?>"/>
 
 <input type="submit" name="submit" class="btn btn-info btn-sm" value="Finalizar"/>
</form>
	  
	  
	  
	  
	  
	  
	  
	  
	  
	<div class="absolute">
	    <div class="col-sm-8">
	        
	        <table class="table">
	            
	            <thead>
	                <tr>
	                    <th>Prestacao</th>
	                    <th>Valor</th>
	                    <th>Data pagamento</th>
	                </tr>
	            </thead>
	            
	            <tbody>
	                
	              <?php
	              		for ($x = 1; $x <= $prest; $x++) {
			
			
    
        $valor_credito_1 = $total/$prest;

		       $date = new DateTime($data);
                $date->modify('+30 day');
                $mudanca_frente_1 = $date->format('Y-m-d');
                
				
				$date = new DateTime($data);
                $date->modify('+60 day');
                $mudanca_frente_2 = $date->format('Y-m-d');
				
				$date = new DateTime($data);
                $date->modify('+90 day');
                $mudanca_frente_3 = $date->format('Y-m-d');
				
				
				 $date = new DateTime($data);
                $date->modify('+120 day');
                $mudanca_frente_4 = $date->format('Y-m-d');
                
				
				$date = new DateTime($data);
                $date->modify('+150 day');
                $mudanca_frente_5 = $date->format('Y-m-d');
				
				$date = new DateTime($data);
                $date->modify('+180 day');
                $mudanca_frente_6 = $date->format('Y-m-d');
				
				 $date = new DateTime($data);
                $date->modify('+210 day');
                $mudanca_frente_7 = $date->format('Y-m-d');
                
				
				$date = new DateTime($data);
                $date->modify('+240 day');
                $mudanca_frente_8 = $date->format('Y-m-d');
				
				$date = new DateTime($data);
                $date->modify('+270 day');
                $mudanca_frente_9 = $date->format('Y-m-d');
				
				 $date = new DateTime($data);
                $date->modify('+300 day');
                $mudanca_frente_10 = $date->format('Y-m-d');
                
				
				$date = new DateTime($data);
                $date->modify('+330 day');
                $mudanca_frente_11 = $date->format('Y-m-d');
				
				$date = new DateTime($data);
                $date->modify('+360 day');
                $mudanca_frente_12 = $date->format('Y-m-d');
				
		
		
		 if($x == 1){
			
			$nova_data = $mudanca_frente_2;
			
		}elseif($x == 2){
			
			$nova_data = $mudanca_frente_4;
		
		}elseif($x == 3){
			
			$nova_data = $mudanca_frente_6;
		}elseif($x == 4){
			
			$nova_data = $mudanca_frente_8;
			
		}elseif($x == 5){
			
			$nova_data = $mudanca_frente_10;
			
		}elseif($x == 6){
			
			$nova_data = $mudanca_frente_12;

		}else{
			
			$nova_data = 0;
		}
	              
	              
	              ?>
	                
	                
	                
	                <tr>
	                    <td>Prestacao <?php echo $x;?></td>
	                    <td><?php echo number_format($v_prest,2,",",".");?></td>
	                    <td><?php echo $nova_data;?></td>
	                </tr>
	                
	            <?php }
	            
	            ?>    
	                
	                
	                </tbody>
	                 <tr><td colspan="4" bgcolor="silver">Total: <?php echo number_format($total,2,",",".");?></td></tr>

	        </table>
	        
	        
	        
	    </div>
	    
	    
	    
	    
	</div>  
	  
	   
      <!-- /.container-fluid -->

      <!-- Sticky Footer -->
      <?php include "includes/footer.php";?>

    </div>
    <!-- /.content-wrapper -->

  </div>
  <!-- /#wrapper -->

  <!-- Scroll to Top Button-->
  <a class="scroll-to-top rounded" href="#page-top">
    <i class="fas fa-angle-up"></i>
  </a>

  <!-- Logout Modal-->
 <?php include "includes/logout_modal.php";?>

  <!-- Bootstrap core JavaScript-->
  <script src="vendor/jquery/jquery.min.js"></script>
  <script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>

  <!-- Core plugin JavaScript-->
  <script src="vendor/jquery-easing/jquery.easing.min.js"></script>

  <!-- Page level plugin JavaScript-->
  <script src="vendor/chart.js/Chart.min.js"></script>
  <script src="vendor/datatables/jquery.dataTables.js"></script>
  <script src="vendor/datatables/dataTables.bootstrap4.js"></script>

  <!-- Custom scripts for all pages-->
  <script src="js/sb-admin.min.js"></script>

  <!-- Demo scripts for this page-->
  <script src="js/demo/datatables-demo.js"></script>
  <script src="js/demo/chart-area-demo.js"></script>

</body>

</html>
