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

$pagetitle = 'Villagers';
$activemenu = 'villagers';
require 'header.php';
?>

<div class="dfp-card">
  <h3>All Villagers</h3>
  <table id="tblVillagers" class="dfp-table" style="width:100%;">
    <thead>
      <tr><th>Name</th><th>Phone</th><th>Panchayat</th><th>Verified Via</th><th>Status</th><th>Payment</th><th>Pass Active</th><th>Valid Until</th><th>Action</th></tr>
      <tr class="filterrow">
        <th><input class="dfp-colfilter"></th>
        <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>
          <select class="dfp-colfilter">
            <option value="">All</option>
            <option value="paid">Paid</option>
            <option value="unpaid">Unpaid</option>
          </select>
        </th>
        <th></th><th></th><th></th>
      </tr>
    </thead>
    <tbody></tbody>
  </table>
</div>

<?php require 'footer.php'; ?>
<script>
var tblVillagers;

$(function () {
  tblVillagers = $('#tblVillagers').DataTable({
    ajax: { url: 'ajax_admin_villager.php', data: { action: 'list' }, dataSrc: 'data' },
    createdRow: function (row, data) {
      if (data.verifymethod === 'house') {
        $(row).addClass('rowneedsmanualcheck');
      }
    },
    columns: [
      { data: 'fullname' },
      { data: 'phone' },
      { data: 'panchayatname' },
      { 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: 'passenddate', render: function (d) { return d ? d : '-'; } },
      { data: null, orderable: false, render: function (d) {
          return '<a class="btn btn-sm btn-outline" href="admin_villagerview.php?id=' + d.id + '">View</a>';
        } }
    ]
  });

  dfpEnableColumnFilters('#tblVillagers');
});
</script>
<?php require 'footer2.php'; ?>
