/* Flexi Access Bank – Admin JS */
'use strict';

// Sidebar toggle
const sidebar = document.getElementById('adminSidebar');
const mainEl  = document.querySelector('.admin-main');
const toggleBtn = document.getElementById('sidebarToggle');
if (toggleBtn && sidebar) {
  toggleBtn.addEventListener('click', () => {
    sidebar.classList.toggle('open');
    sidebar.classList.toggle('collapsed');
    mainEl && mainEl.classList.toggle('expanded');
  });
}

// Auto-dismiss alerts
document.querySelectorAll('.alert').forEach(el => {
  setTimeout(() => {
    const a = bootstrap.Alert.getOrCreateInstance(el);
    a && a.close();
  }, 6000);
});

// Confirm prompts
document.querySelectorAll('[data-confirm]').forEach(el => {
  el.addEventListener('click', e => {
    if (!confirm(el.dataset.confirm)) e.preventDefault();
  });
});

// DataTable-like search
const searchInput = document.getElementById('tableSearch');
if (searchInput) {
  searchInput.addEventListener('input', function() {
    const q = this.value.toLowerCase();
    document.querySelectorAll('.searchable-table tbody tr').forEach(row => {
      row.style.display = row.textContent.toLowerCase().includes(q) ? '' : 'none';
    });
  });
}

// Copy to clipboard
document.querySelectorAll('[data-copy]').forEach(el => {
  el.addEventListener('click', function() {
    navigator.clipboard.writeText(this.dataset.copy).then(() => {
      const orig = this.innerHTML;
      this.innerHTML = '<i class="fa fa-check"></i>';
      setTimeout(() => this.innerHTML = orig, 1500);
    });
  });
});
