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

$pagetitle = 'Admin Users';
$activemenu = 'users';
require 'header.php';
?>

<div class="dfp-card">
  <h3>Admin Users <button class="btn btn-sm" style="float:right;" onclick="openModal()">+ Add Admin</button></h3>
  <table id="tblAdmins" class="dfp-table" style="width:100%;">
    <thead>
      <tr><th>Name</th><th>Username</th><th>Phone</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></th><th></th>
      </tr>
    </thead>
    <tbody></tbody>
  </table>
</div>

<div id="adminModal" class="dfpmodal" style="display:none;">
  <div class="dfpmodalbox">
    <h3 id="adminModalTitle">Add Admin</h3>
    <form id="frmAdmin">
      <input type="hidden" name="id" id="aId">
      <div class="dfp-formgroup"><label>Name</label><input type="text" name="name" id="aName" required></div>
      <div class="dfp-formgroup"><label>Username</label><input type="text" name="username" id="aUsername" required></div>
      <div class="dfp-formgroup"><label>Phone</label><input type="text" name="phone" id="aPhone" required></div>
      <div class="dfp-formgroup" id="aPasswordWrap"><label>Password</label><input type="password" name="password" id="aPassword"></div>
      <button type="submit" class="btn">Save</button>
      <button type="button" class="btn btn-outline" onclick="$('#adminModal').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 tblAdmins;
var myOwnId = <?php echo (int)currentUserId(); ?>;

function openModal(row) {
  $('#frmAdmin')[0].reset();
  $('#aId').val(row ? row.id : '');
  $('#aName').val(row ? row.name : '');
  $('#aUsername').val(row ? row.username : '');
  $('#aPhone').val(row ? row.phone : '');
  $('#aPasswordWrap label').text(row ? 'New Password (leave blank to keep unchanged)' : 'Password');
  $('#aPasswordWrap input').prop('required', !row);
  $('#adminModalTitle').text(row ? 'Edit Admin' : 'Add Admin');
  $('#adminModal').show();
}

$(function () {
  tblAdmins = $('#tblAdmins').DataTable({
    ajax: { url: 'ajax_admin_adminuser.php', data: { action: 'list' }, dataSrc: 'data' },
    columns: [
      { data: 'name' }, { data: 'username' }, { data: 'phone' },
      { 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) {
          var btns = '<button class="btn btn-sm btn-outline" onclick=\'openModal(' + JSON.stringify(d) + ')\'>Edit</button>';
          if (d.id != myOwnId) {
            btns += ' <button class="btn btn-sm btn-danger" onclick="toggleStatus(' + d.id + ')">' + (d.isactive == 1 ? 'Deactivate' : 'Activate') + '</button>';
          }
          return btns;
        } }
    ]
  });
  dfpEnableColumnFilters('#tblAdmins');

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

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