<?php

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

include "../layout/header.php";
include "../layout/sidebar.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="main">

<div style="display:flex;justify-content:space-between;margin-bottom:20px;">

<h2>Government Buildings</h2>

<a href="add_govt_building.php" class="btn-success">Add Govt Building</a>

</div>

<table>

<thead>

<tr>
<th>#</th>
<th>Village</th>
<th>Image</th>
<th>Building</th>
<th>Type</th>
<th>Location</th>
</tr>

</thead>

<tbody>

<?php

$i=1;

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

<tr>

<td><?php echo $i++; ?></td>

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

<td>

<?php if($row['image']){ ?>

<img src="../../uploads/buildings/<?php echo $row['image']; ?>" width="50">

<?php } ?>

</td>

<td><?php echo $row['building_name']; ?></td>
<td><?php echo $row['building_type']; ?></td>
<td><?php echo $row['location']; ?></td>

</tr>

<?php } ?>

</tbody>

</table>

</div>

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