<?php
require_once __DIR__.'/../config/config.php';
require_user();
$U = current_user();
header('Content-Type: application/json');

// mark seen
if (($_GET['mark'] ?? '') !== '') {
    $id = (int)$_GET['mark'];
    $pdo->prepare("UPDATE orders SET user_notified=1 WHERE id=? AND user_id=?")->execute([$id, $U['id']]);
    echo json_encode(['ok'=>true]); exit;
}

$rows = $pdo->prepare("SELECT id, status, cancel_reason, service_id FROM orders
                       WHERE user_id=? AND user_notified=0 AND status IN ('completed','cancelled')
                       ORDER BY id DESC LIMIT 5");
$rows->execute([$U['id']]);
echo json_encode(['items' => $rows->fetchAll()], JSON_UNESCAPED_UNICODE);
