<?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'];
			  
			  ?>
			  
		<h2><font color='green'>Plano de amortização de</font> <font color='red'><?php echo $cliente;?></font></h2>
		
		<div class="container">
		 <div class="col-sm-4">
		  
		  <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>
	  
	  

			
			<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>
 
            </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>
  


<!--FIM DE EDITAR VALOR FINAL-->
		  
			 
			  </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="cliente">Nome do cliente</label>
		  <select required name="cliente" class="form-control">
		   <option><?php echo $cliente;?></option>
		     </select>
		 </div>
		 
		 <div class="form-group">
		  <label for="valor">Valor</label>
		  <input type="text" name="valor" class="form-control" value="<?php echo $saldo_pago;?>" autocomplete="off" required/>
		 </div>

		<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>		   
		   <option>Semestral</option>
		   <option>Anual</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="taxa">Taxa de juros</label>
		  <select required name="taxas" class="form-control">
		   <option></option>
		  	
            <option value='0.10'>10%</option>	
            <option value='0.12'>12%</option>
            <option value='0.15'>15%</option>
            <option value='0.18'>18%</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>
           			   
		  		   
		  </select>
		 </div>	 
		 
		  <div class="form-group">
		  <label for="previsao">Data Prevista</label>
		  <input type="date" name="previsao" class="form-control" required/>
		 </div>
		 
		 <div class="form-group">
		  <label for="previsao">Garantias</label>
		  <input type="text" name="garantias" class="form-control" required/>
		 </div>
		 
		 <div class="form-group">
		  <label for="previsao">Valor estimado</label>
		  <input type="number" name="estimado" 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>	

<!-- Button trigger modal -->
<button type="button" class="btn btn-info btn-sm" data-toggle="modal" data-target="#exampleModal">
  Pagamento antecipado
</button>

<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        
        
        <div class="container">
		 <div class="col-sm-10">
		  
		  <form action="" method="post">
		      		   <div class="form-group">
		    <label for="valor">Valor Total</label>
			 <input type="number" name="valor" class="form-control" autocomplete="off" required/>
		   </div>
		   
		   	 <div class="form-group">
		  <label for="taxa">Taxa de juros</label>
		  <select required name="taxas" class="form-control">
		   <option></option>
		  	
            <option value='0.10'>10%</option>	
            <option value='0.12'>12%</option>
            <option value='0.15'>15%</option>
            <option value='0.18'>18%</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>
           			   
		  		   
		  </select>
		 </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_pay" class="btn btn-info btn-block" value="Pagar"/>
		   </div>
		  
		  
		  </form>
		 
		 
		 </div>
		
		</div>
		      
		      
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Fechar</button>
      </div>
    </div>
  </div>
</div>
	
	 <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">
	          <h3><font color="green">Lista de créditos</font></h3>
	          
	          <table class="table">
	              <thead>
	                  <tr>
	                  <th>Item</th>
	                  <th>Valor</th>
	                    <th>Total</th>
	                  <th>Tipo</th>
	                
	                 
	                  </tr>
	              </thead>
	              <tbody>
	                  
	                  <?php 
	                  $item=1;
	                  
	 $stock = mysqli_query($con, "SELECT SUM(total) as soma  FROM creditos WHERE cliente='$cliente' AND status !='Pago' AND status !='Recapitalizado'");
 
  while($total = mysqli_fetch_array($stock)){
	  
	  $stock_total = $total['soma'];
  }
	                  
	                  
	                  $sql = "SELECT * FROM creditos WHERE cliente='$cliente' AND status !='Pago' AND status !='Recapitalizado'";
	                  $quer_it = mysqli_query($con,$sql);
	                  while($row_all = mysqli_fetch_assoc($quer_it)){
	                      $reforco = $row_all['status'];
	                      $total = $row_all['total'];
	                                      ?>
	                  <tr>
	                      <td><?php echo $item++;?></td>
	                      <td><?php echo number_format($row_all['valor'],2,",",".");?></td>
	                         <td><?php echo number_format($row_all['total'],2,",",".");?></td>
	                     
	                     
	                     <?php if($reforco =='reforco'){
	                         ?>
	                         <td><font color='red'>Reforço</font></td>
	                     <?php }else{
	                     ?>
	                     <td><font color='green'>Crédito</font></td>
	                     <?php }?>
	                     
	                  </tr>
	                  <?php 
	                      
	                  }
	                  
	                
	                  ?>
	                  
	              </tbody>
	              
	              
	              
	          </table>
	          <tr><td><b>Somatorio:</b><?php echo number_format($stock_total,2,",",".");?></td></tr>
	          <tr><td><button type="button" class="btn btn-info btn-sm"><span>Pagar Todos</span></button></td></tr>
	          
	      </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";?>

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