<?php

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

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

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

/* QUERY */

if($role_id == 5)
{
$village_id = $_SESSION['village_id'] ?? 0;

$query = "
SELECT t.*, v.village_name
FROM village_people_thoughts t
LEFT JOIN villages v ON t.village_id = v.id
WHERE t.village_id='$village_id'
ORDER BY t.id DESC
";
}
else
{
$query = "
SELECT t.*, v.village_name
FROM village_people_thoughts t
LEFT JOIN villages v ON t.village_id = v.id
ORDER BY t.id DESC
";
}

$thoughts = mysqli_query($conn,$query);

?>

<div class="main">

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

<h2>People Thoughts</h2>

<a href="add_people_thought.php" class="btn-success" style="padding:8px 14px;text-decoration:none;">
Add Thought
</a>

</div>

<table>

<tr>
<th>#</th>
<th>Village</th>
<th>Name</th>
<th>Image</th>
<th>Description</th>
<th>Action</th>
</tr>

<?php

$i=1;

if(mysqli_num_rows($thoughts)>0)
{

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

<tr>

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

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

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

<td>

<?php
if(!empty($row['image']))
{
echo "<img src='../../uploads/thoughts/".$row['image']."' width='60'>";
}
else
{
echo "-";
}
?>

</td>

<td><?php echo substr($row['description'],0,100); ?></td>

<td>

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

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

</td>

</tr>

<?php
}

}
else
{
?>

<tr>
<td colspan="6" style="text-align:center;">No thoughts found</td>
</tr>

<?php
}
?>

</table>

</div>

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