<?php 
// require_once "config/init.php";
$inicio = $_GET['inicio'] ?? date('Y-m-01');
$fim = $_GET['fim'] ?? date('Y-m-d');
?>
<?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">                      
                      
                        <h2>📊 Fluxo de Caixa</h2>
                        <form class="row g-3 mb-3" method="get">

                            <div class="col-auto">
                                <input type="date" name="inicio" class="form-control" value="<?= $inicio?>" required/>
                            </div>
                            <div class="col-auto">
                                <input type="date" name="fim" class="form-control" value="<?= $fim?>" required>
                            </div>
                            <div class="col-auto">
                                <button type="submit" name="submit" class="btn btn-primary btn-sm">Filtrar</button>
                            </div>
                             <div class="col-auto">
                                <a href="fluxo_pdf.php?inicio=<?= $inicio ?>&fim=<?= $fim ?>" target="_blank" class="btn btn-info btn-sm">👁 Visualizar PDF</a>
                            </div>
                            </form>
                         

<button type="button" class="btn btn-success btn-sm" data-toggle="modal" data-target="#exampleModal">
  <i class="fas fa-fw fa-plus"></i> Lançar Despesas
</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">Despesas do caixa</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" enctype="multipart/form-data">
            <?php $novaVenda->CashFlow();?>
          <div class="form-group">
			  <label for="descricao">Descricao</label>
			  <input type="text" name="descricao" class="form-control" autocomplete="off" required/>
			 </div>
			 
			 
			 <div class="form-group">
			  <label for="productQtd">Valor</label>
			  <input type="number" name="valor" class="form-control" autocomplete="off" required/>
			 </div>
       		
			 <div class="form-group">
			  <input type="submit" name="enviar" class="btn btn-primary btn-block" value="Enviar"/>
			 </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>
                        <table class="table table-bordered table-striped">
                            <thead class="table-dark">
                                <tr>
                                    <th>Data</th>
                                    <th>Tipo</th>
                                    <th>Valor</th>
                                </tr>
                            </thead>
                            <tbody> 
                                <?php 
                                                              $cashFlow = $database->query("SELECT * FROM caixa WHERE data BETWEEN '$inicio' AND '$fim'");
                                while($row = $database->fetch_assoc($cashFlow)){

                                ?>                             
                                    <tr>
                                        <td><?php echo $row['data'];?></td>
                                        <td><?php echo ucfirst($row['tipo']);?></td>
                                        <td><?php echo number_format($row['valor'],2,",",".");?> Mts</td>
                                    </tr>
                                 <?php }
                                 $count = mysqli_num_rows($cashFlow);
                                
                                 if($count == 0){
                                 ?>
                                     <tr>
                                        <td colspan="3" class="text-center">Nenhum lançamento encontrado neste período</td>
                                    </tr>
                                    <?php }?>
                            </tbody>
                            <tfoot>
                                <tr>
                                    <th colspan="2">Total Entradas</th>
                                    <th><?php echo  $novaVenda->SomaEntrada($inicio,$fim);?> MT</th>
                                </tr>
                                <tr>
                                    <th colspan="2">Total Saídas</th>
                                    <th><?php echo $novaVenda->SomaSaida($inicio,$fim);?> MT</th>
                                </tr>
                                <tr>
                                    <th colspan="2">Saldo</th>
                                    <th>

                                    <?php echo  $novaVenda->SaldoCaixa($inicio,$fim);                            
                                    ?> MT
                                    </th>
                                </tr>
                            </tfoot>
                        </table>
                    </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";?>
 <?php include "includes/sweatscripFooter.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>
