<?php

include "../config/db.php";

$slug = $_GET['village'] ?? '';
$type = $_GET['type'] ?? '';

/* FETCH VILLAGE */

$village = mysqli_fetch_assoc(mysqli_query($conn,"
SELECT * FROM villages WHERE slug='$slug'
"));

if(!$village){
echo "Village not found";
exit;
}

$village_id = $village['id'];

include "header.php";

/* DEFAULT VARIABLES */

$table="";
$title="";
$name_field="";
$extra_field="";
$img_path="";

/* BUILDING TYPES */

switch($type){

case "chaupals":

$table="village_chaupals";
$title="Village Chaupals";
$name_field="chaupal_name";
$extra_field="location";
$img_path="uploads/chaupals/";
break;

case "govt_buildings":

$table="village_govt_buildings";
$title="Government Buildings";
$name_field="building_name";
$extra_field="location";
$img_path="uploads/buildings/";
break;

case "anganwadi":

$table="village_anganwadi";
$title="Anganwadi Centers";
$name_field="anganwadi_name";
$extra_field="location";
$img_path="uploads/anganwadi/";
break;

case "temples":

$table="village_temples";
$title="Village Temples";
$name_field="temple_name";
$extra_field="god_name";
$img_path="uploads/temples/";
break;

}

?>

<style>

.gallery-grid{
display:grid;
grid-template-columns:repeat(auto-fit,minmax(250px,1fr));
gap:20px;
margin-top:20px;
}

.gallery-item{
background:#fff;
border-radius:8px;
overflow:hidden;
box-shadow:0 2px 8px rgba(0,0,0,0.1);
transition:0.3s;
}

.gallery-item:hover{
transform:translateY(-5px);
}

.gallery-item img{
width:100%;
height:200px;
object-fit:cover;
}

.image-title{
padding:10px;
text-align:center;
background:#f7f7f7;
font-size:14px;
}

</style>


<div class="container">

<?php

if($table==""){
echo "<h2 class='section-title'>Village Buildings</h2>";
echo "<p>Select building category from the menu.</p>";
}
else{

$query = mysqli_query($conn,"
SELECT *
FROM $table
WHERE village_id='$village_id'
ORDER BY id DESC
");

?>

<h2 class="section-title"><?php echo $title; ?></h2>

<div class="gallery-grid">

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

<div class="gallery-item">

<?php if(!empty($row['image'])){ ?>

<a href="/digital_panchayat/<?php echo $img_path.$row['image']; ?>" data-lightbox="village-buildings">

<img src="/digital_panchayat/<?php echo $img_path.$row['image']; ?>">

</a>

<?php } ?>

<div class="image-title">

<strong><?php echo $row[$name_field]; ?></strong><br>

<?php
if(isset($row[$extra_field])){
echo $row[$extra_field];
}
?>

</div>

</div>

<?php } ?>

</div>

<?php } ?>

</div>

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