<?php

include "../../config/master.php";

allowRoles([1,5,6]);

include "../layout/header.php";
include "../layout/sidebar.php";

$scheme_id = intval($_GET['id'] ?? 0);

if($scheme_id == 0){
header("Location: view_schemes.php");
exit;
}


/* FETCH SCHEME */

$q = mysqli_query($conn,"
SELECT s.*, c.caste_name, e.education_name
FROM schemes s
LEFT JOIN caste_master c ON s.category_id=c.id
LEFT JOIN education_master e ON s.education_id=e.id
WHERE s.id='$scheme_id'
LIMIT 1
");

$scheme = mysqli_fetch_assoc($q);

if(!$scheme){
echo "Scheme not found";
exit;
}


/* ELIGIBLE MEMBERS FILTER */

$where = "WHERE 1";


/* Gender */

if(!empty($scheme['gender']) && $scheme['gender'] != 'Any'){
$where .= " AND m.gender='".mysqli_real_escape_string($conn,$scheme['gender'])."'";
}


/* Age */

if($scheme['age_min'] > 0 && $scheme['age_max'] > 0){
$where .= " AND m.age BETWEEN ".intval($scheme['age_min'])." AND ".intval($scheme['age_max']);
}


/* Education */

if(!empty($scheme['education_id'])){
$where .= " AND m.education_id >= ".intval($scheme['education_id']);
}


/* Family Type */

if(!empty($scheme['family_type']) && $scheme['family_type'] != 'Any'){
$where .= " AND f.family_type='".mysqli_real_escape_string($conn,$scheme['family_type'])."'";
}


/* Caste Category FIX */

if(!empty($scheme['caste_name'])){
$where .= " AND f.caste='".mysqli_real_escape_string($conn,$scheme['caste_name'])."'";
}


/* QUERY */

$members = mysqli_query($conn,"
SELECT 
m.id,
m.name,
m.gender,
m.age,
m.mobile,
f.family_head,
v.village_name
FROM family_members m
LEFT JOIN families f ON m.family_id=f.id
LEFT JOIN villages v ON f.village_id=v.id
$where
ORDER BY m.name
");

?>

<div class="main">

<h2>Scheme Detail</h2>

<div style="background:#fff;padding:25px;border-radius:6px;box-shadow:0 2px 6px rgba(0,0,0,0.1);">

<h3><?php echo htmlspecialchars($scheme['scheme_name']); ?></h3>

<?php if(!empty($scheme['image'])){ ?>

<img src="../../assets/schemes/<?php echo htmlspecialchars($scheme['image']); ?>" 
style="max-width:250px;margin-bottom:15px;">

<?php } ?>


<p><b>Category:</b> 
<?php echo ($scheme['category_id']==0) ? "All" : htmlspecialchars($scheme['caste_name']); ?>
</p>

<p><b>Department:</b> <?php echo htmlspecialchars($scheme['department']); ?></p>

<p><b>Gender:</b> <?php echo htmlspecialchars($scheme['gender']); ?></p>

<p><b>Education:</b> 
<?php echo (!empty($scheme['education_name'])) ? htmlspecialchars($scheme['education_name']) : "Any"; ?>
</p>

<p><b>Family Type:</b> <?php echo htmlspecialchars($scheme['family_type']); ?></p>

<p><b>Age Range:</b> <?php echo $scheme['age_min']; ?> - <?php echo $scheme['age_max']; ?></p>

<hr>

<h3>Eligible Members</h3>

<table border="1" width="100%" cellpadding="8" style="border-collapse:collapse;">

<tr style="background:#2c3e50;color:white">

<th>Name</th>
<th>Gender</th>
<th>Age</th>
<th>Mobile</th>
<th>Family Head</th>
<th>Village</th>

</tr>

<?php

if($members && mysqli_num_rows($members) > 0){

while($row=mysqli_fetch_assoc($members)){
?>

<tr>

<td><?php echo htmlspecialchars($row['name']); ?></td>

<td><?php echo htmlspecialchars($row['gender']); ?></td>

<td><?php echo htmlspecialchars($row['age']); ?></td>

<td><?php echo htmlspecialchars($row['mobile']); ?></td>

<td><?php echo htmlspecialchars($row['family_head']); ?></td>

<td><?php echo htmlspecialchars($row['village_name']); ?></td>

</tr>

<?php
}

}else{

echo "<tr><td colspan='6' align='center'>No Eligible Members Found</td></tr>";

}

?>

</table>

<br>

<a href="view_schemes.php"
style="background:#2c7be5;color:#fff;padding:6px 12px;border-radius:4px;text-decoration:none;">
← Back to Schemes
</a>

</div>

</div>

<?php include "../layout/footer.php"; ?>