<?php
require_once 'db.php';

// Asumsi kita mengirim ID merchant (restoran) lewat URL
$resto_id = isset($_GET['resto_id']) ? (int)$_GET['resto_id'] : 0;

if ($resto_id <= 0) {
    http_response_code(400);
    echo json_encode(["error" => "ID Restoran tidak valid."]);
    exit();
}

try {
    // Ambil semua pesanan untuk resto ini, diurutkan dari yang terbaru
    // Kita juga mengambil data 'users' (pemesan) menggunakan JOIN
    $sql = "SELECT 
                o.id, 
                o.status, 
                o.total_price, 
                o.created_at, 
                u.name as user_name, 
                u.phone as user_phone
            FROM orders o
            JOIN users u ON o.user_id = u.id
            WHERE o.restaurant_id = :resto_id
            ORDER BY o.created_at DESC";
            
    $stmt = $conn->prepare($sql);
    $stmt->bindParam(':resto_id', $resto_id);
    $stmt->execute();
    
    $orders = $stmt->fetchAll(PDO::FETCH_ASSOC);

    echo json_encode($orders);

} catch(PDOException $e) {
    http_response_code(500);
    echo json_encode(["error" => $e->getMessage()]);
}
?>