<?php

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

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

/* FILTER VALUES */
$search   = $_GET['search'] ?? '';
$district = $_GET['district'] ?? '';
$block    = $_GET['block'] ?? '';

$where = "WHERE v.status = 1";

/* SEARCH (EN + HI BOTH) */
if($search != ''){
$where .= " AND (
v.village_name LIKE '%$search%' 
OR v.village_name_hi LIKE '%$search%'
)";
}

if($district != ''){
$where .= " AND v.district_id='$district'";
}

if($block != ''){
$where .= " AND v.block_id='$block'";
}

/* PAGINATION */
$limit = 10;
$page = $_GET['page'] ?? 1;
if($page < 1) $page = 1;
$start = ($page - 1) * $limit;

/* TOTAL */
$count_query = mysqli_query($conn,"
SELECT COUNT(*) as total FROM villages v $where
");
$total = mysqli_fetch_assoc($count_query)['total'];
$total_pages = ceil($total / $limit);

/* DATA (NOW WITH HINDI) */
$query = mysqli_query($conn,"
SELECT 
v.id,
v.village_name,
v.village_name_hi,
d.district_name,
d.district_name_hi,
b.block_name,
b.block_name_hi,
v.pincode
FROM villages v
LEFT JOIN districts d ON v.district_id = d.id
LEFT JOIN blocks b ON v.block_id = b.id
$where
ORDER BY v.village_name
LIMIT $start,$limit
");

/* DISTRICTS */
$districts = mysqli_query($conn,"SELECT * FROM districts ORDER BY district_name");

?>

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

<div class="card-box">
<h4 style="margin:0;">Village Data</h4>
<p style="font-size:12px;color:#777;margin:0;">
Total: <?php echo $total; ?> villages
</p>
</div>

<!-- ===== FILTER BOX ===== -->

<div class="card-box">

<form method="GET" class="filter-form">

<input type="text" name="search" placeholder="Search village"
value="<?php echo $search; ?>">

<select name="district" id="district">
<option value="">All Districts</option>

<?php while($d=mysqli_fetch_assoc($districts)){ ?>

<option value="<?php echo $d['id']; ?>"
<?php if($district==$d['id']) echo "selected"; ?>>

<?php 
echo ($d['district_name_hi'] != '') 
? $d['district_name_hi'] 
: $d['district_name']; 
?>

</option>

<?php } ?>

</select>

<select name="block" id="block">
<option value="">All Blocks</option>
</select>

<div class="filter-actions">
<button type="submit" class="btn-mobile btn-save">Search</button>
<a href="village_data.php" class="btn-mobile btn-reset">Reset</a>
</div>

</form>

</div>

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

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

<div class="card-box">

<h5 style="margin:0;">
<?php 
echo ($row['village_name_hi'] != '') 
? $row['village_name_hi'] 
: $row['village_name']; 
?>
</h5>

<p style="font-size:12px;color:#666;margin:3px 0;">
📍 
<?php 
echo ($row['district_name_hi'] != '') 
? $row['district_name_hi'] 
: $row['district_name']; 
?> 
/ 
<?php 
echo ($row['block_name_hi'] != '') 
? $row['block_name_hi'] 
: $row['block_name']; 
?>
</p>

<p style="font-size:13px;">
📮 <?php echo $row['pincode']; ?>
</p>

<div class="action-icons">

<a href="edit_village.php?id=<?php echo $row['id']; ?>" class="icon-btn edit">
<i class="fa fa-pen"></i>
</a>

<a href="delete_village.php?id=<?php echo $row['id']; ?>"
onclick="return confirm('Deactivate this village?')"
class="icon-btn delete">
<i class="fa fa-ban"></i>
</a>

</div>

</div>

<?php } ?>

<!-- ===== PAGINATION ===== -->

<div class="pagination">

<?php for($i=1;$i<=$total_pages;$i++){ ?>

<a href="?page=<?php echo $i; ?>&search=<?php echo $search; ?>&district=<?php echo $district; ?>&block=<?php echo $block; ?>"
class="<?php if($i==$page) echo 'active'; ?>">
<?php echo $i; ?>
</a>

<?php } ?>

</div>

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

<div class="bottom-nav">
    <a href="../dashboard.php"><i class="fa fa-home"></i>Home</a>
    <a href="village_data.php" class="active"><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"; ?>

<script>

/* LOAD BLOCKS */
document.getElementById("district").addEventListener("change", function(){
fetch("../villages/get_blocks.php?district_id="+this.value)
.then(res => res.text())
.then(data => {
document.getElementById("block").innerHTML = data;
});
});

</script>