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

$pagetitle = 'RND Users';
$activemenu = 'rndusers';
require 'header.php';
?>

<div class="dfp-card">
  <h3>RND Portal Users <button class="btn btn-sm" style="float:right;" onclick="openModal()">+ Add User</button></h3>
  <table id="tblRnd" 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="rndModal" class="dfpmodal" style="display:none;">
  <div class="dfpmodalbox">
    <h3 id="rndModalTitle">Add RND User</h3>
    <form id="frmRnd">
      <input type="hidden" name="id" id="rId">
      <div class="dfp-formgroup"><label>Name</label><input type="text" name="name" id="rName" required></div>
      <div class="dfp-formgroup"><label>Username</label><input type="text" name="username" id="rUsername" required></div>
      <div class="dfp-formgroup"><label>Phone</label><input type="text" name="phone" id="rPhone" required></div>
      <div class="dfp-formgroup" id="rPasswordWrap"><label>Password</label><input type="password" name="password" id="rPassword"></div>
      <button type="submit" class="btn">Save</button>
      <button type="button" class="btn btn-outline" onclick="$('#rndModal').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 tblRnd;

function openModal(row) {
  $('#frmRnd')[0].reset();
  $('#rId').val(row ? row.id : '');
  $('#rName').val(row ? row.name : '');
  $('#rUsername').val(row ? row.username : '');
  $('#rPhone').val(row ? row.phone : '');
  $('#rPasswordWrap label').text(row ? 'New Password (leave blank to keep unchanged)' : 'Password');
  $('#rPasswordWrap input').prop('required', !row);
  $('#rndModalTitle').text(row ? 'Edit RND User' : 'Add RND User');
  $('#rndModal').show();
}

$(function () {
  tblRnd = $('#tblRnd').DataTable({
    ajax: { url: 'ajax_admin_rnduser.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) {
          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('#tblRnd');

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

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