<?php

include "../../config/master.php";
allowRoles([1,5]);

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

$role_id = $_SESSION['role_id'] ?? 0;

if($role_id == 1)
{
$buildings=mysqli_query($conn,"
SELECT g.*,v.village_name
FROM village_govt_buildings g
LEFT JOIN villages v ON g.village_id=v.id
ORDER BY g.id DESC
");
}
else
{
$village_id=$_SESSION['village_id'];

$buildings=mysqli_query($conn,"
SELECT g.*,v.village_name
FROM village_govt_buildings g
LEFT JOIN villages v ON g.village_id=v.id
WHERE g.village_id='$village_id'
ORDER BY g.id DESC
");
}
?>

<div class="card-box" style="display:flex;justify-content:space-between;align-items:center;">
<div>
<h4>Government Buildings</h4>
<p style="font-size:12px;color:#777;">Public infrastructure</p>
</div>

<a href="add_govt_building.php" class="btn-mobile btn-add">
<i class="fa fa-plus"></i>
</a>
</div>

<?php if(mysqli_num_rows($buildings) > 0){ ?>

<?php while($row=mysqli_fetch_assoc($buildings)){ ?>

<div class="card-box">

<?php if(!empty($row['image'])){ ?>
<img src="../../uploads/buildings/<?php echo $row['image']; ?>" class="building-img">
<?php } ?>

<h5><?php echo $row['building_name']; ?></h5>

<p>🏢 <?php echo $row['building_type']; ?></p>

<p style="font-size:12px;color:#666;">
📍 <?php echo $row['village_name']; ?>
</p>

<p><?php echo $row['location']; ?></p>

<!-- 🔥 ACTION BUTTONS -->
<div style="display:flex;gap:10px;margin-top:10px;">

<a href="edit_govt_building.php?id=<?php echo $row['id']; ?>" 
class="btn btn-sm btn-primary">
✏️ Edit
</a>

<a href="delete_govt_building.php?id=<?php echo $row['id']; ?>" 
class="btn btn-sm btn-danger"
onclick="return confirm('Delete this building?')">
🗑 Delete
</a>

</div>

</div>

<?php } ?>

<?php } else { ?>

<div class="card-box" style="text-align:center;">
No buildings found
</div>

<?php } ?>

<div class="bottom-nav">
<a href="../dashboard.php"><i class="fa fa-home"></i>Home</a>
<a href="../villages/village_data.php"><i class="fa fa-map"></i>Villages</a>
<a href="../families/view_families.php"><i class="fa fa-users"></i>Families</a>
<a href="../schemes/view_schemes.php"><i class="fa fa-list"></i>Schemes</a>
</div>

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