<?php
require 'db.php';
require 'functions.php';
requireRole(['panchayatuser']);

$pagetitle = 'Reports';
$activemenu = 'reports';
require 'header.php';
?>

<div class="dfp-stats-row">
  <div class="dfp-card" style="flex:1;min-width:280px;">
    <h3>Registrations (Last 30 Days)</h3>
    <canvas id="chartRegistrations" height="140"></canvas>
  </div>
  <div class="dfp-card" style="flex:1;min-width:220px;">
    <h3>By Status</h3>
    <canvas id="chartStatus" height="140"></canvas>
  </div>
</div>

<div class="dfp-card">
  <h3>Export</h3>
  <a class="btn btn-sm" href="panchayat_reportexport.php">Export Villagers (Excel)</a>
</div>

<?php require 'footer.php'; ?>
<script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.0/dist/chart.umd.min.js"></script>
<script>
$(function () {
  $.getJSON('ajax_panchayat_reportdata.php', function (resp) {
    if (!resp.success) { dfpToast('Failed to load report data', 'error'); return; }
    var d = resp.data;

    new Chart(document.getElementById('chartRegistrations').getContext('2d'), {
      type: 'line',
      data: { labels: d.dailylabels, datasets: [{ label: 'Registrations', data: d.dailycounts, borderColor: '#111111', backgroundColor: '#111111', fill: false }] },
      options: { plugins: { legend: { display: false } }, scales: { y: { beginAtZero: true, ticks: { precision: 0 } } } }
    });

    new Chart(document.getElementById('chartStatus').getContext('2d'), {
      type: 'pie',
      data: { labels: d.statuslabels, datasets: [{ data: d.statuscounts, backgroundColor: ['#666666', '#111111', '#999999'] }] },
      options: {}
    });
  });
});
</script>
<?php require 'footer2.php'; ?>
