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

$pagetitle = 'Registrations';
$activemenu = 'registrations';
require 'header.php';
?>

<div class="dfp-card">
  <h3>Registrations
    <a href="panchayat_registerassisted.php" class="btn btn-sm" style="float:right;">+ Register a Villager (Assisted)</a>
  </h3>
  <p style="font-size:var(--fontsize-sm);">
    <span class="badge badge-manual">House Number</span> entries are highlighted below - these need manual
    address verification since there's no Aadhaar to auto-confirm residency.
  </p>
  <table id="tblRegistrations" class="dfp-table" style="width:100%;">
    <thead>
      <tr><th>Name</th><th>Phone</th><th>Verified Via</th><th>Status</th><th>Payment</th><th>Pass Active</th><th>Submitted</th><th>Action</th></tr>
      <tr class="filterrow">
        <th><input class="dfp-colfilter"></th>
        <th><input class="dfp-colfilter"></th>
        <th>
          <select class="dfp-colfilter">
            <option value="">All</option>
            <option value="aadhaar">Aadhaar</option>
            <option value="house">House Number</option>
          </select>
        </th>
        <th>
          <select class="dfp-colfilter">
            <option value="">All</option>
            <option value="pending">Pending</option>
            <option value="approved">Approved</option>
            <option value="rejected">Rejected</option>
          </select>
        </th>
        <th></th><th></th><th></th><th></th>
      </tr>
    </thead>
    <tbody></tbody>
  </table>
</div>

<?php require 'footer.php'; ?>
<script>
$(function () {
  var tbl = $('#tblRegistrations').DataTable({
    order: [[6, 'desc']],
    ajax: { url: 'ajax_panchayat_registration.php', data: { action: 'list' }, dataSrc: 'data' },
    createdRow: function (row, data) {
      if (data.verifymethod === 'house') {
        $(row).addClass('rowneedsmanualcheck');
      }
    },
    columns: [
      { data: 'fullname' },
      { data: 'phone' },
      { data: 'verifymethod', render: function (d) {
          return d === 'house' ? '<span class="badge badge-manual">House Number</span>' : 'Aadhaar';
        } },
      { data: 'status', render: function (d) {
          var cls = d === 'approved' ? 'badge-approved' : (d === 'rejected' ? 'badge-rejected' : 'badge-pending');
          return '<span class="badge ' + cls + '">' + d + '</span>';
        } },
      { data: 'paymentstatus', render: function (d) {
          return d === 'paid' ? '<span class="badge badge-approved">Paid</span>' : '<span class="badge badge-pending">Unpaid</span>';
        } },
      { data: 'passactive', render: function (d) { return d == 1 ? 'Yes' : 'No'; } },
      { data: 'createdat' },
      { data: null, orderable: false, render: function (d) {
          return '<a class="btn btn-sm btn-outline" href="panchayat_registrationview.php?id=' + d.id + '">View</a>';
        } }
    ]
  });
  dfpEnableColumnFilters('#tblRegistrations');
});
</script>
<?php require 'footer2.php'; ?>
