<?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 w.*, v.village_name
FROM village_development_works w
LEFT JOIN villages v ON w.village_id = v.id
WHERE w.village_id='$village_id'
ORDER BY w.id DESC
";
}
else
{
$query = "
SELECT w.*, v.village_name
FROM village_development_works w
LEFT JOIN villages v ON w.village_id = v.id
ORDER BY w.id DESC
";
}

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

?>

<div class="main">

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

<h2>Development Works</h2>

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

</div>

<table>

<tr>
<th>#</th>
<th>Village</th>
<th>Title</th>
<th>Description</th>
<th>Status</th>
<th>Image</th>
<th>Action</th>
</tr>

<?php

if(mysqli_num_rows($works) > 0)
{

$i=1;

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

<tr>

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

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

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

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

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

<td>

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

</td>

<td>

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

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

</td>

</tr>

<?php
}

}
else
{
?>

<tr>
<td colspan="7" style="text-align:center;">No development works found</td>
</tr>

<?php
}
?>

</table>

</div>

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