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

$pagetitle = 'Dashboard';
$activemenu = 'dashboard';
require 'header.php';
?>

<div class="dfp-stats-row" id="dfpStatsRow">
  <div class="dfp-stat-box"><div class="num" id="statTotal">-</div><div class="lbl">Total Villagers</div></div>
  <div class="dfp-stat-box"><div class="num" id="statPending">-</div><div class="lbl">Pending Approval</div></div>
  <div class="dfp-stat-box"><div class="num" id="statApproved">-</div><div class="lbl">Approved</div></div>
  <div class="dfp-stat-box"><div class="num" id="statRejected">-</div><div class="lbl">Rejected</div></div>
  <div class="dfp-stat-box"><div class="num" id="statActive">-</div><div class="lbl">Active Passes</div></div>
  <div class="dfp-stat-box"><div class="num" id="statExpiring">-</div><div class="lbl">Expiring in 30 days</div></div>
</div>

<div class="dfp-card">
  <h3>Pending Registrations Needing Your Review</h3>
  <table id="tblPendingQuick" class="dfp-table" style="width:100%;">
    <thead><tr><th>Name</th><th>Phone</th><th>Submitted</th><th>Action</th></tr></thead>
    <tbody></tbody>
  </table>
</div>

<?php require 'footer.php'; ?>
<script>
function dfpRenderDashboard(data) {
  $('#statTotal').text(data.total);
  $('#statPending').text(data.pending);
  $('#statApproved').text(data.approved);
  $('#statRejected').text(data.rejected);
  $('#statActive').text(data.active);
  $('#statExpiring').text(data.expiring);

  var tbody = $('#tblPendingQuick tbody').empty();
  if (data.pendingrows.length === 0) {
    tbody.append('<tr><td colspan="4">No pending registrations right now.</td></tr>');
  } else {
    data.pendingrows.forEach(function (r) {
      tbody.append('<tr><td>' + r.fullname + '</td><td>' + r.phone + '</td><td>' + r.createdat +
        '</td><td><a class="btn btn-sm btn-outline" href="panchayat_registrationview.php?id=' + r.id + '">Review</a></td></tr>');
    });
  }
}

$(function () {
  dfpStartAutoRefresh('ajax_panchayat_dashboardstats.php', dfpRenderDashboard, 60000);
});
</script>
<?php require 'footer2.php'; ?>
