<?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>
		
		<!-- COMECA AQUI O Reembolso E Desembolso-->
	
<?php 
if($_SESSION['nivel'] ==1 || $_SESSION['nivel']==2){
?>
	<!-- Button to Open the Modal -->
<button type="button" class="btn btn-danger btn-sm" data-toggle="modal" data-target="#desembolso">
 Total Desembolso
</button>

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

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

      <!-- Modal body -->
      <div class="modal-body">
      <?php $loan->cancelLoan();?>
	 
	  
	  <table class="table">
	   <thead>
	    <tr>
		 <th>Cliente</th>
		 <th>Valor</th>
		 <th>Juros</th>
		 <th>Total</th>
		</tr>
	   </thead>
	   
	   <tbody>
	    <?php 
	  $tira_credito = "SELECT * FROM creditos WHERE status='pendente' AND tipo='Mensal'";
	  $naguira = mysqli_query($con,$tira_credito);
	  while($row = mysqli_fetch_assoc($naguira)){
		  
		  $cliente = $row['cliente'];
		  $valor = $row['valor'];
		  $juros = $row['juros'];
		  $total = $row['total'];		  
	  	  ?>
	    <tr>
		 <td><?php echo $cliente;?></td>
		 <td><?php echo $valor;?></td>
		 <td><?php echo $juros;?></td>
		 <td><?php echo $total;?></td>
		</tr>
		
		<?php }?>
	   </tbody>
	   
	   <?php 
	 $stock = mysqli_query($con, "SELECT SUM(valor) as soma  FROM creditos WHERE status='pendente' AND tipo='Mensal'");
 
  while($total = mysqli_fetch_array($stock)){
	  
	  $stock_total = $total['soma'];
  }
  
   $stock1 = mysqli_query($con, "SELECT SUM(juros) as soma  FROM creditos WHERE status='pendente' AND tipo='Mensal'");
 
  while($total1 = mysqli_fetch_array($stock1)){
	  
	  $stock_total1 = $total1['soma'];
  }
  
   $stock2 = mysqli_query($con, "SELECT SUM(total) as soma  FROM creditos WHERE status='pendente' AND tipo='Mensal'");
 
  while($total2 = mysqli_fetch_array($stock2)){
	  
	  $stock_total2 = $total2['soma'];
  }
	   
	   ?>
	   
	   
	   <td colspan="2"><b>Valor:</b><?php echo number_format($stock_total,2,",",".");?></td>
	   <td><b>Juros:</b><?php echo number_format($stock_total1,2,",",".");?></td>
	   <td><b>Total:</b><?php echo number_format($stock_total2,2,",",".");?></td>
	  </table>
	  
	  
      </div>

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

    </div>
  </div>
</div>

<!-- Button to Open the Modal -->
<button type="button" class="btn btn-success btn-sm" data-toggle="modal" data-target="#reembolso">
  Prev. Reembolso
</button>

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

      <!-- Modal Header -->
      <div class="modal-header">
        <h4 class="modal-title">Valor previsto de reembolso mensal</h4>
        <button type="button" class="close" data-dismiss="modal">&times;</button>
      </div>

      <!-- Modal body -->
      <div class="modal-body">
       
	 <table class="table">
	   <thead>
	    <tr>
		 <th>Cliente</th>
		 <th>Valor previsto</th>
		</tr>
	   </thead>
	   
	   <tbody>
	    <?php 
	  $tira_credito = "SELECT * FROM creditos WHERE status='pendente' AND tipo='Mensal'";
	  $naguira = mysqli_query($con,$tira_credito);
	  while($row = mysqli_fetch_assoc($naguira)){
		  
		  $cliente = $row['cliente'];
		   $prest = $row['v_prest'];		  
	  	  ?>
	    <tr>
		 <td><?php echo $cliente;?></td>
		 <td><?php echo number_format($prest,2,",",".");?></td>
		 </tr>
		
		<?php }?>
	   </tbody>
	   
	   <?php 
	 $stock = mysqli_query($con, "SELECT SUM(v_prest) as soma  FROM creditos WHERE status='pendente' AND tipo='Mensal'");
 
  while($total = mysqli_fetch_array($stock)){
	  
	  $stock_total = $total['soma'];
  }
  
  
	   
	   ?>
	   
	   
	   <td colspan="2"><b>Total previsto mensal:</b><?php echo number_format($stock_total,2,",",".");?></td>
	   
	  </table>
	   
	   
      </div>

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

    </div>
  </div>
</div>

<?php }else{
	
	echo "";
}?>

<!-- TERMINA AQUI O Desembolso E Reembolso-->
		
	<button type="button" class="btn btn-info btn-sm"><a href="juros_de_mora.php" style="text-decoration:none; color:white;"><span>Juros de Mora</span></a></button>
	
	
<!-- Button to Open the Modal -->
<button type="button" class="btn btn-success btn-sm" data-toggle="modal" data-target="#reciboUnico">
  Recibo Unico
</button>

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

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

      <!-- Modal body -->
      <div class="modal-body">
        
	   <form action="recibo_unico.php" method="POST">
	       <div class="form-group">
	           <label for="nome">Nome do cliente</label>
	           <select required name="cliente" class="form-control">
	               <option></option>
	                              <?php 
	               
	               $buscar = "SELECT * FROM pagamentos WHERE status='Finalizado'";
	               $query = mysqli_query($con,$buscar);
	               while($row = mysqli_fetch_assoc($query)){
	                   
	                   $nome = $row['nome_cliente'];
	                   $id_pagamento = $row['id_pagamento'];
	                   
	                   echo "<option value='$id_pagamento'>$nome ||CREDITO=$id_pagamento</option>";
	                }
	               ?>

	           </select>
	       </div>
	       <div class="form-group">
	           <input type="submit" name="submit" class="btn btn-info btn-sm" value="Submit">
	       </div>
	   </form>
	   
      </div>

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

    </div>
  </div>
</div>


<!-- Button to Open the Modal -->
<button type="button" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#quitacao">
  Carta de Quitação
</button>

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

      <!-- Modal Header -->
      <div class="modal-header">
        <h4 class="modal-title">Quitação da divida</h4>
        <button type="button" class="close" data-dismiss="modal">&times;</button>
      </div>

      <!-- Modal body -->
      <div class="modal-body">
        	   <form action="quitacao.php" method="POST">
	       <div class="form-group">
	           <label for="nome">Nome do cliente</label>
	           <select required name="cliente" class="form-control">
	               <option></option>
	                              <?php 
	               
	               $buscar = "SELECT * FROM pagamentos WHERE status='Finalizado'";
	               $query = mysqli_query($con,$buscar);
	               while($row = mysqli_fetch_assoc($query)){
	                   
	                   $nome = $row['nome_cliente'];
	                   $id_pagamento = $row['id_pagamento'];
	                   
	                   echo "<option value='$id_pagamento'>$nome ||CREDITO=$id_pagamento</option>";
	                }
	               ?>

	           </select>
	       </div>
	       <div class="form-group">
	           <input type="submit" name="submit" class="btn btn-info btn-sm" value="Submit">
	       </div>
	   </form>
	   
	   
      </div>

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

    </div>
  </div>
</div>

		
		<hr>	

        <table class="table">
	 <thead>
	  <tr>
	   <th>Nome do cliente</th>
	   <th>Tipo de credito</th>
	   <th>Valor</th>
	   <th>Juros</th>
	   <th>Total</th>
	   <th>Data</th>
	   <th>Contrato</th>
	   <th>Amortizar</th>
	   <th>Dias</th>
	   <th>J.Mora</th>
	   <th>Cancelar</th>
	   

	  </tr>
	 </thead>
	 
	 <tbody>
	 <?php 
	 if($_SESSION['nivel'] ==1 || $_SESSION['nivel']==2){
	 
	 
	 $busca_trato = "SELECT * FROM creditos WHERE tipo='Mensal' AND status = 'pendente' OR status='reforco'";
	 $confirma = mysqli_query($con,$busca_trato);
	 while($row = mysqli_fetch_assoc($confirma)){
		 
		 $id = $row['id'];
		 $nome = $row['cliente'];
		 $valor = $row['valor'];
		 $tipo = $row['tipo'];
		 $juros = $row['juros'];
		 $total = $row['total'];
		 $siku = $row['previsao'];
		 $status = $row['status'];
		 $tipo_credito = $row['tipo_credito'];
		 
		 
		 
		 $now = date('Y-m-d');
		 
$date1=date_create($siku);
$date2=date_create($now);
$diff=date_diff($date1,$date2);
		 ?>
	 
	  <tr>
	   <td><?php echo $nome;?></td>
	   <td><?php echo $tipo;?></td>
	   <td><?php echo number_format($valor,2,",",".");?></td>
	   <td><?php echo number_format($juros,2,",",".");?></td>
	   <td><?php echo number_format($total,2,",",".");?></td>
	   <td><?php echo $siku;?></td>
	   
	   <?php if($tipo_credito =='Garantia Possessoria'){
	   ?>
	   <td><button type="button" class="btn btn-warning btn-sm"><a href="contractoGarantia.php?id=<?php echo $id;?>"
	   style="text-decoration:none; color:white;"><span>C.Garantia</span></a></button></td>
	   
	  <?php }else{
	  ?>
	  <td><button type="button" class="btn btn-info btn-sm"><a href="contracto.php?id=<?php echo $id;?>"
	   style="text-decoration:none; color:white;"><span>Contracto</span></a></button></td>
	  
	 <?php } ?>
	   
	   
	  
      <?php 
	  if($_SESSION['nivel'] == 1){
	  
	  ?>

	  <td><button type="button" class="btn btn-success btn-sm"><a href="pay.php?id=<?php echo $id;?>"
	   style="text-decoration:none; color:white;"><span>Pagar</span></a></button></td>
	  
	  
	  	  
	  	   <!-- Button to Open the Modal INICIO  -->
	  	   <?php 
	  	   if($date2 > $date1){
	  	       ?>
	  	        <td><font color='red'><?php echo $diff->format("%a");?></font></td>
	  	  <?php  }else{
	  	      echo "<td>N/A</td>";
	  	  }
	  	   ?>
	  	  
	  	   <?php 
	  	   $juros_mora = $valor * 0.02;
	  	   
	  	   $somaDias = $juros_mora * $diff->format("%a");
	  	   if($date2 > $date1){
	  	       ?>
	  	        <td><font color='red'><?php echo number_format($somaDias,2,",",".");?></font></td>
	  	  <?php  }else{
	  	      echo "<td>N/A</td>";
	  	  }
	  	   ?>
	  	  

			<td><button type="button" class="btn btn-danger btn-sm" data-toggle="modal" data-target="#exampleModalLong1<?php echo $id;?>">
  <span><i class="fa fa-trash" aria-hidden="true"></i></span>
</button></td>

<!-- Modal -->
<div class="modal fade" id="exampleModalLong1<?php echo $id;?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLongTitle" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLongTitle"></h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
		
        <p><h3>Deseja eliminar <font color="red"><?php echo $nome;?>?</font></h3></p>
        <button type="button" class="btn btn-danger btn-sm"><a href="mensais.php?cancelar=<?php echo $id;?>" style="text-decoration:none; color:white"><span>Confirmar</span></a></button>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary btn-sm" data-dismiss="modal">Cancelar</button>
      </div>
    </div>
  </div>
</div>

	  
	  
	  
	  
	  
	  
	  <?php }else{
		  ?>
		  <td><button type="button" class="btn btn-info btn-sm"><a href="pagar_2.php?id=<?php echo $id;?>"
	   style="text-decoration:none; color:white;"><span>Lançar pagamento</span></a></button></td>
		  
		  
	  <?php } ?>
	  
	  
	  </tr>
	  
	 <?php }
	 
	 
	 
	 }else{
		 
		 $gestor = $_SESSION['nome'];
		 
	 $busca_trato = "SELECT * FROM creditos WHERE tipo='Mensal' AND status = 'pendente' AND vendedor='$gestor'";
	 $confirma = mysqli_query($con,$busca_trato);
	 while($row = mysqli_fetch_assoc($confirma)){
		 
		 $id = $row['id'];
		 $nome = $row['cliente'];
		 $valor = $row['valor'];
		 $tipo = $row['tipo'];
		 $juros = $row['juros'];
		 $total = $row['total'];
		 $siku = $row['data'];	
		 ?>
	 
	  <tr>
	   <td><?php echo $nome;?></td>
	   <td><?php echo $tipo;?></td>
	   <td><?php echo $valor;?></td>
	   <td><?php echo $juros;?></td>
	   <td><?php echo $total;?></td>
	   <td><?php echo $siku;?></td>
	   <td><button type="button" class="btn btn-info btn-sm"><a href="contracto.php?id=<?php echo $id;?>"
	   style="text-decoration:none; color:white;"><span>Contrato</span></a></button></td>
	   
	   
	   
	   
	   <?php 
	  if($_SESSION['nivel'] == 1 || $_SESSION['nivel'] == 2){
	  
	  ?>

	  <td><button type="button" class="btn btn-success btn-sm"><a href="pay.php?id=<?php echo $id;?>"
	   style="text-decoration:none; color:white;"><span>Pagar</span></a></button></td>
	  
	  <?php }else{
		  ?>
		  <td><button type="button" class="btn btn-info btn-sm"><a href="pagar_2.php?id=<?php echo $id;?>"
	   style="text-decoration:none; color:white;"><span>Lançar pagamento</span></a></button></td>
		  
		  
	  <?php } ?>
	  </tr>
	  
	 <?php }
		 
		 
		 
	 }
	 
	 
	 
	 ?>
	 </tbody>
	
	</table>
      



        

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