<?php

/* ==========================
   VILLAGE SLIDERS
========================== */
/* ==========================
   VILLAGE SLIDERS
========================== */

$sliders = mysqli_query($conn,"
SELECT *
FROM village_sliders
WHERE village_id='$village_id'
ORDER BY display_order ASC
");


/* ==========================
   VILLAGE VIDEOS
========================== */

$videos = mysqli_query($conn,"
SELECT * FROM village_videos
WHERE village_id='$village_id'
ORDER BY id DESC
LIMIT 6
");

/* ==========================
   VILLAGE STATISTICS
========================== */

/* TOTAL FAMILIES */

$fam = mysqli_fetch_assoc(mysqli_query($conn,"
SELECT COUNT(*) as total FROM families
WHERE village_id='$village_id'
"));

$total_families = $fam['total'];


/* TOTAL MEMBERS */

$mem = mysqli_fetch_assoc(mysqli_query($conn,"
SELECT COUNT(*) as total FROM family_members fm
JOIN families f ON f.id = fm.family_id
WHERE f.village_id='$village_id'
"));

$total_members = $mem['total'];


/* POPULATION */

$population = $total_members + $total_families;


/* DEVELOPMENT WORKS */

$total_works = 0;


/* TOTAL SCHEMES */

$schemes = mysqli_fetch_assoc(mysqli_query($conn,"
SELECT COUNT(*) as total FROM schemes
"));

$total_schemes = $schemes['total'];

?>

<div class="container">

<!-- SLIDER + NEWS -->

<div class="top-section">

<div class="slider">

<button class="slider-btn prev" onclick="prevSlide()">&#10094;</button>
<button class="slider-btn next" onclick="nextSlide()">&#10095;</button>

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

<div class="slider-container">

<?php 
$first = true;
while($s = mysqli_fetch_assoc($sliders)){ 
?>

<div class="slide <?php echo $first ? 'active' : ''; ?>">

<img src="/digital_panchayat/uploads/sliders/<?php echo $s['image']; ?>" alt="slider">

<?php if($s['title'] || $s['description']){ ?>

<div class="slide-caption">
<h3><?php echo $s['title']; ?></h3>
<p><?php echo $s['description']; ?></p>
</div>

<?php } ?>

</div>

<?php 
$first = false;
} 
?>

</div>

<?php } else { ?>

<div style="padding:20px;text-align:center;">
No Slider Images
</div>

<?php } ?>

</div>



<!-- NEWS SECTION -->

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


</div>



<!-- VILLAGE STATISTICS 

<h2 class="section-title">गांव  से संभधित आंकड़े</h2>

<div class="stats">

<div class="stat-box">
<h3><?php echo $total_families; ?></h3>
<p>परिवार</p>
</div>

<div class="stat-box">
<h3><?php echo $total_members; ?></h3>
<p>सदस्य</p>
</div>

<div class="stat-box">
<h3><?php echo $population; ?></h3>
<p>जनसंखया</p>
</div>

<div class="stat-box">
<h3><?php echo $total_works; ?></h3>
<p>गांव में कराये गए विकास कार्य्य</p>
</div>

<div class="stat-box">
<h3><?php echo $total_schemes; ?></h3>
<p>सरकारी योजनाएं</p>
</div>

</div>  -->

<!-- ==========================
   VILLAGE SCHEMES
========================== -->

<?php

$scheme_list = mysqli_query($conn,"
SELECT *
FROM schemes
WHERE status=1
ORDER BY id DESC
LIMIT 4
");

/* ==========================
   DEVELOPMENT WORKS (HOME)
========================== */

$dev_home = mysqli_query($conn,"
SELECT * FROM village_development_works
WHERE village_id='$village_id'
ORDER BY id DESC
LIMIT 4
");

?>


<!-- ABOUT VILLAGE -->

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

<!-- DEVELOPMENT WORKS -->

<h2 class="section-title">गांव में कराये गए विकास कार्य</h2>

<div class="gallery-grid">

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

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

<div class="dev-card">

<?php if(!empty($d['image'])){ ?>
<div class="dev-img">
<img src="/digital_panchayat/uploads/development/<?php echo htmlspecialchars($d['image']); ?>" 
onclick="openImage(this.src)">
</div>
<?php } ?>

<div class="dev-content">

<h4><?php echo htmlspecialchars($d['title']); ?></h4>

<p><?php echo substr(strip_tags($d['description']),0,60); ?>...</p>

<p>

<?php 
if($d['status']=="Completed"){
echo "<span class='status-completed'>Completed</span>";
}elseif($d['status']=="In Progress"){
echo "<span class='status-progress'>In Progress</span>";
}else{
echo "<span class='status-planned'>Planned</span>";
}
?>

</p>

</div>

</div>

<?php } ?>

<?php } else { ?>

<p>No development works available</p>

<?php } ?>

</div>
<div style="text-align:center;margin-top:20px;">

<a href="/development.php?village=<?php echo $village['slug']; ?>" class="view-btn">
सभी विकास कार्य देखें</a>

</div>



<!-- VIEW ALL BUTTON -->

<?php include "sarpanch_section.php"; ?>
<?php include "special_thanks_section.php"; ?>

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


<!-- VILLAGE VIDEOS -->

<h2 class="section-title">गाओं से संबधित वीडियो</h2>

<div class="gallery-grid">

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

<?php while($v = mysqli_fetch_assoc($videos)){ 

preg_match(
"/(youtu\.be\/|v=|\/embed\/|\/shorts\/)([a-zA-Z0-9_-]{11})/",
$v['youtube_url'],
$matches
);

$video_id = $matches[2] ?? '';

?>

<div class="gallery-item">

<?php if($video_id){ ?>

<iframe 
src="https://www.youtube.com/embed/<?php echo $video_id; ?>" 
frameborder="0" 
allowfullscreen>
</iframe>

<p style="margin-top:8px;"><?php echo $v['title']; ?></p>

<?php } ?>

</div>

<?php } ?>

<?php } else { ?>

<p>कोई वीडियो उपलब्ध नहीं   </p>

<?php } ?>

</div>

<h2 class="section-title">सरकारी योजनाएं</h2>

<div class="scheme-grid">

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

<?php while($s = mysqli_fetch_assoc($scheme_list)){ ?>

<div class="scheme-card">

    <?php if(!empty($s['image'])){ ?>
<img src="/digital_panchayat/uploads/schemes/<?php echo $s['image']; ?>">
    <?php } ?>

    <h3><?php echo htmlspecialchars($s['scheme_name']); ?></h3>


    <a href="/scheme_detail.php?id=<?php echo $s['id']; ?>&village=<?php echo $slug; ?>" class="scheme-btn">
        आगे ओर पढ़ें
    </a>

</div>

<?php } ?>

<?php } else { ?>

<p>No schemes available</p>

<?php } ?>

</div>



<!-- VIEW ALL BUTTON -->

<div style="text-align:center;margin-top:20px;">

<a href="/schemes.php?village=<?php echo $slug; ?>" class="view-all-btn">
    सभी सरकारी योजनाएं देखें
</a>

</div>


</div>