<?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)
{

$temples=mysqli_query($conn,"
SELECT t.*,v.village_name
FROM village_temples t
LEFT JOIN villages v ON t.village_id=v.id
ORDER BY t.id DESC
");

}
else
{

$village_id=$_SESSION['village_id'];

$temples=mysqli_query($conn,"
SELECT t.*,v.village_name
FROM village_temples t
LEFT JOIN villages v ON t.village_id=v.id
WHERE t.village_id='$village_id'
ORDER BY t.id DESC
");

}

?>

<div class="main">

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

<h2>Village Temples</h2>

<a href="add_temple.php" class="btn-success">Add Temple</a>

</div>

<table>

<thead>

<tr>
<th>#</th>
<th>Village</th>
<th>Image</th>
<th>Temple</th>
<th>God</th>
</tr>

</thead>

<tbody>

<?php

$i=1;

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

<tr>

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

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

<td>

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

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

<?php } ?>

</td>

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

</tr>

<?php } ?>

</tbody>

</table>

</div>

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