<?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">
       
	   
	   <div class="container">
	       <div class="col-sm-10">
	           <h3><font color='red'>Creditos finalizados</font></h3>
	           <table class="table">
	               <thead>
	                   <tr>
	                       <th>Id</th>
	                       <th>Nome</th>
	                       <th>Capital</th>
	                       <th>Juros</th>
	                       <th>Total</th>
	                       <th>Data financiamento</th>
	                   </tr>
	               </thead>
	               <tbody>
	                   <?php
	                   $item =1;
                      $totalValor = 0;
                     $totalJuros = 0;
                     $totalGeral = 0;

	                   $creditos = $database->query("SELECT cr.*,cli.* FROM creditos cr INNER JOIN clientes cli ON cr.id_cliente=cli.id_cliente WHERE cr.status='Pago'");
	                   while($row = $database->fetch_assoc($creditos)){

                     // Converte os valores vindos do banco de dados para float para evitar erros matemáticos
                         $v_valor = (float)$row['valor'];
                         $v_juros = (float)$row['juros'];
                         $v_total = (float)$row['total'];
                         // 2. Somar os valores da linha atual ao total acumulado
                         $totalValor += $v_valor;
                         $totalJuros += $v_juros;
                         $totalGeral += $v_total;
	                   ?>
	                   <tr>
	                       <td><?php echo $item++;?></td>
	                       <td><?php echo $row['nome_cliente'];?></td>
	                       <td><?php echo number_format((float)$v_valor,2,",",".");?></td>
	                       <td><?php echo number_format((float)$v_juros,2,",",".");?></td>
	                       	<td><?php echo number_format((float)$v_total,2,",",".");?></td>
	                       <td><?php echo $row['data'];?></td>

	                   </tr>
	                   <?php } ?>
	               </tbody>
                 <tfoot>
                     <tr style="font-weight: bold; background-color: #f8f9fa; border-top: 2px solid #dee2e6;">
                         <td colspan="2" class="text-right">Total Geral:</td>
                         <td><?php echo number_format($totalValor, 2, ",", "."); ?></td>
                         <td><?php echo number_format($totalJuros, 2, ",", "."); ?></td>
                         <td><?php echo number_format($totalGeral, 2, ",", "."); ?></td>
                         <td></td> <!-- Coluna da data vazia no rodapé -->
                     </tr>
                 </tfoot>
	           </table>
	       </div>
	   </div>
        
        
        </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>
