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

$pagetitle = 'Expiring Passes';
$activemenu = 'expiring';
$defaultrenewalyears = getSetting($pdo, 'renewalyears', '1');
require 'header.php';
?>

<div class="dfp-card">
  <h3>Passes Expiring Within 60 Days</h3>
  <table id="tblExpiring" class="dfp-table" style="width:100%;">
    <thead>
      <tr><th>Name</th><th>Phone</th><th>Valid Until</th><th>Renew By</th><th>Action</th></tr>
      <tr class="filterrow">
        <th><input class="dfp-colfilter"></th><th><input class="dfp-colfilter"></th><th></th><th></th><th></th>
      </tr>
    </thead>
    <tbody></tbody>
  </table>
</div>

<?php require 'footer.php'; ?>
<script>
var tblExpiring;
var defaultRenewalYears = '<?php echo safeOut($defaultrenewalyears); ?>';

$(function () {
  tblExpiring = $('#tblExpiring').DataTable({
    order: [[2, 'asc']],
    ajax: { url: 'ajax_panchayat_expiring.php', data: { action: 'list' }, dataSrc: 'data' },
    columns: [
      { data: 'fullname' }, { data: 'phone' }, { data: 'passenddate' },
      { data: null, orderable: false, render: function (d) {
          return '<select class="dfp-colfilter" style="width:90px;display:inline-block;" id="renew_' + d.id + '">' +
            '<option value="1"' + (defaultRenewalYears == '1' ? ' selected' : '') + '>1 Year</option>' +
            '<option value="2"' + (defaultRenewalYears == '2' ? ' selected' : '') + '>2 Years</option></select>';
        } },
      { data: null, orderable: false, render: function (d) {
          return '<button class="btn btn-sm" onclick="renewPass(' + d.id + ')">Renew</button>';
        } }
    ]
  });
  dfpEnableColumnFilters('#tblExpiring');
});

function renewPass(id) {
  var years = $('#renew_' + id).val();
  $.post('ajax_panchayat_expiring.php', { action: 'renew', id: id, years: years }, function (resp) {
    dfpToast(resp.message, resp.success ? 'success' : 'error');
    if (resp.success) { tblExpiring.ajax.reload(null, false); }
  }, 'json');
}
</script>
<?php require 'footer2.php'; ?>
