<?php

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

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


/* FILTER VALUES */

$search   = $_GET['search'] ?? '';
$district = $_GET['district'] ?? '';
$block    = $_GET['block'] ?? '';


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


if($search != '')
{
$where .= " AND v.village_name 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 */

$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);


/* FETCH DATA */

$query = mysqli_query($conn,"
SELECT 
v.id,
v.village_name,
d.district_name,
b.block_name,
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
");


/* FETCH DISTRICTS */

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

?>

<div class="main">

<h2>Village Data</h2>

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

<p><b>Total Villages:</b> <?php echo $total; ?></p>

<a href="inactive_villages.php"
style="
background:#e67e22;
color:white;
padding:8px 12px;
text-decoration:none;
border-radius:4px;
font-size:14px;
">

View Inactive Villages

</a>

</div>

<br>


<form method="GET">

<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']; ?>

</option>

<?php } ?>

</select>


<select name="block" id="block">

<option value="">All Blocks</option>

</select>


<button type="submit">Search</button>

<a href="village_data.php">Reset</a>

</form>

<br>


<table border="1" width="100%" cellpadding="8" cellspacing="0">

<tr style="background:#eee">

<th>ID</th>
<th>Village</th>
<th>District</th>
<th>Block</th>
<th>Pincode</th>
<th>Action</th>

</tr>


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

<tr>

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

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

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

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

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

<td>

<a href="edit_village.php?id=<?php echo $row['id']; ?>">Edit</a> |

<a href="delete_village.php?id=<?php echo $row['id']; ?>"
onclick="return confirm('Deactivate this village?')">

Deactivate

</a>

</td>

</tr>

<?php } ?>

</table>


<br>


<!-- 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; ?>">

<?php echo $i; ?>

</a>

<?php } ?>


</div>


<script>

/* LOAD BLOCKS WHEN DISTRICT CHANGES */

document.getElementById("district").addEventListener("change", function(){

var district_id = this.value;

fetch("../villages/get_blocks.php?district_id="+district_id)

.then(res => res.text())

.then(data => {

document.getElementById("block").innerHTML = data;

});

});

</script>


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