<?php
header('Content-Type: application/json');

$conn = new mysqli("localhost", "root", "", "stock");

$sql = "SELECT SUM(valor) AS total_entrada FROM caixa WHERE tipo='entrada', SUM(valor) as total_saida FROM caixa WHERE tipo='saida';


$result = $conn->query($sql);
$entradas = [];
$saidas = [];

while ($row = $result->fetch_assoc()) {
    $entradas[]   = (float)$row['total_entrada'];
    $saidas[] = (float)$row['total_saida'];
}

echo json_encode([
    'entradas'   => $entradas,
    'saidas' => $saidas
]);
?>