<?php
require_once("_guard.php");
$status = $_GET['status'] ?? 'pending';
$allowed = ['pending', 'approved', 'rejected', 'not_submitted', 'all'];
if (!in_array($status, $allowed, true)) $status = 'pending';

$sql = "SELECT v.id, v.user_id, v.id_front, v.id_back, v.selfie, v.status, v.rejection_reason, u.full_name, u.email
        FROM user_verifications v INNER JOIN users u ON u.id=v.user_id";
if ($status !== 'all') $sql .= " WHERE v.status=?";
$sql .= " ORDER BY FIELD(v.status,'pending','rejected','approved'), v.id DESC";
$rows=[];
if ($status === 'all') {
  $result=$conn->query($sql);
} else {
  $stmt=$conn->prepare($sql); $stmt->bind_param('s',$status); $stmt->execute(); $result=$stmt->get_result();
}
while ($result && ($row=$result->fetch_assoc())) $rows[]=$row;
echo json_encode(['success'=>true,'verifications'=>$rows]);
?>
