<?php
// require_once "config/init.php";
$inicio = $_GET['inicio'] ?? date('Y-m-01');
$fim = $_GET['fim'] ?? date('Y-m-d');

?>
<?php require "includes/header.php";?>
<?php include "includes/navbar.php";?>

    <!-- Page Wrapper -->
    <div id="wrapper">

        <!-- Sidebar -->
        <?php include "includes/sidebar.php";?>
        <!-- End of Sidebar -->

        <!-- Content Wrapper -->
        <div id="content-wrapper" class="d-flex flex-column">

            <!-- Main Content -->
            <div id="content">

                <!-- Topbar -->
                <!-- End of Topbar -->

                <!-- Begin Page Content -->
                <div class="container-fluid">


                    <!-- Content Row -->

                    <div class="row">
                <div class="container">
    <h2>📋 Balancete</h2>
    <form class="row g-3 mb-3" method="get">
        <div class="col-auto">
            <input type="date" name="inicio" class="form-control" value="<?= $inicio ?>">
        </div>
        <div class="col-auto">
            <input type="date" name="fim" class="form-control" value="<?= $fim ?>">
        </div>
        <div class="col-auto">
            <button class="btn btn-primary btn-sm">Filtrar</button>
        </div>
        <div class="col-auto">
            <a href="balancete_pdf.php?inicio=<?= $inicio ?>&fim=<?= $fim ?>" target="_blank" class="btn btn-info btn-sm">👁 Visualizar PDF</a>
        </div>
    </form>

    <table class="table table-bordered table-striped">
        <thead class="table-dark">
            <tr>
                <th>Data</th>
                <th>Tipo</th>
                <th>Valor</th>
                <th>Saldo Acumulado</th>
            </tr>
        </thead>
        <tbody>
            <?php 
            $saldo_acumulado = 0;
            $sql = $novaVenda->MostraBalancete($inicio,$fim);
            while($row = $database->fetch_assoc($sql)){
                $tipo = $row['tipo'];
                $valor = $row['valor'];

                if($tipo == 'entrada'){
                    $saldo_acumulado = $saldo_acumulado + $valor;
                }else{
                    $saldo_acumulado = $saldo_acumulado - $valor;
                }
            ?>
            <tr>
                <td><?= $row['data'] ?></td>
                <td><?= ucfirst($row['tipo']) ?></td>
                <td><?= number_format($row['valor'],2,',','.') ?> MT</td>
                <td><?= number_format($saldo_acumulado,2,',','.') ?> MT</td>
            </tr>
            <?php } ?>
        </tbody>
    </table>
</div>      
    
</div> 

       
            <!-- End of Main Content -->

            <!-- Footer -->
          <?php include "includes/footer.php";?>
            <!-- End of Footer -->

        </div>
        <!-- End of Content Wrapper -->

    </div>
    <!-- End of Page 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";?>

</body>

</html>










