<?php

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

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

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

if($role_id == 1)
{
$chaupals=mysqli_query($conn,"
SELECT c.*,v.village_name
FROM village_chaupals c
LEFT JOIN villages v ON c.village_id=v.id
ORDER BY c.id DESC
");
}
else
{
$village_id=$_SESSION['village_id'];

$chaupals=mysqli_query($conn,"
SELECT c.*,v.village_name
FROM village_chaupals c
LEFT JOIN villages v ON c.village_id=v.id
WHERE c.village_id='$village_id'
ORDER BY c.id DESC
");
}
?>

<!-- ===== PAGE HEADER ===== -->

<div class="card-box" style="display:flex;justify-content:space-between;align-items:center;">

<div>
<h4 style="margin:0;">Village Chaupals</h4>
<p style="font-size:12px;color:#777;margin:0;">Community places</p>
</div>

<a href="add_chaupal.php" class="btn-mobile btn-add">
    <i class="fa fa-plus"></i>
</a>

</div>

<!-- ===== LIST ===== -->

<?php if(mysqli_num_rows($chaupals) > 0){ ?>

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

<div class="card-box">

<!-- IMAGE -->
<?php if(!empty($row['image'])){ ?>
<img src="../../uploads/chaupals/<?php echo $row['image']; ?>" class="chaupal-img">
<?php } ?>

<!-- NAME -->
<h5 style="margin-top:8px;"><?php echo $row['chaupal_name']; ?></h5>

<!-- VILLAGE -->
<p style="font-size:12px;color:#666;margin:2px 0;">
📍 <?php echo $row['village_name']; ?>
</p>

<!-- LOCATION -->
<p style="font-size:13px;color:#444;">
<?php echo $row['location']; ?>
</p>

<!-- ACTION BUTTONS -->
<div style="display:flex;gap:10px;margin-top:10px;">

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

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

</div>

</div>

<?php } ?>

<?php } else { ?>

<div class="card-box" style="text-align:center;">
No chaupals found
</div>

<?php } ?>

<!-- ===== BOTTOM NAV ===== -->

<div class="bottom-nav">
    <a href="../dashboard.php">
        <i class="fa fa-home"></i>
        Home
    </a>

    <a href="../villages/village_data.php">
        <i class="fa fa-map"></i>
        Villages
    </a>

    <a href="../families/view_families.php">
        <i class="fa fa-users"></i>
        Families
    </a>

    <a href="../schemes/view_schemes.php">
        <i class="fa fa-list"></i>
        Schemes
    </a>
</div>

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