<?php
include 'includes/config.php';

// Get sales data for the last 30 days
$sql = "
    SELECT DATE(date_sale) as sale_date, SUM(sale_qty) as total_sales
    FROM stock_sale
    WHERE date_sale >= CURDATE() - INTERVAL 30 DAY
    GROUP BY sale_date
    ORDER BY sale_date ASC
";

$stmt = $conn->prepare($sql);
$stmt->execute();
$salesData = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Prepare the data for JSON
$result = [];
foreach ($salesData as $row) {
    $result[] = [
        'date' => $row['sale_date'],
        'total_sales' => (float)$row['total_sales']
    ];
}

echo json_encode($result);
?>
