<?php
require_once("auth.php");
require_once("../api/config/db.php");

$q = $conn->query("
SELECT sr.id,sr.reason,sr.status,
v.name visitor_name,v.mobile
FROM suspicious_requests sr
LEFT JOIN visitors v ON sr.visitor_id=v.id
WHERE sr.status='pending'
ORDER BY sr.id DESC
");
?>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Suspicious Requests</title>

<style>
body{margin:0;font-family:Arial;background:#f1f5f9}
.app{max-width:520px;margin:0 auto}
.header{background:#dc2626;color:white;padding:20px;border-radius:0 0 25px 25px}
.card{background:white;margin:14px;padding:14px;border-radius:18px}
.btn{padding:10px;border:0;border-radius:10px;color:white;margin-top:10px}
.green{background:#16a34a}
.red{background:#dc2626}
</style>
</head>

<body>
<div class="app">

<div class="header">
<h2>Suspicious Requests</h2>
</div>

<?php while($row = $q->fetch_assoc()): ?>
<div class="card">

<b><?= $row['visitor_name'] ?></b><br>
<?= $row['mobile'] ?><br>
Reason: <?= $row['reason'] ?>

<br>

<input type="file" id="file<?= $row['id'] ?>">

<button class="btn green" onclick="approveReq(<?= $row['id'] ?>)">Approve</button>
<button class="btn red" onclick="rejectReq(<?= $row['id'] ?>)">Reject</button>

</div>
<?php endwhile; ?>

</div>

<script>
function approveReq(id){
let file=document.getElementById('file'+id).files[0];
let fd=new FormData();
fd.append('request_id',id);
fd.append('document',file);

fetch('../api/suspicious/approve.php',{method:'POST',body:fd})
.then(r=>r.json()).then(d=>{alert(d.message);location.reload();});
}

function rejectReq(id){
fetch('../api/suspicious/reject.php',{
method:'POST',
headers:{'Content-Type':'application/json'},
body:JSON.stringify({request_id:id,user_id:<?= $admin_id ?>})
}).then(r=>r.json()).then(d=>{alert(d.message);location.reload();});
}
</script>

</body>
</html>