<?php
error_reporting(E_ALL);
ini_set('display_errors',1);

session_start();
include "../config/db.php";

if(!isset($_SESSION['user_id'])){
header("Location: login.php");
exit;
}

/* GET STATISTICS */

$villages = mysqli_fetch_assoc(mysqli_query($conn,"SELECT COUNT(*) as total FROM villages"))['total'];

$families = mysqli_fetch_assoc(mysqli_query($conn,"SELECT COUNT(*) as total FROM families"))['total'];

$members = mysqli_fetch_assoc(mysqli_query($conn,"SELECT COUNT(*) as total FROM family_members"))['total'];

include "layout/header.php";
?>

<div class="main">

<h1>Dashboard</h1>

<p>Welcome to Digital Panchayat Admin Panel</p>

<br>

<div style="display:flex;gap:20px;flex-wrap:wrap;">

<div style="background:#fff;padding:20px;width:220px;border-radius:6px;box-shadow:0 2px 6px rgba(0,0,0,0.1);">
<h2><?php echo $villages; ?></h2>
<p>Total Villages Listed</p>
</div>

<div style="background:#fff;padding:20px;width:220px;border-radius:6px;box-shadow:0 2px 6px rgba(0,0,0,0.1);">
<h2><?php echo $families; ?></h2>
<p>Families Listed</p>
</div>

<div style="background:#fff;padding:20px;width:220px;border-radius:6px;box-shadow:0 2px 6px rgba(0,0,0,0.1);">
<h2><?php echo $members; ?></h2>
<p>Total Members Listed</p>
</div>

</div>

<br><br>

<div style="background:#fff;padding:20px;border-radius:6px;box-shadow:0 2px 6px rgba(0,0,0,0.1);">

<h3>Population Statistics</h3>

<canvas id="statsChart" height="100"></canvas>

</div>

</div>

<!-- Chart.js -->
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>

<script>

const ctx = document.getElementById('statsChart');

new Chart(ctx, {
type: 'bar',
data: {
labels: ['Villages','Families','Members'],
datasets: [{
label: 'Digital Panchayat Statistics',
data: [
<?php echo $villages; ?>,
<?php echo $families; ?>,
<?php echo $members; ?>
],
borderWidth: 1
}]
},
options: {
responsive:true,
scales:{
y:{
beginAtZero:true
}
}
}
});

</script>

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