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

$pagetitle = 'Ticket Checkers';
$activemenu = 'checkers';
require 'header.php';
?>

<div class="dfp-card">
  <h3>Ticket Checkers <button class="btn btn-sm" style="float:right;" onclick="openModal()">+ Add Checker</button></h3>
  <table id="tblCheckers" class="dfp-table" style="width:100%;">
    <thead>
      <tr><th>Name</th><th>Phone</th><th>Username</th><th>Panchayat</th><th>Status</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><input class="dfp-colfilter"></th><th></th><th></th>
      </tr>
    </thead>
    <tbody></tbody>
  </table>
</div>

<div id="checkerModal" class="dfpmodal" style="display:none;">
  <div class="dfpmodalbox">
    <h3 id="checkerModalTitle">Add Checker</h3>
    <form id="frmChecker">
      <input type="hidden" name="id" id="cId">
      <div class="dfp-formgroup"><label>Name</label><input type="text" name="name" id="cName" required></div>
      <div class="dfp-formgroup"><label>Phone</label><input type="text" name="phone" id="cPhone" required></div>
      <div class="dfp-formgroup"><label>Username</label><input type="text" name="username" id="cUsername" required></div>
      <div class="dfp-formgroup">
        <label>Panchayat (optional - blank means both)</label>
        <select name="panchayatid" id="cPanchayatId"><option value="">-- Both --</option></select>
      </div>
      <div class="dfp-formgroup" id="cPasswordWrap"><label>Password</label><input type="password" name="password" id="cPassword"></div>
      <button type="submit" class="btn">Save</button>
      <button type="button" class="btn btn-outline" onclick="$('#checkerModal').hide()">Cancel</button>
    </form>
  </div>
</div>
<style>.dfpmodal{position:fixed;top:0;left:0;right:0;bottom:0;background:rgba(0,0,0,0.5);z-index:2000;}.dfpmodalbox{background:#fff;max-width:420px;margin:60px auto;padding:16px;border-radius:4px;}</style>

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

function loadPanchayatOptionsForChecker(callback) {
  $.getJSON('ajax_admin_panchayat.php', { action: 'listall' }, function (resp) {
    var sel = $('#cPanchayatId');
    sel.find('option:not(:first)').remove();
    (resp.data || []).forEach(function (p) {
      sel.append('<option value="' + p.id + '">' + p.name + '</option>');
    });
    if (callback) { callback(); }
  });
}

function openModal(row) {
  loadPanchayatOptionsForChecker(function () {
    $('#frmChecker')[0].reset();
    $('#cId').val(row ? row.id : '');
    $('#cName').val(row ? row.name : '');
    $('#cPhone').val(row ? row.phone : '');
    $('#cUsername').val(row ? row.username : '');
    $('#cPanchayatId').val(row && row.panchayatid ? row.panchayatid : '');
    $('#cPasswordWrap label').text(row ? 'New Password (leave blank to keep unchanged)' : 'Password');
    $('#cPasswordWrap input').prop('required', !row);
    $('#checkerModalTitle').text(row ? 'Edit Checker' : 'Add Checker');
    $('#checkerModal').show();
  });
}

$(function () {
  tblCheckers = $('#tblCheckers').DataTable({
    ajax: { url: 'ajax_admin_ticketchecker.php', data: { action: 'list' }, dataSrc: 'data' },
    columns: [
      { data: 'name' }, { data: 'phone' }, { data: 'username' },
      { data: 'panchayatname', render: function (d) { return d ? d : 'Both'; } },
      { data: 'isactive', render: function (d) { return d == 1 ? '<span class="badge badge-approved">Active</span>' : '<span class="badge badge-inactive">Inactive</span>'; } },
      { data: null, orderable: false, render: function (d) {
          return '<button class="btn btn-sm btn-outline" onclick=\'openModal(' + JSON.stringify(d) + ')\'>Edit</button> ' +
                 '<button class="btn btn-sm btn-danger" onclick="toggleStatus(' + d.id + ')">' + (d.isactive == 1 ? 'Deactivate' : 'Activate') + '</button>';
        } }
    ]
  });
  dfpEnableColumnFilters('#tblCheckers');

  $('#frmChecker').on('submit', function (e) {
    e.preventDefault();
    $.post('ajax_admin_ticketchecker.php', $(this).serialize() + '&action=save', function (resp) {
      dfpToast(resp.message, resp.success ? 'success' : 'error');
      if (resp.success) { $('#checkerModal').hide(); tblCheckers.ajax.reload(null, false); }
    }, 'json');
  });
});

function toggleStatus(id) {
  $.post('ajax_admin_ticketchecker.php', { action: 'togglestatus', id: id }, function (resp) {
    dfpToast(resp.message, resp.success ? 'success' : 'error');
    if (resp.success) { tblCheckers.ajax.reload(null, false); }
  }, 'json');
}
</script>
<?php require 'footer2.php'; ?>
