<?php
require_once '../includes/config.php';
require_once '../includes/db.php';
require_once '../includes/functions.php';

// Check if user is admin
if (!isLoggedIn() || !isAdmin()) {
    header('Content-Type: application/json');
    echo json_encode(['error' => 'Unauthorized']);
    exit();
}

// Get the last check time from session, default to a long time ago if not set
$last_check_time = isset($_SESSION['last_order_check']) ? $_SESSION['last_order_check'] : '1970-01-01 00:00:00';

// Count new orders
$stmt = $pdo->prepare("SELECT COUNT(*) as count FROM orders WHERE order_date > ?");
$stmt->execute([$last_check_time]);
$new_orders_count = $stmt->fetch(PDO::FETCH_ASSOC)['count'];

header('Content-Type: application/json');
echo json_encode(['new_orders' => $new_orders_count]);
?>