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

/* SUPER ADMIN */

$images = mysqli_query($conn,"
SELECT g.*, c.category_name, v.village_name
FROM gallery_images g
LEFT JOIN gallery_categories c ON g.category_id = c.id
LEFT JOIN villages v ON g.village_id = v.id
ORDER BY g.id DESC
");

}else{

/* VILLAGE ADMIN */

$village_id = $_SESSION['village_id'];

$images = mysqli_query($conn,"
SELECT g.*, c.category_name, v.village_name
FROM gallery_images g
LEFT JOIN gallery_categories c ON g.category_id = c.id
LEFT JOIN villages v ON g.village_id = v.id
WHERE g.village_id='$village_id'
ORDER BY g.id DESC
");

}

?>

<div class="main">

<h2>Gallery Images</h2>

<a href="add_gallery_image.php" class="btn-success">Add Image</a>

<table>

<thead>

<tr>
<th>ID</th>
<th>Village</th>
<th>Category</th>
<th>Image</th>
<th>Title</th>
</tr>

</thead>

<tbody>

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

<tr>

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

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

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

<td>
<?php if($row['image']){ ?>
<img src="../../uploads/gallery/<?php echo $row['image']; ?>" width="80">
<?php } ?>
</td>

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

</tr>

<?php } ?>

</tbody>

</table>

</div>

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