<?php

header("Content-Type: application/json");
header("Access-Control-Allow-Origin: *");

require_once("../config/db.php");

$query = "
SELECT 
    sr.id,
    sr.visitor_id,
    sr.reason,
    sr.status,
    sr.created_at,

    v.name AS visitor_name,
    v.mobile,

    u.name AS requested_by_name

FROM suspicious_requests sr

LEFT JOIN visitors v ON sr.visitor_id = v.id
LEFT JOIN users u ON sr.requested_by = u.id

WHERE sr.status = 'pending'

ORDER BY sr.id DESC
";

$result = $conn->query($query);

$data = [];

if ($result) {
    while ($row = $result->fetch_assoc()) {
        $data[] = $row;
    }
}

echo json_encode([
    "status" => true,
    "data" => $data
]);