<?php

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

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

$role_id = $_SESSION['role_id'];

if($role_id == 1){

$cats=mysqli_query($conn,"
SELECT c.*,v.village_name
FROM gallery_categories c
LEFT JOIN villages v ON c.village_id=v.id
");

}else{

$village_id=$_SESSION['village_id'];

$cats=mysqli_query($conn,"
SELECT c.*,v.village_name
FROM gallery_categories c
LEFT JOIN villages v ON c.village_id=v.id
WHERE c.village_id='$village_id'
");

}

?>

<div class="main">

<h2>Gallery Categories</h2>

<a href="create_gallery_category.php" class="btn-success">
Create Category
</a>

<table>

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

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

<tr>

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

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

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

<td>

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

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

<?php } ?>

</td>

<td>

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

<a href="delete_gallery_category.php?id=<?php echo $row['id']; ?>"
onclick="return confirm('Are you sure you want to delete this category?')"
class="btn btn-danger">
Delete
</a>

</td>

</tr>

<?php } ?>

</table>

</div>

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