<?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>📊 Relatório de Vendas</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>
                            
                            </form>
                         



                        <table class="table table-bordered table-striped">
                            <thead class="table-dark">
                                <tr>
                                    <th>Item</th>
                                    <th>Producto</th>
                                    <th>Qtd</th>
                                    <th>Total</th>
                                    <th>Lucro</th>
                                    <th>Data</th>
                                </tr>
                            </thead>
                            <tbody> 
                                <?php 
                               
                             $item = 1;
                                $cashFlow = $database->query("SELECT * FROM vendas WHERE salesDate BETWEEN '$inicio' AND '$fim'");
                                while($row = $database->fetch_assoc($cashFlow)){

                                ?>                             
                                    <tr>
                                        <td><?php echo $item++;?></td>
                                        <td><?php echo $row['productName'];?></td>
                                        <td><?php echo $row['productQtd'];?></td>
                                        <td><?php echo number_format($row['productTotal'],2,",",".");?></td>
                                        <td><?php echo number_format($row['productLucro'],2,",",".");?></td>
                                        <td><?php echo $row['salesDate'];?></td>
                                    </tr>
                                 <?php }
                                 $count = mysqli_num_rows($cashFlow);
                                 
                                 if($count == 0){
                                 ?>
                                     <tr>
                                        <td colspan="5" class="text-center">Nenhum lançamento encontrado neste período</td>
                                    </tr>
                                    <?php }?>
                            </tbody>
                            <tfoot>
                                <tr>
                                    <th colspan="2">Total Vendido no Periodo</th>
                                    <th><?php echo  $novaVenda->SomaVendas($inicio,$fim);?> MT</th>
                                    <th>Lucro</th>
                                    <th><?php echo  $novaVenda->SomaLucro($inicio,$fim);?> MT</th>
                                    <td><button type="button" class="btn btn-info btn-sm"><a href="imprimir.php?inicio=<?=$inicio;?>&&fim=<?=$fim;?>" target="_blank" style="text-decoration:none; color:white">Imprimir</a></button></td>
                                </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";?>

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