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

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

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

<div class="dfp-card">
  <h3>Export</h3>
  <a class="btn btn-sm" href="rnd_reportexport.php">Export Completed Registrations (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_rnd_reportdata.php', function (resp) {
    if (!resp.success) { dfpToast('Failed to load report data', 'error'); return; }
    var d = resp.data;

    new Chart(document.getElementById('chartCompleted').getContext('2d'), {
      type: 'line',
      data: { labels: d.dailylabels, datasets: [{ label: 'Completed', 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('chartPanchayat').getContext('2d'), {
      type: 'pie',
      data: { labels: d.panchayatlabels, datasets: [{ data: d.panchayatcounts, backgroundColor: ['#111111', '#666666'] }] },
      options: {}
    });
  });
});
</script>
<?php require 'footer2.php'; ?>
