<?php
require_once './shikanisha.kts.php';

$date = isset($_GET['date']) ? $_GET['date'] : date('Y-m-d');
$shiftId = isset($_GET['shift_id']) ? intval($_GET['shift_id']) : null;

$query = "SELECT 
            s.product_name,
            s.category,
            SUM(s.quantity) AS quantity,
            SUM(s.subtotal) AS subtotal
          FROM sales s
          JOIN transaction_receipts tr ON s.trans_id = tr.trans_id
          WHERE DATE(tr.seller_date) = '$date'";

if ($shiftId) {
    $query .= " AND tr.shift_id = $shiftId";
}

$query .= " GROUP BY s.product_name, s.category ORDER BY subtotal DESC LIMIT 10";

$result = $conn->query($query);
$data = [];

while ($row = $result->fetch_assoc()) {
    $data[] = $row;
}

header('Content-Type: application/json');
echo json_encode($data);
?>