<?php
// update_people_count.php
include 'db.php';

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

// ===== Get POST data safely =====
$id = isset($_POST['id']) ? intval($_POST['id']) : 0;
$people_count = isset($_POST['people_count']) ? intval($_POST['people_count']) : 0;

// ===== Validate =====
if ($id <= 0) {
    echo json_encode(['status' => 'error', 'message' => 'Invalid ID']);
    exit;
}

if ($people_count < 0) {
    echo json_encode(['status' => 'error', 'message' => 'People count cannot be negative']);
    exit;
}

// ===== Update database =====
$stmt = $conn->prepare("UPDATE production_plan SET people_count = ? WHERE id = ?");
$stmt->bind_param("ii", $people_count, $id);

if ($stmt->execute()) {
    echo json_encode(['status' => 'success', 'message' => 'People count updated']);
} else {
    echo json_encode(['status' => 'error', 'message' => 'Database update failed']);
}

$stmt->close();
$conn->close();