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

$pagetitle = 'Payments';
$activemenu = 'payments';
require 'header.php';
?>

<div class="dfp-card">
  <h3>Payment Orders
    <a href="admin_paymentcreateorder.php" class="btn btn-sm" style="float:right;">+ Create Order for Incomplete Payment</a>
  </h3>
  <table id="tblPayments" class="dfp-table" style="width:100%;">
    <thead>
      <tr><th>Villager</th><th>Order ID</th><th>Payment ID</th><th>Amount</th><th>Status</th><th>Created By</th><th>Created At</th><th>Paid At</th></tr>
      <tr class="filterrow">
        <th><input class="dfp-colfilter"></th>
        <th><input class="dfp-colfilter"></th>
        <th><input class="dfp-colfilter"></th>
        <th></th>
        <th>
          <select class="dfp-colfilter">
            <option value="">All</option>
            <option value="created">Created</option>
            <option value="paid">Paid</option>
            <option value="failed">Failed</option>
            <option value="cancelled">Cancelled</option>
          </select>
        </th>
        <th><input class="dfp-colfilter"></th><th></th><th></th>
      </tr>
    </thead>
    <tbody></tbody>
  </table>
</div>

<?php require 'footer.php'; ?>
<script>
$(function () {
  var tbl = $('#tblPayments').DataTable({
    ajax: { url: 'ajax_admin_payment.php', data: { action: 'list' }, dataSrc: 'data' },
    columns: [
      { data: 'fullname' },
      { data: 'razorpayorderid' },
      { data: 'razorpaypaymentid', render: function (d) { return d ? d : '-'; } },
      { data: 'amountformatted' },
      { data: 'status', render: function (d) {
          var cls = d === 'paid' ? 'badge-approved' : (d === 'failed' || d === 'cancelled' ? 'badge-rejected' : 'badge-pending');
          return '<span class="badge ' + cls + '">' + d + '</span>';
        } },
      { data: 'createdbytype' },
      { data: 'createdat' },
      { data: 'paidat', render: function (d) { return d ? d : '-'; } }
    ]
  });
  dfpEnableColumnFilters('#tblPayments');
});
</script>
<?php require 'footer2.php'; ?>
