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

$q = $conn->query("
SELECT id,name,father_name,mobile,created_at 
FROM users 
WHERE village_id='$village_id' 
AND role='sub_admin' 
AND status='pending'
ORDER BY id DESC
");
?>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pending Sub Admins</title>

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

<body>
<div class="app">

<div class="header">
<h2>Pending Sub Admins</h2>
</div>

<?php while($row = $q->fetch_assoc()): ?>
<div class="card">
<b><?= $row['name'] ?></b><br>
<?= $row['mobile'] ?><br>
<?= $row['father_name'] ?><br>

<div class="row">
<button class="btn green" onclick="approve(<?= $row['id'] ?>)">Approve</button>
<button class="btn red" onclick="rejectUser(<?= $row['id'] ?>)">Reject</button>
</div>
</div>
<?php endwhile; ?>

</div>

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

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

</body>
</html>