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

$employeeId = isset($_GET['employee_id']) ? intval($_GET['employee_id']) : null;
$dateFrom = isset($_GET['date_from']) ? $_GET['date_from'] : null;
$dateTo = isset($_GET['date_to']) ? $_GET['date_to'] : null;

// Get data for the chart (e.g., sales by day)
$query = "SELECT 
            DATE(tr.seller_date) AS sale_date,
            SUM(s.subtotal) AS total_amount
          FROM transaction_receipts tr
          JOIN sales s ON tr.trans_id = s.trans_id
          JOIN employees e ON tr.order_employee_id = e.employee_id
          WHERE e.is_active = 1";

if ($employeeId) {
    $query .= " AND e.employee_id = $employeeId";
}

if ($dateFrom && $dateTo) {
    $query .= " AND DATE(tr.seller_date) BETWEEN '$dateFrom' AND '$dateTo'";
} elseif ($dateFrom) {
    $query .= " AND DATE(tr.seller_date) >= '$dateFrom'";
} elseif ($dateTo) {
    $query .= " AND DATE(tr.seller_date) <= '$dateTo'";
}

$query .= " GROUP BY DATE(tr.seller_date) ORDER BY sale_date";

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

while ($row = $result->fetch_assoc()) {
    $labels[] = $row['sale_date'];
    $data[] = floatval($row['total_amount']);
}

$response = [
    'labels' => $labels,
    'data' => $data
];

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