<?php
// api-orders-status.php - Tarik status order (refund) untuk sinkron ke app
require_once __DIR__ . '/config.php';

if ($_SERVER['REQUEST_METHOD'] !== 'GET') {
    jsonResponse(['status' => 'error', 'message' => 'GET only'], 405);
}

$user = requireAuth();
$db = getDB();

$where = 'WHERE o.created_at >= (CURDATE() - INTERVAL 30 DAY)';
$params = [];
if ($user['role'] !== 'admin') {
    $where .= ' AND o.outlet_id = ?';
    $params[] = $user['outlet_id'] ?? 0;
}

try {
    $stmt = $db->prepare("
        SELECT o.id, o.transaction_time, o.outlet_id, o.order_number, o.status, o.refunded_at, o.refunded_by
        FROM orders o
        $where
        ORDER BY o.created_at DESC
    ");
    $stmt->execute($params);
    $orders = $stmt->fetchAll();

    jsonResponse(['status' => 'success', 'data' => $orders]);
} catch (Exception $e) {
    jsonResponse(['status' => 'error', 'message' => $e->getMessage()], 500);
}