<?php
require_once 'config/init.php';


$data_inicio = $_GET['inicio'] ?? date('Y-m-01');
$data_fim = $_GET['fim'] ?? date('Y-m-d');

?>
<!DOCTYPE html>
<html>
<head>
    <title>Fluxo de Caixa</title>
<link href="vendor/fontawesome-free/css/all.min.css" rel="stylesheet" type="text/css">
    <link
        href="https://fonts.googleapis.com/css?family=Nunito:200,200i,300,300i,400,400i,600,600i,700,700i,800,800i,900,900i"
        rel="stylesheet">

    <!-- Custom styles for this template-->
    <link href="css/sb-admin-2.min.css" rel="stylesheet">
    
  <!-- Page level plugin CSS-->
  <link href="vendor/datatables/dataTables.bootstrap4.css" rel="stylesheet">
    <style>
        body{padding:20px;}
    </style>
</head>
<body>
<div class="container">
    <div class="d-flex justify-content-between mb-4">
        <h3>Fluxo de Caixa</h3>
        <div>
            <button onclick="window.print()" class="btn btn-primary btn-sm">🖨 Imprimir</button>
        </div>
    </div>
    <table class="table">
        <thead class="table-dark">
            <tr>
            <th>Data</th>
            <th>Descricao</th>
            <th>Tipo</th>
            <th>Valor</th></tr>
        </thead>
        <tbody>
       <?php 
       $sql = $database->query("SELECT * FROM caixa WHERE data BETWEEN '$data_inicio' AND '$data_fim'");
       while($caixa = $database->fetch_assoc($sql)){
        ?>
        <tr>
            <td><?php echo $caixa['data'];?></td>
            <td><?php echo $caixa['descricao'];?></td>
            <td><?php echo $caixa['tipo'];?></td>
            <td><?php echo number_format($caixa['valor'],2,",",".");?></td>
        </tr>
       
       
       <?php } ?>
        </tbody>
    </table>
    <?php 
    $soma = $database->query("SELECT SUM(valor) as soma FROM caixa WHERE data BETWEEN '$data_inicio' AND '$data_fim' AND tipo='entrada'");
    $confirmaSoma = $database->fetch_assoc($soma);
    $somaEntrada = $confirmaSoma['soma'];

    $soma1 = $database->query("SELECT SUM(valor) as soma FROM caixa WHERE data BETWEEN '$data_inicio' AND '$data_fim' AND tipo='saida'");
    $confirmaSoma1 = $database->fetch_assoc($soma1);
    $somaSaida = $confirmaSoma1['soma'];

    $somaTotal = $somaEntrada - $somaSaida;
    ?>
    <tr>
        <td>Total entradas: <b> <?php echo number_format($somaEntrada,2,",",".");?></b> </td>
         <td>Total saidas: <b> <?php echo number_format($somaSaida,2,",",".");?></b> </td>
           <td>Saldo: <b> <?php echo number_format($somaTotal,2,",",".");?></b> </td>
    </tr>

   
</div>
</body>
</html>
