<?php
include 'includes/auth.php';
include 'db.php';

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

// Get POST dates
$from = $_POST['from'] ?? '';
$to   = $_POST['to'] ?? '';

// Default: show today's productions
if(empty($from)) $from = date('Y-m-d');
if(empty($to))   $to   = date('Y-m-d');

// Escape inputs
$from = $conn->real_escape_string($from);
$to   = $conn->real_escape_string($to);

// Fetch productions within date range
$sql = "
    SELECT * 
    FROM perishable_production 
    WHERE DATE(created_at) BETWEEN '$from' AND '$to'
    ORDER BY created_at DESC
";

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

while($row = $res->fetch_assoc()){
    $data[] = [
        'id' => $row['id'],
        'created_by' => $row['created_by'],
        'number_of_people' => $row['number_of_people'],
        'created_at' => $row['created_at']
    ];
}

echo json_encode($data);